Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 850575c

Browse files
committed
(draft) storey: container impl docs
1 parent d1c2fa7 commit 850575c

File tree

3 files changed

+426
-0
lines changed

3 files changed

+426
-0
lines changed

docs-test-gen/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
1212
"ibc-channel" => include_str!("../templates/ibc-channel.tpl"),
1313
"ibc-packet" => include_str!("../templates/ibc-packet.tpl"),
1414
"storage" => include_str!("../templates/storage.tpl"),
15+
"storey-container-impl" => include_str!("../templates/storey-container-impl.tpl"),
1516
"sylvia-storey-contract" => include_str!("../templates/sylvia/storey_contract.tpl"),
1617
"sylvia-cw-storage-contract" => include_str!("../templates/sylvia/cw_storage_contract.tpl"),
1718
"sylvia-empty" => include_str!("../templates/sylvia/empty.tpl"),
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#![allow(
2+
unexpected_cfgs,
3+
dead_code,
4+
unused_variables,
5+
unused_imports,
6+
clippy::new_without_default
7+
)]
8+
use cosmwasm_schema::cw_serde;
9+
use cosmwasm_std::*;
10+
use storey::containers::{NonTerminal, Storable};
11+
use storey::storage::StorageBranch;
12+
13+
mod users {
14+
use super::*;
15+
16+
use cw_storage_plus::{index_list, IndexedMap, MultiIndex, UniqueIndex};
17+
18+
pub type Handle = String;
19+
20+
#[cw_serde]
21+
pub struct User {
22+
pub handle: String,
23+
pub country: String,
24+
}
25+
26+
pub struct ExampleUsers {
27+
pub alice: User,
28+
pub bob: User,
29+
}
30+
31+
pub fn example_users() -> ExampleUsers {
32+
ExampleUsers {
33+
alice: User {
34+
handle: "alice".to_string(),
35+
country: "Wonderland".to_string(),
36+
},
37+
bob: User {
38+
handle: "bob".to_string(),
39+
country: "USA".to_string(),
40+
},
41+
}
42+
}
43+
44+
#[index_list(User)]
45+
pub struct UserIndexes<'a> {
46+
pub handle_ix: UniqueIndex<'a, Handle, User, Addr>,
47+
pub country_ix: MultiIndex<'a, String, User, Addr>,
48+
}
49+
50+
pub fn user_indexes() -> UserIndexes<'static> {
51+
user_indexes_custom("u", "uh", "uc")
52+
}
53+
54+
pub fn user_indexes_custom(
55+
ns: &'static str,
56+
handle_prefix: &'static str,
57+
country_prefix: &'static str,
58+
) -> UserIndexes<'static> {
59+
UserIndexes {
60+
handle_ix: UniqueIndex::new(|user| user.handle.clone(), handle_prefix),
61+
country_ix: MultiIndex::new(|_pk, user| user.country.clone(), ns, country_prefix),
62+
}
63+
}
64+
}
65+
66+
fn advance_height(env: &mut Env, blocks: u64) {
67+
env.block.height += blocks;
68+
}
69+
70+
struct MyMap<V> {
71+
prefix: u8,
72+
phantom: std::marker::PhantomData<V>,
73+
}
74+
75+
impl<V> MyMap<V>
76+
where
77+
V: Storable,
78+
<V as Storable>::KeyDecodeError: std::fmt::Display,
79+
{
80+
pub const fn new(prefix: u8) -> Self {
81+
Self {
82+
prefix,
83+
phantom: std::marker::PhantomData,
84+
}
85+
}
86+
87+
pub fn access<S>(&self, storage: S) -> MyMapAccess<V, StorageBranch<S>> {
88+
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
89+
}
90+
}
91+
92+
pub struct MyMapAccess<V, S> {
93+
storage: S,
94+
phantom: std::marker::PhantomData<V>,
95+
}
96+
97+
impl<V> Storable for MyMap<V>
98+
where
99+
V: Storable,
100+
<V as Storable>::KeyDecodeError: std::fmt::Display,
101+
{
102+
type Kind = NonTerminal;
103+
type Accessor<S> = MyMapAccess<V, S>;
104+
type Key = (u32, V::Key);
105+
type KeyDecodeError = ();
106+
type Value = V::Value;
107+
type ValueDecodeError = V::ValueDecodeError;
108+
109+
fn access_impl<S>(storage: S) -> MyMapAccess<V, S> {
110+
MyMapAccess {
111+
storage,
112+
phantom: std::marker::PhantomData,
113+
}
114+
}
115+
116+
fn decode_key(key: &[u8]) -> Result<Self::Key, ()> {
117+
todo!()
118+
}
119+
120+
fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError> {
121+
V::decode_value(value)
122+
}
123+
}
124+
125+
impl<V, S> MyMapAccess<V, S>
126+
where
127+
V: Storable,
128+
{
129+
pub fn entry(&self, key: u32) -> V::Accessor<StorageBranch<&S>> {
130+
let key = key.to_be_bytes().to_vec();
131+
132+
V::access_impl(StorageBranch::new(&self.storage, key))
133+
}
134+
135+
pub fn entry_mut(&mut self, key: u32) -> V::Accessor<StorageBranch<&mut S>> {
136+
let key = key.to_be_bytes().to_vec();
137+
138+
V::access_impl(StorageBranch::new(&mut self.storage, key))
139+
}
140+
}
141+
142+
#[test]
143+
fn doctest() {
144+
#[allow(unused_variables, unused_mut)]
145+
let mut storage = cosmwasm_std::testing::MockStorage::new();
146+
#[allow(unused_mut)]
147+
let mut env = cosmwasm_std::testing::mock_env();
148+
149+
let users = cw_storage_plus::IndexedMap::<Addr, _, _>::new(
150+
"uu",
151+
users::user_indexes_custom("uu", "uuh", "uuc"),
152+
);
153+
154+
let users_data = [
155+
(
156+
Addr::unchecked("aaa"),
157+
users::User {
158+
handle: "alice".to_string(),
159+
country: "Wonderland".to_string(),
160+
},
161+
),
162+
(
163+
Addr::unchecked("bbb"),
164+
users::User {
165+
handle: "bob".to_string(),
166+
country: "USA".to_string(),
167+
},
168+
),
169+
(
170+
Addr::unchecked("ccc"),
171+
users::User {
172+
handle: "carol".to_string(),
173+
country: "UK".to_string(),
174+
},
175+
),
176+
(
177+
Addr::unchecked("ddd"),
178+
users::User {
179+
handle: "dave".to_string(),
180+
country: "USA".to_string(),
181+
},
182+
),
183+
];
184+
185+
for (addr, user) in users_data {
186+
users.save(&mut storage, addr, &user).unwrap();
187+
}
188+
189+
#[rustfmt::skip]
190+
{{code}}
191+
}

0 commit comments

Comments
 (0)