Rust: project 3 `trait KvsEngine`'s `fn remove`

Author: thanhnguyen2187Created Jan 6, 2025Updated Jan 6, 2025
Labelstype/enhancement

Now, the trait is:

rust
pub trait KvsEngine: Send + Sync {
    fn set(&mut self, key: String, value: String) -> Result<()>;
    fn get(&self, key: String) -> Result<Option<String>>;
    fn remove(&mut self, key: String) -> Result<()>;
}

I think Result<()> of remove is not good typing, as it groups both key not found and other errors into one type. The signature of get is much better, as we can infer from the returned result:

  • Result(Some(...)): a corresponding value is found
  • Result(None): there is no corresponding value
  • Error(...): error happened

My proposal is to have remove returns the same type as get:

rust
pub trait KvsEngine: Send + Sync {
    fn set(&mut self, key: String, value: String) -> Result<()>;
    fn get(&self, key: String) -> Result<Option<String>>;
    fn remove(&mut self, key: String) -> Result<Option<String>>;
}