I am trying to create a mock object for testing in my Rust project, the mock object implements the trait ‘Dao’.
Here is the trait:
pub trait Dao<T: Identifiable + Serialize + DeserializeOwned + FileDescriptor> {
fn new_id(&self) -> Result<u64, DaoError>;
fn get(&self, id: u64) -> Result<Option<T>, DaoError>;
fn get_some(&self, ids: &Vec<u64>) -> Result<Vec<T>, DaoError>;
fn get_all(&self) -> Result<Vec<T>, DaoError>;
fn save(&mut self, to_save: &T) -> Result<(), DaoError>;
fn update(&self, to_update: &T) -> Result<(), DaoError>;
fn delete(&self, to_delete: &T) -> Result<(), DaoError>;
}
I then try to create my mock which implements this trait:
#[derive(Serialize, Deserialize, Clone)]
pub struct MockComponent {}
impl MockComponent {
pub fn new() -> MockComponent {
return MockComponent {};
}
}
impl Identifiable for MockComponent {
fn id(&self) -> u64 {
return 0;
}
}
impl FileDescriptor for MockComponent {
fn file_descriptor() -> String {
return String::new();
}
}
pub struct MockDao;
impl<MockComponent> Dao<MockComponent> for MockDao {
fn new_id(&self) -> Result<u64, DaoError> {
return Ok(0);
}
// other implemented methods
}
For some reason I have an error on Dao<MockComponent>
telling me that:
the trait bound `MockComponent: components::component_access::FileDescriptor` is not satisfied
`Dao` is a "sealed trait", because to implement it you also need to implement `components::component_access::FileDescriptor`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it
I dont quite understand what is meant by a sealed trait, the trait Dao
is public in my crate, and my MockComponent
implements both Identifiable
and FileDescriptor
. Does it matter where in my folder structure these traits are defined even if they are public and available to the module in which im defining my mock component?