Skip to content

Commit

Permalink
&mut self for max flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed May 27, 2024
1 parent 58d3d6d commit 28a733f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,26 @@ where

/// A trait representing a key-value BLOB storage.
pub trait KvBlobStore {
async fn load<'a>(&self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error>;
async fn load<'a>(&mut self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error>;
async fn store(&mut self, key: &str, value: &[u8]) -> Result<(), Error>;
async fn remove(&self, key: &str) -> Result<(), Error>;
async fn remove(&mut self, key: &str) -> Result<(), Error>;
}

impl<T> KvBlobStore for &mut T
where
T: KvBlobStore,
{
async fn load<'a>(&mut self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
T::load(self, key, buf).await
}

async fn store(&mut self, key: &str, value: &[u8]) -> Result<(), Error> {
T::store(self, key, value).await
}

async fn remove(&mut self, key: &str) -> Result<(), Error> {
T::remove(self, key).await
}
}

/// An implementation of the `KvBlobStore` trait that stores the BLOBs in a directory.
Expand Down Expand Up @@ -309,15 +326,15 @@ impl DirKvStore {

#[cfg(feature = "std")]
impl KvBlobStore for DirKvStore {
async fn load<'a>(&self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
async fn load<'a>(&mut self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
DirKvStore::load(self, key, buf)
}

async fn store(&mut self, key: &str, value: &[u8]) -> Result<(), Error> {
DirKvStore::store(self, key, value)
}

async fn remove(&self, key: &str) -> Result<(), Error> {
async fn remove(&mut self, key: &str) -> Result<(), Error> {
DirKvStore::remove(self, key)
}
}

0 comments on commit 28a733f

Please sign in to comment.