Skip to content

Commit

Permalink
Use "natural" <K, V> order for Prefix
Browse files Browse the repository at this point in the history
Makes V optional(Vec<u8> default) too
  • Loading branch information
maurolacy committed Sep 23, 2021
1 parent a0fc176 commit 4fd6b75
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 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 @@ -128,17 +128,17 @@ where
}

// use prefix to scan -> range
pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &p.prefix())
}

// use sub_prefix to scan -> range
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &p.prefix())
}

// use no_prefix to scan -> range
fn no_prefix(&self) -> Prefix<T> {
fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &[])
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,17 @@ where
}

// use prefix to scan -> range
pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &p.prefix())
}

// use sub_prefix to scan -> range
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &p.prefix())
}

// use no_prefix to scan -> range
pub fn no_prefix(&self) -> Prefix<T> {
pub fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
Prefix::new(self.pk_namespace, &[])
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
{
pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(
self.idx_namespace,
&p.prefix(),
Expand All @@ -152,7 +152,7 @@ where
)
}

pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(
self.idx_namespace,
&p.prefix(),
Expand All @@ -161,7 +161,7 @@ where
)
}

fn no_prefix(&self) -> Prefix<T> {
fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(
self.idx_namespace,
&[],
Expand Down Expand Up @@ -314,19 +314,19 @@ where
k.joined_key()
}

pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| {
deserialize_unique_kv(kv)
})
}

pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| {
deserialize_unique_kv(kv)
})
}

fn no_prefix(&self) -> Prefix<T> {
fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
Prefix::with_deserialization_function(self.idx_namespace, &[], &[], |_, _, kv| {
deserialize_unique_kv(kv)
})
Expand Down
12 changes: 6 additions & 6 deletions packages/storage-plus/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ where
}

#[cfg(feature = "iterator")]
pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.namespace, &p.prefix())
}

#[cfg(feature = "iterator")]
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
Prefix::new(self.namespace, &p.prefix())
}

#[cfg(feature = "iterator")]
pub(crate) fn no_prefix(&self) -> Prefix<T> {
pub(crate) fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
Prefix::new(self.namespace, &[])
}

Expand Down Expand Up @@ -116,11 +116,11 @@ where
T: Serialize + DeserializeOwned,
K: PrimaryKey<'a>,
{
pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix<T, K::SuperSuffix> {
pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix<K::SuperSuffix, T> {
Prefix::new(self.namespace, &p.prefix())
}

pub fn prefix_de(&self, p: K::Prefix) -> Prefix<T, K::Suffix> {
pub fn prefix_de(&self, p: K::Prefix) -> Prefix<K::Suffix, T> {
Prefix::new(self.namespace, &p.prefix())
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ where
self.no_prefix_de().keys_de(store, min, max, order)
}

fn no_prefix_de(&self) -> Prefix<T, K> {
fn no_prefix_de(&self) -> Prefix<K, T> {
Prefix::new(self.namespace, &[])
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/storage-plus/src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type DeserializeKvFn<K, T> =
fn(&dyn Storage, &[u8], Pair) -> StdResult<Pair2<<K as Deserializable>::Output, T>>;

#[derive(Clone)]
pub struct Prefix<T, K = Vec<u8>>
pub struct Prefix<K = Vec<u8>, T = Vec<u8>>
where
K: Deserializable,
T: Serialize + DeserializeOwned,
Expand All @@ -62,7 +62,7 @@ where
de_fn: DeserializeKvFn<K, T>,
}

impl<K, T> Deref for Prefix<T, K>
impl<K, T> Deref for Prefix<K, T>
where
K: Deserializable,
T: Serialize + DeserializeOwned,
Expand All @@ -74,7 +74,7 @@ where
}
}

impl<K, T> Prefix<T, K>
impl<K, T> Prefix<K, T>
where
K: Deserializable,
T: Serialize + DeserializeOwned,
Expand Down Expand Up @@ -237,7 +237,7 @@ mod test {
fn ensure_proper_range_bounds() {
let mut store = MockStorage::new();
// manually create this - not testing nested prefixes here
let prefix: Prefix<u64, Vec<u8>> = Prefix {
let prefix: Prefix<Vec<u8>, u64> = Prefix {
storage_prefix: b"foo".to_vec(),
data: PhantomData::<u64>,
pk_name: vec![],
Expand Down
6 changes: 3 additions & 3 deletions packages/storage-plus/src/snapshot/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ where
self.primary.key(k)
}

pub fn prefix(&self, p: K::Prefix) -> Prefix<T> {
pub fn prefix(&self, p: K::Prefix) -> Prefix<Vec<u8>, T> {
self.primary.prefix(p)
}

pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<T> {
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<Vec<u8>, T> {
self.primary.sub_prefix(p)
}

fn no_prefix(&self) -> Prefix<T> {
fn no_prefix(&self) -> Prefix<Vec<u8>, T> {
self.primary.no_prefix()
}

Expand Down

0 comments on commit 4fd6b75

Please sign in to comment.