@@ -19,7 +19,9 @@ struct A<U, V> {
19
19
}
20
20
```
21
21
22
- ``` ignore
22
+ ``` rust
23
+ use borsh :: BorshSerialize ;
24
+
23
25
/// impl<U, V> borsh::ser::BorshSerialize for A<U, V>
24
26
/// where
25
27
/// U: borsh::ser::BorshSerialize,
@@ -60,7 +62,8 @@ Attribute is optional.
60
62
61
63
Examples of usage:
62
64
63
- ` ` ` ignore
65
+ (example is not compiled, as there' s usually no `reexporter` crate during doc build)
66
+ ```rust,compile_fail
64
67
use reexporter::borsh::BorshSerialize;
65
68
66
69
// specifying the attribute removes need for a direct import of `borsh` into `[dependencies]`
@@ -73,7 +76,8 @@ struct B {
73
76
}
74
77
```
75
78
76
- ` ` ` ignore
79
+ (example is not compiled, as there' s usually no ` reexporter` crate during doc build)
80
+ ` ` ` rust,compile_fail
77
81
use reexporter::borsh::{self, BorshSerialize};
78
82
79
83
// specifying the attribute removes need for a direct import of ` borsh` into ` [dependencies]`
@@ -93,28 +97,33 @@ This attribute is only applicable to enums.
93
97
You must specify ` use_discriminant` for all enums with explicit discriminants in your project.
94
98
95
99
This is equivalent of borsh version 0.10.3 (explicit discriminant is ignored and this enum is equivalent to ` A` without explicit discriminant):
96
- ` ` ` ignore
100
+
101
+ ` ` ` rust
102
+ # use borsh::BorshSerialize;
103
+ #
97
104
# [derive(BorshSerialize)]
98
105
# [borsh(use_discriminant = false)]
99
106
enum A {
100
- A
107
+ A,
101
108
B = 10,
102
109
}
103
110
` ` `
104
111
105
112
To have explicit discriminant value serialized as is, you must specify ` borsh(use_discriminant=true)` for enum.
106
- ` ` ` ignore
113
+ ` ` ` rust
114
+ # use borsh::BorshSerialize;
115
+ #
107
116
# [derive(BorshSerialize)]
108
117
# [borsh(use_discriminant = true)]
109
118
enum B {
110
- A
119
+ A,
111
120
B = 10,
112
121
}
113
122
` ` `
114
123
115
124
# ##### borsh, expressions, evaluating to `isize`, as discriminant
116
125
This case is not supported:
117
- ` ` ` ignore
126
+ ` ` ` rust,compile_fail
118
127
const fn discrim () -> isize {
119
128
0x14
120
129
}
@@ -133,7 +142,7 @@ enum X {
133
142
134
143
# ##### borsh explicit discriminant does not support literal values outside of u8 range
135
144
This is not supported:
136
- ` ` ` ignore
145
+ ` ` ` rust,compile_fail
137
146
# [derive(BorshSerialize)]
138
147
# [borsh(use_discriminant = true)]
139
148
enum X {
@@ -152,7 +161,9 @@ enum X {
152
161
153
162
` # [borsh(skip)]` makes derive skip adding any type parameters, present in the field, to parameters bound by ` borsh::ser::BorshSerialize` .
154
163
155
- ` ` ` ignore
164
+ ` ` ` rust
165
+ # use borsh::BorshSerialize;
166
+ #
156
167
# [derive(BorshSerialize)]
157
168
struct A {
158
169
x: u64,
@@ -174,9 +185,15 @@ Attribute adds possibility to override bounds for `BorshSerialize` in order to e
174
185
1. removal of bounds on type parameters from struct/enum definition itself and moving them to the trait' s implementation block.
175
186
2. fixing complex cases, when derive hasn' t figured out the right bounds on type parameters automatically.
176
187
177
- ```ignore
188
+ ```rust
189
+ # use borsh::BorshSerialize;
190
+ # #[cfg(feature = "hashbrown")]
191
+ # use hashbrown::HashMap;
192
+ # #[cfg(feature = "std")]
193
+ # use std::collections::HashMap;
178
194
/// additional bound `T: Ord` (required by `HashMap`) is injected into
179
195
/// derived trait implementation via attribute to avoid adding the bounds on the struct itself
196
+ # #[cfg(hash_collections)]
180
197
#[derive(BorshSerialize)]
181
198
struct A<T, U> {
182
199
a: String,
@@ -188,7 +205,12 @@ struct A<T, U> {
188
205
```
189
206
190
207
191
- ```ignore
208
+ ```rust
209
+ # use borsh::BorshSerialize;
210
+ trait TraitName {
211
+ type Associated;
212
+ fn method(&self);
213
+ }
192
214
/// derive here figures the bound erroneously as `T: borsh::ser::BorshSerialize`
193
215
#[derive(BorshSerialize)]
194
216
struct A<T, V>
@@ -221,12 +243,22 @@ It may be used when `BorshSerialize` cannot be implemented for field's type, if
221
243
222
244
It may be used to override the implementation of serialization for some other reason.
223
245
224
- ` ` ` ignore
246
+ ` ` ` rust
247
+ use borsh::BorshSerialize;
225
248
use indexmap::IndexMap;
226
249
250
+ /// this a stub module, representing a 3rd party crate ` indexmap`
251
+ mod indexmap {
252
+ /// this a stub struct, representing a 3rd party ` indexmap::IndexMap`
253
+ /// or some local type we want to override trait implementation for
254
+ pub struct IndexMap< K, V> {
255
+ pub(crate) tuples: Vec<( K, V) > ,
256
+ }
257
+
258
+ }
259
+
227
260
mod index_map_impl {
228
- use super::IndexMap;
229
- use core::hash::Hash;
261
+ use super::indexmap::IndexMap;
230
262
231
263
pub fn serialize_index_map<
232
264
K: borsh::ser::BorshSerialize,
@@ -236,7 +268,9 @@ mod index_map_impl {
236
268
obj: & IndexMap< K, V> ,
237
269
writer: & mut W,
238
270
) -> ::core::result::Result< (), borsh::io::Error> {
239
- let key_value_tuples = obj.iter().collect::<Vec<_>> ();
271
+ // the line of implementation for type from real ` indexmap` crate
272
+ // let key_value_tuples = obj.iter().collect::<Vec<_>> ();
273
+ let key_value_tuples = obj.tuples.iter().collect::<Vec<_>> ();
240
274
borsh::BorshSerialize::serialize(& key_value_tuples, writer)? ;
241
275
Ok( ())
242
276
}
@@ -250,6 +284,9 @@ struct B<K, V> {
250
284
x: IndexMap< K, V> ,
251
285
y: String,
252
286
}
287
+
288
+ # fn main() {
289
+ # }
253
290
` ` `
254
291
255
292
# ##### usage (comprehensive example)
0 commit comments