@@ -49,8 +49,9 @@ The constructor is simple - it just initializes the fields.
4949
5050The 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
5556struct MyMap <V > {
5657 prefix : u8 ,
@@ -60,7 +61,6 @@ struct MyMap<V> {
6061impl <V > MyMap <V >
6162where
6263 V : Storable ,
63- <V as Storable >:: KeyDecodeError : std :: fmt :: Display ,
6464{
6565 pub const fn new (prefix : u8 ) -> Self {
6666 Self {
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> {
8286impl <V > Storable for MyMap <V >
8387where
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
114124the collection given a ` Storage ` instance - this is usually a subspace of the "root" storage
115125backend.
116126
127+ TODO: rework this
128+
117129The [ ` Storable ` ] trait is the main trait a container must implement. It's currently a mix of things.
118130The associated types tell the framework: | Associated type | Details |
119131| -------------------| -------------------------------------------------------------------------| |
@@ -138,8 +150,8 @@ container.
138150There's one thing we're missing for this to actually by useful. We need some methods for the
139151accessor.
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 };
143155use storey :: storage :: StorageBranch ;
144156
145157struct MyMap <V > {
@@ -150,7 +162,6 @@ struct MyMap<V> {
150162impl <V > MyMap <V >
151163where
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> {
172187impl <V > Storable for MyMap <V >
173188where
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"
225246use cw_storey :: containers :: {Item };
226- use cw_storey :: CwStorage ;
227247
228248use storey :: containers :: IterableAccessor as _;
229249
230250const MAP_IX : u8 = 0 ;
231251
232252let 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
236255access . entry_mut (1 ). set (& 100 ). unwrap ();
237256access . entry_mut (2 ). set (& 200 ). unwrap ();
0 commit comments