Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use traits to remove repeating implementations #436

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Serialize;
use crate::indexes::Index;
use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::map::Map;
use crate::prefix::{Bound, Prefix};
use crate::prefix::{Bound, PrefixT};
use crate::Path;

pub trait IndexList<T> {
Expand Down Expand Up @@ -50,6 +50,18 @@ where
}
}

impl<'a, K, T, P, I> PrefixT<'a, P, T> for IndexedMap<'a, K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
P: Prefixer<'a>,
{
fn get_pk_namespace(&self) -> &[u8] {
self.pk_namespace
}
}

impl<'a, K, T, I> IndexedMap<'a, K, T, I>
where
K: PrimaryKey<'a>,
Expand Down Expand Up @@ -126,16 +138,6 @@ where
pub fn may_load(&self, store: &dyn Storage, key: K) -> StdResult<Option<T>> {
self.primary.may_load(store, key)
}

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

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

// short-cut for simple keys, rather than .prefix(()).range(...)
Expand All @@ -144,7 +146,7 @@ where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
K::SubPrefix: EmptyPrefix,
K::NoPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -158,7 +160,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.range(store, min, max, order)
}
}
Expand Down
28 changes: 15 additions & 13 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::prefix::{Bound, Prefix};
use crate::prefix::{Bound, PrefixT};
use crate::snapshot::SnapshotMap;
use crate::{IndexList, Path, Strategy};

Expand Down Expand Up @@ -35,6 +35,18 @@ impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I> {
}
}

impl<'a, K, T, P, I> PrefixT<'a, P, T> for IndexedSnapshotMap<'a, K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
P: Prefixer<'a>,
{
fn get_pk_namespace(&self) -> &[u8] {
self.pk_namespace
}
}

impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
Expand Down Expand Up @@ -150,16 +162,6 @@ where
pub fn may_load(&self, store: &dyn Storage, key: K) -> StdResult<Option<T>> {
self.primary.may_load(store, key)
}

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

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

// short-cut for simple keys, rather than .prefix(()).range(...)
Expand All @@ -168,7 +170,7 @@ where
K: PrimaryKey<'a> + Prefixer<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
K::SubPrefix: EmptyPrefix,
K::NoPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -182,7 +184,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.range(store, min, max, order)
}
}
Expand Down
27 changes: 21 additions & 6 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ where
)
}

fn no_prefix(&self, p: K::NoPrefix) -> Prefix<T> {
Prefix::with_deserialization_function(
self.idx_namespace,
&p.prefix(),
self.pk_namespace,
deserialize_multi_kv,
)
}

pub fn index_key(&self, k: K) -> Vec<u8> {
k.joined_key()
}
Expand Down Expand Up @@ -168,7 +177,7 @@ impl<'a, K, T> MultiIndex<'a, K, T>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
K::SubPrefix: EmptyPrefix,
K::NoPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -182,7 +191,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.range(store, min, max, order)
}

Expand All @@ -193,7 +202,7 @@ where
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.keys(store, min, max, order)
}
}
Expand Down Expand Up @@ -279,6 +288,12 @@ where
})
}

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

/// returns all items that match this secondary index, always by pk Ascending
pub fn item(&self, store: &dyn Storage, idx: K) -> StdResult<Option<Pair<T>>> {
let data = self
Expand All @@ -294,7 +309,7 @@ impl<'a, K, T> UniqueIndex<'a, K, T>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
K::SubPrefix: EmptyPrefix,
K::NoPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -308,7 +323,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.range(store, min, max, order)
}

Expand All @@ -319,7 +334,7 @@ where
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
self.no_prefix(K::NoPrefix::new())
.keys(store, min, max, order)
}
}
14 changes: 14 additions & 0 deletions packages/storage-plus/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use crate::Endian;
pub trait PrimaryKey<'a>: Clone {
type Prefix: Prefixer<'a>;
type SubPrefix: Prefixer<'a>;
// FIXME: Use this after 'error[E0658]: associated type defaults are unstable' is resolved.
// See issue #29661 <https://github.com/rust-lang/rust/issues/29661>
// type NoPrefix: Prefixer<'a> = ();
type NoPrefix: Prefixer<'a>;

/// returns a slice of key steps, which can be optionally combined
fn key(&self) -> Vec<&[u8]>;
Expand All @@ -23,6 +27,7 @@ pub trait PrimaryKey<'a>: Clone {
impl<'a> PrimaryKey<'a> for () {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
vec![]
Expand All @@ -32,6 +37,7 @@ impl<'a> PrimaryKey<'a> for () {
impl<'a> PrimaryKey<'a> for &'a [u8] {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
// this is simple, we don't add more prefixes
Expand All @@ -43,6 +49,7 @@ impl<'a> PrimaryKey<'a> for &'a [u8] {
impl<'a> PrimaryKey<'a> for &'a str {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
// this is simple, we don't add more prefixes
Expand All @@ -54,6 +61,7 @@ impl<'a> PrimaryKey<'a> for &'a str {
impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a>> PrimaryKey<'a> for (T, U) {
type Prefix = T;
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
let mut keys = self.0.key();
Expand All @@ -68,6 +76,7 @@ impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a> + Prefixer<'a>, V:
{
type Prefix = (T, U);
type SubPrefix = T;
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
let mut keys = self.0.key();
Expand Down Expand Up @@ -131,6 +140,7 @@ impl EmptyPrefix for () {
impl<'a> PrimaryKey<'a> for Vec<u8> {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
vec![&self]
Expand All @@ -146,6 +156,7 @@ impl<'a> Prefixer<'a> for Vec<u8> {
impl<'a> PrimaryKey<'a> for String {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
vec![self.as_bytes()]
Expand All @@ -162,6 +173,7 @@ impl<'a> Prefixer<'a> for String {
impl<'a> PrimaryKey<'a> for &'a Addr {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
// this is simple, we don't add more prefixes
Expand All @@ -179,6 +191,7 @@ impl<'a> Prefixer<'a> for &'a Addr {
impl<'a> PrimaryKey<'a> for Addr {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
// this is simple, we don't add more prefixes
Expand All @@ -196,6 +209,7 @@ impl<'a> Prefixer<'a> for Addr {
impl<'a, T: Endian + Clone> PrimaryKey<'a> for IntKey<T> {
type Prefix = ();
type SubPrefix = ();
type NoPrefix = ();

fn key(&self) -> Vec<&[u8]> {
self.wrapped.key()
Expand Down
Loading