关于这一主题,已经提出了多个问题:
答案或多或少:这是不可能的(没有不安全的)。
我自己试过这种不安全的变体,想问一下这种方法是否安全。
我的想法是,我把保护包在一个实现Iterator的结构中。除了保护之外,还存储了一个迭代器,该迭代器将从存储的保护程序中创建:
struct MapIter<'a> {
guard: RwLockReadGuard<'a, HashMap<i32, i32>>,
iter: Iter<'a, i32, i32>,
}它是用以下行创建的:
impl<'a> MapIter<'a> {
fn new(map: &'a RwLock<HashMap<i32, i32>>) -> Box<Self> {
// create a `box Self`
// the iterator remains uninitialized.
let mut boxed = Box::new(Self {
guard: map.read().expect("ToDo"),
iter: unsafe { mem::uninitialized() },
});
// create the iterator from `box Self`.
boxed.iter = unsafe {
(*(&boxed.guard as *const RwLockReadGuard<'a, HashMap<i32, i32>>)).iter()
};
boxed
}
}现在它可以实现Iterator。
impl<'a> Iterator for MapIter<'a> {
type Item = (&'a i32, &'a i32);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}这密码安全吗?
请在游乐场上查看此代码的实际操作。
此外,我还得到了一个微不足道的抛出警告。
warning: trivial cast: warning: trivial cast: `&std::sync::RwLockReadGuard<'_, std::collections::HashMap<i32, i32>>` as `*const std::sync::RwLockReadGuard<'a, std::collections::HashMap<i32, i32>>`. Cast can be replaced by coercion, this might require type ascription or a temporary variable
|
| unsafe { (*(&boxed.guard as *const RwLockReadGuard<'a, HashMap<i32, i32>>)).iter() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|怎么才能避开这一切?
发布于 2018-08-15 19:27:25
不这不安全。我可以使用Container在安全代码中创建一个悬空引用:
let container = Container::new(); // create a container
let r = {
let mut it = container.iter();
it.next() // obtain a reference to part of it
};
container.map.write().unwrap().clear(); // empty the container
println!("{:?}", r); // oh dear.在操场中,这是不太好的编译,因为r包含对数据的引用,这些引用在清除HashMap时无效。
弗拉基米尔·马特维耶夫对类似问题的回答更详细地解释了为什么这是不合理的,并包含以下简明摘要:
您不能这样做,因为它将允许您绕过运行时检查唯一性冲突。
https://stackoverflow.com/questions/51863819
复制相似问题