Skip to content

Commit

Permalink
Return &[u8] from val trait (Fixes #165)
Browse files Browse the repository at this point in the history
Instead of return a Vec<u8> return a reference to avoid cloning the data before
returning.
  • Loading branch information
nytzuga committed Jul 30, 2023
1 parent 01be091 commit e7d2721
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion firewood/src/v2/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub trait DbView {
async fn root_hash(&self) -> Result<HashKey, Error>;

/// Get the value of a specific key
async fn val<K: KeyType>(&self, key: K) -> Result<Option<Vec<u8>>, Error>;
async fn val<K: KeyType>(&self, key: K) -> Result<Option<&[u8]>, Error>;

/// Obtain a proof for a single key
async fn single_key_proof<K: KeyType, V: ValueType>(
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/v2/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl api::DbView for DbView {
todo!()
}

async fn val<K: KeyType>(&self, _key: K) -> Result<Option<Vec<u8>>, api::Error> {
async fn val<K: KeyType>(&self, _key: K) -> Result<Option<&[u8]>, api::Error> {
todo!()
}

Expand Down
2 changes: 1 addition & 1 deletion firewood/src/v2/emptydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl DbView for HistoricalImpl {
Ok(ROOT_HASH)
}

async fn val<K: KeyType>(&self, _key: K) -> Result<Option<Vec<u8>>, Error> {
async fn val<K: KeyType>(&self, _key: K) -> Result<Option<&[u8]>, Error> {
Ok(None)
}

Expand Down
4 changes: 2 additions & 2 deletions firewood/src/v2/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl<T: api::DbView + Send + Sync> api::DbView for Proposal<T> {
todo!()
}

async fn val<K: KeyType>(&self, key: K) -> Result<Option<Vec<u8>>, api::Error> {
async fn val<K: KeyType>(&self, key: K) -> Result<Option<&[u8]>, api::Error> {
// see if this key is in this proposal
match self.delta.get(key.as_ref()) {
Some(change) => match change {
// key in proposal, check for Put or Delete
KeyOp::Put(val) => Ok(Some(val.clone())),
KeyOp::Put(val) => Ok(Some(val)),
KeyOp::Delete => Ok(None), // key was deleted in this proposal
},
None => match &self.base {
Expand Down

0 comments on commit e7d2721

Please sign in to comment.