Skip to content

Commit

Permalink
feat: use single-byte keys for top-level containers
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed May 7, 2024
1 parent fe76dd9 commit f0ea983
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/cw-storey/tests/smoke_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use cosmwasm_std::Storage as _;
fn smoke_test() {
let mut storage = CwStorage(cosmwasm_std::testing::MockStorage::new());

let item1 = Item::<u64>::new(&[0]);
let item1 = Item::<u64>::new(0);

item1.access(&mut storage).set(&42).unwrap();
assert_eq!(item1.access(&storage).get().unwrap(), Some(42));

let item2 = Item::<u64>::new(&[1]);
let item2 = Item::<u64>::new(1);
assert_eq!(item2.access(&storage).get().unwrap(), None);

assert_eq!(storage.0.get(&[0]), Some(vec![42]));
Expand Down
10 changes: 5 additions & 5 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const META_LEN: &[u8] = &[1];
/// assert_eq!(access.get(2).unwrap(), None);
/// ```
pub struct Column<T, E> {
prefix: &'static [u8],
prefix: u8,
phantom: PhantomData<(T, E)>,
}

Expand All @@ -50,7 +50,7 @@ where
/// with other keys in the storage.
///
/// The key provided here is used as a prefix for all keys the column itself might generate.
pub const fn new(prefix: &'static [u8]) -> Self {
pub const fn new(prefix: u8) -> Self {
Self {
prefix,
phantom: PhantomData,
Expand All @@ -76,7 +76,7 @@ where
/// let mut access = column.access(&mut storage);
/// ```
pub fn access<S>(&self, storage: S) -> ColumnAccess<E, T, StorageBranch<S>> {
Self::access_impl(StorageBranch::new(storage, self.prefix.to_vec()))
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
}
}

Expand Down Expand Up @@ -396,7 +396,7 @@ mod tests {
fn basic() {
let mut storage = TestStorage::new();

let column = Column::<u64, TestEncoding>::new(&[0]);
let column = Column::<u64, TestEncoding>::new(0);
let mut access = column.access(&mut storage);

access.push(&1337).unwrap();
Expand All @@ -420,7 +420,7 @@ mod tests {
fn iteration() {
let mut storage = TestStorage::new();

let column = Column::<u64, TestEncoding>::new(&[0]);
let column = Column::<u64, TestEncoding>::new(0);
let mut access = column.access(&mut storage);

access.push(&1337).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions packages/storey/src/containers/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::Storable;
/// assert_eq!(item.access(&storage).get().unwrap(), Some(42));
/// ```
pub struct Item<T, E> {
prefix: &'static [u8],
key: u8,
phantom: PhantomData<(T, E)>,
}

Expand All @@ -36,9 +36,9 @@ where
/// Create a new item with the given key.
///
/// It is the responsibility of the caller to ensure that the key is unique.
pub const fn new(prefix: &'static [u8]) -> Self {
pub const fn new(key: u8) -> Self {
Self {
prefix,
key,
phantom: PhantomData,
}
}
Expand All @@ -61,7 +61,7 @@ where
/// let item = Item::<u64, TestEncoding>::new(&[0]);
/// let mut access = item.access(&mut storage);
pub fn access<S>(&self, storage: S) -> ItemAccess<E, T, StorageBranch<S>> {
Self::access_impl(StorageBranch::new(storage, self.prefix.to_vec()))
Self::access_impl(StorageBranch::new(storage, vec![self.key]))
}
}

Expand Down Expand Up @@ -188,10 +188,10 @@ mod tests {
fn basic() {
let mut storage = TestStorage::new();

let item0 = Item::<u64, TestEncoding>::new(&[0]);
let item0 = Item::<u64, TestEncoding>::new(0);
item0.access(&mut storage).set(&42).unwrap();

let item1 = Item::<u64, TestEncoding>::new(&[1]);
let item1 = Item::<u64, TestEncoding>::new(1);
let access1 = item1.access(&storage);

assert_eq!(item0.access(&storage).get().unwrap(), Some(42));
Expand Down
14 changes: 7 additions & 7 deletions packages/storey/src/containers/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use super::Storable;
/// assert_eq!(access.entry("foo").entry("baz").get().unwrap(), None);
/// ```
pub struct Map<K: ?Sized, V> {
prefix: &'static [u8],
prefix: u8,
phantom: PhantomData<(*const K, V)>,
}

Expand All @@ -59,7 +59,7 @@ where
/// with other keys in the storage.
///
/// The key provided here is used as a prefix for all keys managed by the map.
pub const fn new(prefix: &'static [u8]) -> Self {
pub const fn new(prefix: u8) -> Self {
Self {
prefix,
phantom: PhantomData,
Expand All @@ -85,7 +85,7 @@ where
/// let mut access = map.access(&mut storage);
/// ```
pub fn access<S>(&self, storage: S) -> MapAccess<K, V, StorageBranch<S>> {
Self::access_impl(StorageBranch::new(storage, self.prefix.to_vec()))
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
}
}

Expand Down Expand Up @@ -313,7 +313,7 @@ mod tests {
fn map() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let map = Map::<String, Item<u64, TestEncoding>>::new(0);

map.access(&mut storage)
.entry_mut("foo")
Expand All @@ -332,7 +332,7 @@ mod tests {
fn pairs() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let map = Map::<String, Item<u64, TestEncoding>>::new(0);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
Expand All @@ -355,7 +355,7 @@ mod tests {
fn keys() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let map = Map::<String, Item<u64, TestEncoding>>::new(0);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
Expand All @@ -372,7 +372,7 @@ mod tests {
fn values() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let map = Map::<String, Item<u64, TestEncoding>>::new(0);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions packages/storey/tests/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use storey_storage::Storage as _;
fn map_of_map() {
let mut storage = TestStorage::new();

let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(&[0]);
let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(0);

map.access(&mut storage)
.entry_mut("foo")
Expand Down Expand Up @@ -42,7 +42,7 @@ fn map_of_map() {
fn map_of_column() {
let mut storage = TestStorage::new();

let map = Map::<String, Column<u64, TestEncoding>>::new(&[0]);
let map = Map::<String, Column<u64, TestEncoding>>::new(0);
let mut access = map.access(&mut storage);

access.entry_mut("foo").push(&1337).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion packages/storey/tests/iteration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mocks::encoding::TestEncoding;
fn map_of_map_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(&[0]);
let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(0);
let mut access = map.access(&mut storage);

// populate with data
Expand Down

0 comments on commit f0ea983

Please sign in to comment.