rust - 如何在不破坏封装的情况下返回对 RefCell 中某些内容的引用?

标签 rust encapsulation contravariance mutability interior-mutability

我有一个具有内部可变性的结构。

use std::cell::RefCell;

struct MutableInterior {
    hide_me: i32,
    vec: Vec<i32>,
}
struct Foo {
    //although not used in this particular snippet,
    //the motivating problem uses interior mutability
    //via RefCell.
    interior: RefCell<MutableInterior>,
}

impl Foo {
    pub fn get_items(&self) -> &Vec<i32> {
        &self.interior.borrow().vec
    }
}

fn main() {
    let f = Foo {
        interior: RefCell::new(MutableInterior {
            vec: Vec::new(),
            hide_me: 2,
        }),
    };
    let borrowed_f = &f;
    let items = borrowed_f.get_items();
}

产生错误:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:16:10
   |
16 |         &self.interior.borrow().vec
   |          ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
17 |     }
   |     - temporary value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 15:5...
  --> src/main.rs:15:5
   |
15 | /     pub fn get_items(&self) -> &Vec<i32> {
16 | |         &self.interior.borrow().vec
17 | |     }
   | |_____^

问题是我无法在 Foo 上使用函数返回借来的 vec , 因为借了vec仅在 Ref 的生命周期内有效,但是 Ref立即超出范围。

我认为 Ref必须坚持 because :

RefCell<T> uses Rust's lifetimes to implement 'dynamic borrowing', a process whereby one can claim temporary, exclusive, mutable access to the inner value. Borrows for RefCell<T>s are tracked 'at runtime', unlike Rust's native reference types which are entirely tracked statically, at compile time. Because RefCell<T> borrows are dynamic it is possible to attempt to borrow a value that is already mutably borrowed; when this happens it results in task panic.

现在我可以改为编写一个返回整个内部的函数:

pub fn get_mutable_interior(&self) -> std::cell::Ref<MutableInterior>;

然而,这可能会向 MutableInterior.hide_me 公开字段(在本例中为 Foo),这些字段是真正私有(private)的实现细节。 .

理想情况下,我只想公开 vec本身,可能有一个守卫来实现动态借用行为。这样调用者就不必了解 hide_me 了。 .

最佳答案

您可以使用 Ref::map 而不是创建一个全新的类型(自 Rust 1.8 起)。这与 Levans' existing answer 具有相同的结果:

use std::cell::Ref;

impl Foo {
    pub fn get_items(&self) -> Ref<'_, Vec<i32>> {
        Ref::map(self.interior.borrow(), |mi| &mi.vec)
    }
}

您还可以使用 impl Trait 等新功能从 API 中隐藏 Ref:

use std::cell::Ref;
use std::ops::Deref;

impl Foo {
    pub fn get_items(&self) -> impl Deref<Target = Vec<i32>> + '_ {
        Ref::map(self.interior.borrow(), |mi| &mi.vec)
    }
}

关于rust - 如何在不破坏封装的情况下返回对 RefCell 中某些内容的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29401626/

相关文章:

generics - Kotlin 泛型类属性

vector - Rust 特征向量 : cast each trait

iterator - 如何设置盒装闭包捕获 `self` 的生命周期?

rust - 如何在不通过String的情况下从Rust中的Vec <char>转换为u32?

Java:封装库的自动方式

c# - 实例化参数化类型列表,更好地利用泛型和 Linq

rust - 分块字符串向量

java - java中封装的清晰性

java - 为什么私有(private) getter 方法会违反封装性,而缺少 getter 方法则不会?

c# - c++/cli 中的委托(delegate)方差