Skip to content

Commit

Permalink
Inline may_deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 authored and chipshort committed Nov 13, 2023
1 parent 8a1bb1c commit c0c3804
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 42 deletions.
7 changes: 4 additions & 3 deletions src/deque.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{any::type_name, convert::TryInto, marker::PhantomData};

use cosmwasm_std::{to_vec, StdError, StdResult, Storage};
use cosmwasm_std::{from_slice, to_vec, StdError, StdResult, Storage};
use serde::{de::DeserializeOwned, Serialize};

use crate::helpers::{may_deserialize, namespaces_with_key};
use crate::helpers::namespaces_with_key;

// metadata keys need to have different length than the position type (4 bytes) to prevent collisions
const TAIL_KEY: &[u8] = b"t";
Expand Down Expand Up @@ -170,7 +170,8 @@ impl<'a, T: Serialize + DeserializeOwned> Deque<'a, T> {
/// Used internally
fn get_unchecked(&self, storage: &dyn Storage, pos: u32) -> StdResult<Option<T>> {
let prefixed_key = namespaces_with_key(&[self.namespace], &pos.to_be_bytes());
may_deserialize(&storage.get(&prefixed_key))
let value = storage.get(&prefixed_key);
value.map(|v| from_slice(&v)).transpose()
}

/// Removes the value at the given position
Expand Down
38 changes: 3 additions & 35 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,15 @@
//! Everything in this file is only responsible for building such keys
//! and is in no way specific to any kind of storage.
use serde::de::DeserializeOwned;
use std::any::type_name;

use crate::keys::Key;

use cosmwasm_std::{
from_slice, to_vec, Addr, Binary, ContractResult, CustomQuery, QuerierWrapper, QueryRequest,
StdError, StdResult, SystemResult, WasmQuery,
to_vec, Addr, Binary, ContractResult, CustomQuery, QuerierWrapper, QueryRequest, StdError,
StdResult, SystemResult, WasmQuery,
};

/// may_deserialize parses json bytes from storage (Option), returning Ok(None) if no data present
///
/// value is an odd type, but this is meant to be easy to use with output from storage.get (Option<Vec<u8>>)
/// and value.map(|s| s.as_slice()) seems trickier than &value
pub(crate) fn may_deserialize<T: DeserializeOwned>(
value: &Option<Vec<u8>>,
) -> StdResult<Option<T>> {
match value {
Some(vec) => Ok(Some(from_slice(vec)?)),
None => Ok(None),
}
}

/// This is equivalent concat(to_length_prefixed_nested(namespaces), key)
/// But more efficient when the intermediate namespaces often must be recalculated
pub(crate) fn namespaces_with_key(namespaces: &[&[u8]], key: &[u8]) -> Vec<u8> {
Expand Down Expand Up @@ -121,7 +107,7 @@ pub(crate) fn not_found_object_info<T>(key: &[u8]) -> String {
#[cfg(test)]
mod test {
use super::*;
use cosmwasm_std::{to_vec, Uint128};
use cosmwasm_std::Uint128;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand All @@ -148,24 +134,6 @@ mod test {
encode_length(&vec![1; 65536]);
}

#[test]
fn may_deserialize_handles_some() {
let person = Person {
name: "Maria".to_string(),
age: 42,
};
let value = to_vec(&person).unwrap();

let may_parse: Option<Person> = may_deserialize(&Some(value)).unwrap();
assert_eq!(may_parse, Some(person));
}

#[test]
fn may_deserialize_handles_none() {
let may_parse = may_deserialize::<Person>(&None).unwrap();
assert_eq!(may_parse, None);
}

#[test]
fn not_found_object_info_works() {
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{
from_slice, to_vec, Addr, CustomQuery, QuerierWrapper, StdError, StdResult, Storage, WasmQuery,
};

use crate::helpers::{may_deserialize, not_found_object_info};
use crate::helpers::not_found_object_info;

/// Item stores one typed item at the given key.
/// This is an analog of Singleton.
Expand Down Expand Up @@ -60,7 +60,7 @@ where
/// returns an error on issues parsing
pub fn may_load(&self, store: &dyn Storage) -> StdResult<Option<T>> {
let value = store.get(self.storage_key);
may_deserialize(&value)
value.map(|v| from_slice(&v)).transpose()
}

/// Returns `true` if data is stored at the key, `false` otherwise.
Expand Down
4 changes: 2 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize;
use std::marker::PhantomData;

use crate::helpers::{may_deserialize, nested_namespaces_with_key, not_found_object_info};
use crate::helpers::{nested_namespaces_with_key, not_found_object_info};
use crate::keys::Key;
use cosmwasm_std::{from_slice, to_vec, StdError, StdResult, Storage};
use std::ops::Deref;
Expand Down Expand Up @@ -74,7 +74,7 @@ where
/// returns an error on issues parsing
pub fn may_load(&self, store: &dyn Storage) -> StdResult<Option<T>> {
let value = store.get(&self.storage_key);
may_deserialize(&value)
value.map(|v| from_slice(&v)).transpose()
}

/// has returns true or false if any data is at this key, without parsing or interpreting the
Expand Down

0 comments on commit c0c3804

Please sign in to comment.