Compiling the following code:
use std::rc::Rc;
use std::cell::RefCell;
pub enum StorageType {
// ...
}
pub trait Storable {
fn get_nblocks(&self) -> usize;
fn get_storage(&self) -> Rc<RefCell<Storage>>;
}
pub trait Storage {
fn store(&mut self, data: &Storable);
fn get_type(&self) -> &StorageType;
}
pub struct DataVolume<T: Storage> {
nblocks: usize,
storage: Rc<RefCell<T>>,
}
impl<T: Storage> DataVolume<T> {
pub fn new(nblocks: usize, storage: Rc<RefCell<T>>) -> DataVolume<T> {
let this = DataVolume { nblocks: nblocks, storage: storage.clone() };
storage.borrow_mut().store(&this);
this
}
}
impl<T: Storage> Storable for DataVolume<T> {
fn get_nblocks(&self) -> usize {
self.nblocks
}
fn get_storage(&self) -> Rc<RefCell<T>> {
self.storage.clone()
}
}
gives me:
src/store.rs:37:5: 39:6 error: method `get_storage` has an incompatible type
for trait: expected trait store::Storage, found type parameter [E0053]
src/store.rs:37 fn get_storage(&self) -> Rc<RefCell<T>> {
src/store.rs:38 self.storage.clone()
src/store.rs:39 }
error: aborting due to previous error
I tried many things, and this is what I thought would finally be correct... Is my design of data structures itself wrong in the Rust world?
<T:Storage>
during compile time? – Daniel Fath