我有一个父对象,我想用它构造一个子对象。我想在父母和孩子之间使用共享的storage
特征,其用途可以实现他们自己的storage
。但是我无法理解分享指向父母的弱指针的最佳方法
trait Storage {
fn get(&self, name: &str) -> [&u8]
}
struct DataStore {
pub collections: HashMap<String, Collection>,
pub storage: Storage
}
pub struct Collection {
pub name: String,
pub storage: Rc<Storage>
}
但是当我尝试在collection
实现上实现DataStore
方法时,我得到了这个:
impl DataStore {
fn collection(&mut self, name: &str) -> &Collection {
let collection = self.collections.entry(name.to_string()).or_insert(Collection{
name: name.to_string(),
storage: Rc::new(self.storage)
});
return collection;
}
}
我目前收到此错误:
--> src/lib.rs:24:13
|
24 | storage: Rc::new(self.storage)
| ^^^^^^^ `storage::Storage` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `storage::Storage`
= note: required by `<std::rc::Rc<T>>::new`
我是生锈的新手,但似乎好像我想要对父DataStore.storage
对象或存储的任何实现的弱引用。