Skip to content

Commit

Permalink
All code compiles, tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 12, 2020
1 parent e925aed commit 2fc3214
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ where
/// this must load the old value to update the indexes properly
/// if you loaded the old value earlier in the same function, use replace to avoid needless db reads
pub fn save(&mut self, store: &mut S, key: K, data: &T) -> StdResult<()> {
let old_data = self.may_load(store, key)?;
let old_data = self.may_load(store, key.clone())?;
self.replace(store, key, Some(data), old_data.as_ref())
}

pub fn remove(&mut self, store: &mut S, key: K) -> StdResult<()> {
let old_data = self.may_load(store, key)?;
let old_data = self.may_load(store, key.clone())?;
self.replace(store, key, None, old_data.as_ref())
}

Expand Down Expand Up @@ -156,7 +156,7 @@ where
A: FnOnce(Option<T>) -> Result<T, E>,
E: From<StdError>,
{
let input = self.may_load(store, key)?;
let input = self.may_load(store, key.clone())?;
let old_val = input.clone();
let output = action(input)?;
self.replace(store, key, Some(&output), old_val.as_ref())?;
Expand Down
8 changes: 5 additions & 3 deletions packages/storage-plus/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::Endian;
use std::marker::PhantomData;

// pub trait PrimaryKey<'a>: Copy {
pub trait PrimaryKey<'a> {
pub trait PrimaryKey<'a>: Clone {
type Prefix: Prefixer<'a>;

/// returns a slice of key steps, which can be optionally combined
Expand All @@ -23,7 +23,7 @@ pub trait PrimaryKey<'a> {
// optional type aliases to refer to them easier
type Pk0 = ();
type Pk1<'a> = &'a [u8];
type Pk2<'a> = (&'a [u8], &'a [u8]);
type Pk2<'a, T = &'a [u8], U = &'a [u8]> = (T, U);

impl<'a> PrimaryKey<'a> for Pk1<'a> {
type Prefix = Pk0;
Expand Down Expand Up @@ -90,6 +90,7 @@ impl EmptyPrefix for () {
}

// Add support for an dynamic keys - constructor functions below
#[derive(Clone, Debug)]
pub struct PkOwned(pub Vec<u8>);

impl<'a> PrimaryKey<'a> for PkOwned {
Expand All @@ -111,7 +112,7 @@ impl<'a> Prefixer<'a> for PkOwned {
}

// this auto-implements PrimaryKey for all the IntKey types (and more!)
impl<'a, T: AsRef<PkOwned> + From<PkOwned>> PrimaryKey<'a> for T {
impl<'a, T: AsRef<PkOwned> + From<PkOwned> + Clone> PrimaryKey<'a> for T {
type Prefix = ();

fn key<'b>(&'b self) -> Vec<&'b [u8]> {
Expand Down Expand Up @@ -141,6 +142,7 @@ pub type U128Key = IntKey<u128>;
/// let k = U64Key::new(12345);
/// let k = U32Key::from(12345);
/// let k: U16Key = 12345.into();
#[derive(Clone, Debug)]
pub struct IntKey<T: Endian> {
pub wrapped: PkOwned,
pub data: PhantomData<T>,
Expand Down

0 comments on commit 2fc3214

Please sign in to comment.