Skip to content

Commit 9a04366

Browse files
committed
dirty
1 parent 29a3214 commit 9a04366

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

docs-test-gen/templates/storey-container-impl.tpl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
)]
88
use cosmwasm_schema::cw_serde;
99
use cosmwasm_std::*;
10-
use storey::containers::{NonTerminal, Storable};
11-
use storey::storage::StorageBranch;
10+
use storey::containers::{IterableStorable, NonTerminal, Storable};
11+
use storey::storage::{IntoStorage, StorageBranch};
1212

1313
mod users {
1414
use super::*;
@@ -75,7 +75,6 @@ struct MyMap<V> {
7575
impl<V> MyMap<V>
7676
where
7777
V: Storable,
78-
<V as Storable>::KeyDecodeError: std::fmt::Display,
7978
{
8079
pub const fn new(prefix: u8) -> Self {
8180
Self {
@@ -84,7 +83,11 @@ where
8483
}
8584
}
8685

87-
pub fn access<S>(&self, storage: S) -> MyMapAccess<V, StorageBranch<S>> {
86+
pub fn access<F, S>(&self, storage: F) -> MyMapAccess<V, StorageBranch<S>>
87+
where
88+
(F,): IntoStorage<S>,
89+
{
90+
let storage = (storage,).into_storage();
8891
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
8992
}
9093
}
@@ -97,21 +100,27 @@ pub struct MyMapAccess<V, S> {
97100
impl<V> Storable for MyMap<V>
98101
where
99102
V: Storable,
100-
<V as Storable>::KeyDecodeError: std::fmt::Display,
101103
{
102104
type Kind = NonTerminal;
103105
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;
108106
109107
fn access_impl<S>(storage: S) -> MyMapAccess<V, S> {
110108
MyMapAccess {
111109
storage,
112110
phantom: std::marker::PhantomData,
113111
}
114112
}
113+
}
114+
115+
impl<V> IterableStorable for MyMap<V>
116+
where
117+
V: IterableStorable,
118+
<V as IterableStorable>::KeyDecodeError: std::fmt::Display,
119+
{
120+
type Key = (u32, V::Key);
121+
type KeyDecodeError = ();
122+
type Value = V::Value;
123+
type ValueDecodeError = V::ValueDecodeError;
115124
116125
fn decode_key(key: &[u8]) -> Result<Self::Key, ()> {
117126
todo!()

src/pages/storey/container-impl.mdx

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ The constructor is simple - it just initializes the fields.
4949

5050
The next step is to set up an accessor. Hold on tight! This will be a ride.
5151

52-
```rust template="storage" {1, 9-11, 20-22, 25-56}
53-
use storey::containers::{NonTerminal, Storable};
52+
```rust template="storage" {1-2, 10-11, 20-26, 29-66}
53+
use storey::containers::{IterableStorable, NonTerminal, Storable};
54+
use storey::storage::{IntoStorage, StorageBranch};
5455

5556
struct MyMap<V> {
5657
prefix: u8,
@@ -60,7 +61,6 @@ struct MyMap<V> {
6061
impl<V> MyMap<V>
6162
where
6263
V: Storable,
63-
<V as Storable>::KeyDecodeError: std::fmt::Display,
6464
{
6565
pub const fn new(prefix: u8) -> Self {
6666
Self {
@@ -69,7 +69,11 @@ where
6969
}
7070
}
7171

72-
pub fn access<S>(&self, storage: S) -> MyMapAccess<V, StorageBranch<S>> {
72+
pub fn access<F, S>(&self, storage: F) -> MyMapAccess<V, StorageBranch<S>>
73+
where
74+
(F,): IntoStorage<S>,
75+
{
76+
let storage = (storage,).into_storage();
7377
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
7478
}
7579
}
@@ -82,21 +86,27 @@ pub struct MyMapAccess<V, S> {
8286
impl<V> Storable for MyMap<V>
8387
where
8488
V: Storable,
85-
<V as Storable>::KeyDecodeError: std::fmt::Display,
8689
{
8790
type Kind = NonTerminal;
8891
type Accessor<S> = MyMapAccess<V, S>;
89-
type Key = (u32, V::Key);
90-
type KeyDecodeError = ();
91-
type Value = V::Value;
92-
type ValueDecodeError = V::ValueDecodeError;
9392

9493
fn access_impl<S>(storage: S) -> MyMapAccess<V, S> {
9594
MyMapAccess {
9695
storage,
9796
phantom: std::marker::PhantomData,
9897
}
9998
}
99+
}
100+
101+
impl<V> IterableStorable for MyMap<V>
102+
where
103+
V: IterableStorable,
104+
<V as IterableStorable>::KeyDecodeError: std::fmt::Display,
105+
{
106+
type Key = (u32, V::Key);
107+
type KeyDecodeError = ();
108+
type Value = V::Value;
109+
type ValueDecodeError = V::ValueDecodeError;
100110

101111
fn decode_key(key: &[u8]) -> Result<Self::Key, ()> {
102112
todo!()
@@ -114,6 +124,8 @@ The `MyMapAccess` struct is our accessor. It's a facade that's used to actually
114124
the collection given a `Storage` instance - this is usually a subspace of the "root" storage
115125
backend.
116126

127+
TODO: rework this
128+
117129
The [`Storable`] trait is the main trait a container must implement. It's currently a mix of things.
118130
The associated types tell the framework: | Associated type | Details |
119131
|-------------------|-------------------------------------------------------------------------| |
@@ -138,8 +150,8 @@ container.
138150
There's one thing we're missing for this to actually by useful. We need some methods for the
139151
accessor.
140152

141-
```rust template="storage" {2, 59-78}
142-
use storey::containers::{NonTerminal, Storable};
153+
```rust template="storage" {68-83}
154+
use storey::containers::{IterableStorable, NonTerminal, Storable};
143155
use storey::storage::StorageBranch;
144156

145157
struct MyMap<V> {
@@ -150,7 +162,6 @@ struct MyMap<V> {
150162
impl<V> MyMap<V>
151163
where
152164
V: Storable,
153-
<V as Storable>::KeyDecodeError: std::fmt::Display,
154165
{
155166
pub const fn new(prefix: u8) -> Self {
156167
Self {
@@ -159,7 +170,11 @@ where
159170
}
160171
}
161172

162-
pub fn access<S>(&self, storage: S) -> MyMapAccess<V, StorageBranch<S>> {
173+
pub fn access<F, S>(&self, storage: F) -> MyMapAccess<V, StorageBranch<S>>
174+
where
175+
(F,): IntoStorage<S>,
176+
{
177+
let storage = (storage,).into_storage();
163178
Self::access_impl(StorageBranch::new(storage, vec![self.prefix]))
164179
}
165180
}
@@ -172,21 +187,27 @@ pub struct MyMapAccess<V, S> {
172187
impl<V> Storable for MyMap<V>
173188
where
174189
V: Storable,
175-
<V as Storable>::KeyDecodeError: std::fmt::Display,
176190
{
177191
type Kind = NonTerminal;
178192
type Accessor<S> = MyMapAccess<V, S>;
179-
type Key = (u32, V::Key);
180-
type KeyDecodeError = ();
181-
type Value = V::Value;
182-
type ValueDecodeError = V::ValueDecodeError;
183193

184194
fn access_impl<S>(storage: S) -> MyMapAccess<V, S> {
185195
MyMapAccess {
186196
storage,
187197
phantom: std::marker::PhantomData,
188198
}
189199
}
200+
}
201+
202+
impl<V> IterableStorable for MyMap<V>
203+
where
204+
V: IterableStorable,
205+
<V as IterableStorable>::KeyDecodeError: std::fmt::Display,
206+
{
207+
type Key = (u32, V::Key);
208+
type KeyDecodeError = ();
209+
type Value = V::Value;
210+
type ValueDecodeError = V::ValueDecodeError;
190211

191212
fn decode_key(key: &[u8]) -> Result<Self::Key, ()> {
192213
todo!()
@@ -223,15 +244,13 @@ Time to see this in action.
223244

224245
```rust template="storey-container-impl"
225246
use cw_storey::containers::{Item};
226-
use cw_storey::CwStorage;
227247

228248
use storey::containers::IterableAccessor as _;
229249

230250
const MAP_IX: u8 = 0;
231251

232252
let my_map: MyMap<Item<u32>> = MyMap::new(MAP_IX);
233-
let mut cw_storage = CwStorage(&mut storage);
234-
let mut access = my_map.access(&mut cw_storage);
253+
let mut access = my_map.access(&mut storage);
235254

236255
access.entry_mut(1).set(&100).unwrap();
237256
access.entry_mut(2).set(&200).unwrap();

0 commit comments

Comments
 (0)