![](/img/trans.png)
[英]Is there an alternative or way to have Rc<RefCell<X>> that restricts mutability of X?
[英]Why is my zero-cost alternative to RefCell not the standard way of achieving interior mutability?
我一直在思考為什么在大多數情況下 Rust 的內部可變性需要運行時檢查(例如RefCell
)。 看起來我找到了一個沒有運行時成本的安全替代方案。 我將類型稱為SafeCell
(主要是因為它是UnsafeCell
的安全包裝器),它允許您將任何 function 應用於包裝值,而不會有引用轉義的風險:
struct SafeCell<T> {
inner: UnsafeCell<T>,
}
impl<T> SafeCell<T> {
pub fn new(value: T) -> Self {
Self {
inner: UnsafeCell::new(value),
}
}
pub fn apply<R, F>(&self, fun: F) -> R
where
F: FnOnce(&mut T) -> R,
{
// Reference below has a lifetime of the current scope, so if
// user tries to save it somewhere, borrow checker will catch this.
let reference: &mut T = unsafe { &mut *self.inner.get() };
fun(reference)
}
}
這種類型可用於內部可變性,如下所示:
pub struct MySet {
set: HashSet<i32>,
unique_lookups: SafeCell<HashSet<i32>>,
}
impl MySet {
pub fn contains(&self, value: i32) -> bool {
self.unique_lookups.apply(|lookups| lookups.insert(value));
self.set.contains(value)
}
pub fn unique_lookups_count(&self) -> usize {
self.unique_lookups.apply(|lookups| lookups.len())
}
}
或與Rc
結合使用:
fn foo(rc: Rc<SafeCell<String>>) {
rc.apply(|string| {
if string.starts_with("hello") {
string.push_str(", world!")
}
println!("{}", string);
});
}
RefCell
一樣可用,同時提供 static 生命周期檢查而不是運行時檢查。 您的 API 中沒有任何內容可以阻止用戶在為apply
apply
這允許對同一數據同時存在多個可變引用,這是未定義的行為。
let x = SafeCell::new(0);
x.apply(|y| {
x.apply(|z| {
// `y` and `z` are now both mutable references to the same data
// UB!
*y = 1;
*z = 2;
})
});
x.apply(|y| println!("x: {}", y));
Miri 在看到第二個可變引用時正確地指出了這一點。
error: Undefined Behavior: not granting access to tag <untagged> because incompatible item is protected: [Unique for <1651> (call 1230)]
--> src/main.rs:20:42
|
20 | let reference: &mut T = unsafe { &mut *self.inner.get() };
| ^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <untagged> because incompatible item is protected: [Unique for <1651> (call 1230)]
|
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.