Skip to content

Commit

Permalink
Merge pull request #41 from evavh/fixes
Browse files Browse the repository at this point in the history
Remove some of the doubly wrapped errors, add remove, and add example for Clone

note clone is not yet implemented
  • Loading branch information
dvdsk authored Dec 6, 2024
2 parents 388ea00 + 7b9dfab commit 537d3d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
16 changes: 16 additions & 0 deletions examples/map_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[dbstruct::dbstruct(db=sled)]
#[derive(Clone)]
pub struct Test {
computers: HashMap<String, usize>,
}

fn main() {
let dir = tempdir::TempDir::new("dbstruct_examples").unwrap();
let path = dir.path().join("map_wrapper");

let db = Test::new(&path).unwrap();
db.computers().insert(&"Deep Thought".to_owned(), &42).unwrap();
db.computers().remove(&"Deep Thought".to_owned()).unwrap();

let clone = db.clone();
}
16 changes: 13 additions & 3 deletions src/wrapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::marker::PhantomData;
use tracing::{instrument, trace};

use crate::traits::DataStore;
use crate::Error;

mod extend;
mod iterator;
Expand Down Expand Up @@ -57,18 +56,29 @@ where

/// returns existing value if any was set
#[instrument(skip_all, level = "debug")]
pub fn insert(&self, key: &'a Key, value: &'a Value) -> Result<Option<Value>, Error<E>> {
pub fn insert(
&self,
key: &'a Key,
value: &'a Value,
) -> Result<Option<Value>, E> {
let key = self.prefix(key);
let existing = self.tree.insert(&key, value)?;
Ok(existing)
}

#[instrument(skip_all, level = "debug")]
pub fn get(&self, key: &'a Key) -> Result<Option<Value>, Error<E>> {
pub fn get(&self, key: &'a Key) -> Result<Option<Value>, E> {
let key = self.prefix(key);
let value = self.tree.get(&key)?;
Ok(value)
}

#[instrument(skip_all, level = "debug")]
pub fn remove(&self, key: &'a Key) -> Result<Option<Value>, E> {
let key = self.prefix(key);
let value = self.tree.remove(&key)?;
Ok(value)
}
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src/wrapper/map/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
Key: Serialize + DeserializeOwned,
Value: Serialize + DeserializeOwned,
{
type Error = crate::Error<DS::Error>;
type Error = DS::Error;

fn try_extend<I>(
&mut self,
Expand Down Expand Up @@ -48,7 +48,7 @@ where
Key: Serialize + DeserializeOwned,
Value: Serialize + DeserializeOwned,
{
type Error = crate::Error<DS::Error>;
type Error = DS::Error;

fn try_extend<I>(
&mut self,
Expand Down

0 comments on commit 537d3d9

Please sign in to comment.