@@ -5,3 +5,141 @@ tags: ["storey", "containers"]
5
5
import { Callout } from " nextra/components" ;
6
6
7
7
# Column
8
+
9
+ A [ ` Column ` ] is a collection of items indexed by auto-incrementing ` u32 ` keys. This might sound
10
+ similar to a relational database table; in fact, it is a good analogy.
11
+
12
+ A [ ` Column ` ] makes it efficient to push items to it and access the last element.
13
+
14
+ <Callout >
15
+ It should be noted that in a future version indexing will start at ` 1 ` , not ` 0 ` .
16
+
17
+ This change will also break existing contracts that rely on the current ` Column ` implementation!
18
+
19
+ </Callout >
20
+
21
+ ## Examples
22
+
23
+ ### Storing a message log
24
+
25
+ Let's say you want to store a log of messages. You can do this with a ` Column<String> ` .
26
+
27
+ ``` rust template="storage" showLineNumbers {6,8,10,12}
28
+ use cw_storey :: containers :: Column ;
29
+ use cw_storey :: CwStorage ;
30
+
31
+ const MESSAGES_IX : u8 = 1 ;
32
+
33
+ let messages : Column <String > = Column :: new (MESSAGES_IX );
34
+ let mut cw_storage = CwStorage (& mut storage );
35
+ let mut access = messages . access (& mut cw_storage );
36
+
37
+ access . push (& " Hello, world!" . to_string ()). unwrap ();
38
+
39
+ assert_eq! (access . get (0 ). unwrap (), Some (" Hello, world!" . to_string ()));
40
+ ```
41
+
42
+ - _ line 6:_ Here we construct the ` Column ` facade. The constructor takes a key, which is the prefix
43
+ of the keys in the underlying storage backend.
44
+ - _ line 8:_ The [ ` access ` ] method returns a [ ` ColumnAccess ` ] entity, which allows manipulating the
45
+ column.
46
+ - _ line 10:_ Here we push a message to the column.
47
+ - _ line 12:_ We check that the message is stored correctly.
48
+
49
+ #### Iterating over the messages
50
+
51
+ As with [ ` Map ` ] , we can iterate over all messages.
52
+
53
+ ``` rust template="storage" showLineNumbers {4, 17}
54
+ use cw_storey :: containers :: {Column , Item };
55
+ use cw_storey :: CwStorage ;
56
+
57
+ use storey :: containers :: IterableAccessor as _;
58
+
59
+ const MESSAGES_IX : u8 = 1 ;
60
+
61
+ let messages : Column <String > = Column :: new (MESSAGES_IX );
62
+ let mut cw_storage = CwStorage (& mut storage );
63
+ let mut access = messages . access (& mut cw_storage );
64
+
65
+ // populate the column
66
+ access . push (& " Hello, world!" . to_string ()). unwrap ();
67
+ access . push (& " My name is Bob." . to_string ()). unwrap ();
68
+ access . push (& " Hell is empty and all the devils are here." . to_string ()). unwrap ();
69
+
70
+ let messages : Vec <_ > = access . pairs (). collect :: <Result <_ , _ >>(). unwrap ();
71
+
72
+ assert_eq! (messages , vec! [
73
+ (0 , " Hello, world!" . to_string ()),
74
+ (1 , " My name is Bob." . to_string ()),
75
+ (2 , " Hell is empty and all the devils are here." . to_string ()),
76
+ ]);
77
+ ```
78
+
79
+ - _ line 4:_ We import the ` IterableAccessor ` trait to use iteration methods.
80
+ - _ line 17:_ The ` pairs ` method produces an iterator over tuples of ` (index, value) ` . In this case,
81
+ we collect the iterator into a ` Vec ` for ease of making our assertion later.
82
+
83
+ Similarly to [ ` Map ` ] , you can use the [ ` keys ` ] , [ ` values ` ] , and [ ` pairs ` ] methods to iterate over
84
+ the items.
85
+
86
+ #### Bounded iteration
87
+
88
+ If you want to perform bounded iteration, it is possible. This time you need the
89
+ [ ` BoundedIterableAccessor ` ] trait, and the relevant methods are [ ` bounded_pairs ` ] , [ ` bounded_keys ` ] ,
90
+ and [ ` bounded_values ` ] .
91
+
92
+ The following example is the same as the previous one except for the bounds found in line 17, and
93
+ the limited results.
94
+
95
+ <Callout >
96
+ Currently, the lower bound is inclusive, and the upper bound is exclusive.
97
+
98
+ This will change in a future version, where you'll be able to choose between inclusive and exclusive
99
+ bounds as suits you.
100
+
101
+ </Callout >
102
+
103
+ ``` rust template="storage" showLineNumbers {4, 17}
104
+ use cw_storey :: containers :: {Column , Item };
105
+ use cw_storey :: CwStorage ;
106
+
107
+ use storey :: containers :: BoundedIterableAccessor as _;
108
+
109
+ const MESSAGES_IX : u8 = 1 ;
110
+
111
+ let messages : Column <String > = Column :: new (MESSAGES_IX );
112
+ let mut cw_storage = CwStorage (& mut storage );
113
+ let mut access = messages . access (& mut cw_storage );
114
+
115
+ // populate the column
116
+ access . push (& " Hello, world!" . to_string ()). unwrap ();
117
+ access . push (& " My name is Bob." . to_string ()). unwrap ();
118
+ access . push (& " Hell is empty and all the devils are here." . to_string ()). unwrap ();
119
+
120
+ let messages : Vec <_ > = access . bounded_pairs (Some (0 ), Some (2 )). collect :: <Result <_ , _ >>(). unwrap ();
121
+
122
+ assert_eq! (messages , vec! [
123
+ (0 , " Hello, world!" . to_string ()),
124
+ (1 , " My name is Bob." . to_string ()),
125
+ ]);
126
+ ```
127
+
128
+ [ `Column` ] : https://docs.rs/storey/latest/storey/containers/struct.Column.html
129
+ [ `access` ] : https://docs.rs/storey/latest/storey/containers/struct.Column.html#method.access
130
+ [ `ColumnAccess` ] : https://docs.rs/storey/latest/storey/containers/struct.ColumnAccess.html
131
+ [ `keys` ] : https://docs.rs/storey/latest/storey/containers/struct.ColumnAccess.html#method.keys
132
+ [ `values` ] : https://docs.rs/storey/latest/storey/containers/struct.ColumnAccess.html#method.values
133
+ [ `pairs` ] : https://docs.rs/storey/latest/storey/containers/struct.ColumnAccess.html#method.pairs
134
+ [ `Map` ] : https://docs.rs/storey/latest/storey/containers/map/struct.Map.html
135
+ [ `MapAccess` ] : https://docs.rs/storey/latest/storey/containers/map/struct.MapAccess.html
136
+ [ `ItemAccess` ] : https://docs.rs/storey/latest/storey/containers/struct.ItemAccess.html
137
+ [ `IterableAccessor` ] : https://docs.rs/storey/latest/storey/containers/trait.IterableAccessor.html
138
+ [ ` BoundedIterableAccessor ` ] :
139
+ https://docs.rs/storey/latest/storey/containers/trait.BoundedIterableAccessor.html
140
+ [ ` bounded_pairs ` ] :
141
+ https://docs.rs/storey/latest/storey/containers/trait.BoundedIterableAccessor.html#method.bounded_pairs
142
+ [ ` bounded_keys ` ] :
143
+ https://docs.rs/storey/latest/storey/containers/trait.BoundedIterableAccessor.html#method.bounded_keys
144
+ [ ` bounded_values ` ] :
145
+ https://docs.rs/storey/latest/storey/containers/trait.BoundedIterableAccessor.html#method.bounded_values
0 commit comments