-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_type.rs
More file actions
310 lines (250 loc) · 8.44 KB
/
custom_type.rs
File metadata and controls
310 lines (250 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// SPDX-License-Identifier: Apache-2.0
//! Custom type implementation example
//!
//! This example demonstrates how to implement multitrait traits for your
//! custom types, including:
//! - Implementing EncodeInto for custom types
//! - Implementing TryDecodeFrom for custom types
//! - Implementing Null and TryNull for custom types
//! - Creating composable encoding/decoding for complex structures
use multitrait::{EncodeInto, EncodeIntoBuffer, Null, TryDecodeFrom, TryNull};
fn main() {
println!("=== Multitrait Custom Type Example ===\n");
// Example 1: Simple newtype wrapper
simple_newtype();
// Example 2: Struct with multiple fields
multi_field_struct();
// Example 3: Implementing Null trait
null_trait_example();
// Example 4: Implementing TryNull trait
try_null_trait_example();
// Example 5: Complex nested structures
nested_structures();
}
/// Example 1: Simple newtype wrapper
fn simple_newtype() {
println!("1. Simple Newtype Wrapper");
println!("-------------------------");
// Define a newtype
#[derive(Debug, PartialEq)]
struct UserId(u64);
// Implement EncodeInto
impl EncodeInto for UserId {
fn encode_into(&self) -> Vec<u8> {
self.0.encode_into()
}
}
// Implement TryDecodeFrom
impl<'a> TryDecodeFrom<'a> for UserId {
type Error = multitrait::Error;
fn try_decode_from(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
let (id, remaining) = u64::try_decode_from(bytes)?;
Ok((UserId(id), remaining))
}
}
// Use the custom type
let user_id = UserId(12345);
println!("Original UserId: {:?}", user_id);
let encoded = user_id.encode_into();
println!("Encoded: {:?}", encoded);
let (decoded, _) = UserId::try_decode_from(&encoded).unwrap();
println!("Decoded: {:?}", decoded);
assert_eq!(user_id, decoded);
println!();
}
/// Example 2: Struct with multiple fields
fn multi_field_struct() {
println!("2. Struct with Multiple Fields");
println!("-------------------------------");
// Define a struct with multiple fields
#[derive(Debug, PartialEq)]
struct Person {
id: u32,
age: u8,
score: u16,
}
// Implement EncodeInto - encode fields sequentially
impl EncodeInto for Person {
fn encode_into(&self) -> Vec<u8> {
let mut buffer = Vec::new();
self.id.encode_into_buffer(&mut buffer);
self.age.encode_into_buffer(&mut buffer);
self.score.encode_into_buffer(&mut buffer);
buffer
}
}
// Implement TryDecodeFrom - decode fields sequentially
impl<'a> TryDecodeFrom<'a> for Person {
type Error = multitrait::Error;
fn try_decode_from(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
let (id, remaining) = u32::try_decode_from(bytes)?;
let (age, remaining) = u8::try_decode_from(remaining)?;
let (score, remaining) = u16::try_decode_from(remaining)?;
Ok((Person { id, age, score }, remaining))
}
}
// Use the custom type
let person = Person {
id: 1001,
age: 25,
score: 9500,
};
println!("Original Person: {:?}", person);
let encoded = person.encode_into();
println!("Encoded ({} bytes): {:?}", encoded.len(), encoded);
let (decoded, remaining) = Person::try_decode_from(&encoded).unwrap();
println!("Decoded: {:?}", decoded);
println!("Remaining bytes: {:?}", remaining);
assert_eq!(person, decoded);
println!();
}
/// Example 3: Implementing Null trait
fn null_trait_example() {
println!("3. Implementing Null Trait");
println!("--------------------------");
// Define a type that can have a null value
#[derive(Debug, PartialEq)]
struct SessionId(u64);
impl Null for SessionId {
fn null() -> Self {
SessionId(0)
}
fn is_null(&self) -> bool {
self.0 == 0
}
}
// Create null and non-null values
let null_session = SessionId::null();
let valid_session = SessionId(98765);
println!(
"Null session: {:?}, is_null: {}",
null_session,
null_session.is_null()
);
println!(
"Valid session: {:?}, is_null: {}",
valid_session,
valid_session.is_null()
);
assert!(null_session.is_null());
assert!(!valid_session.is_null());
println!();
}
/// Example 4: Implementing TryNull trait
fn try_null_trait_example() {
println!("4. Implementing TryNull Trait");
println!("------------------------------");
// Define a type with validated null creation
#[derive(Debug, PartialEq)]
struct ValidatedToken(String);
impl TryNull for ValidatedToken {
type Error = &'static str;
fn try_null() -> Result<Self, Self::Error> {
// Perform validation even for null value
Ok(ValidatedToken(String::from("NULL_TOKEN")))
}
fn is_null(&self) -> bool {
self.0 == "NULL_TOKEN"
}
}
// Create null token
match ValidatedToken::try_null() {
Ok(token) => {
println!("Created null token: {:?}", token);
println!("Is null: {}", token.is_null());
assert!(token.is_null());
}
Err(e) => {
println!("Failed to create null token: {}", e);
}
}
// Non-null token
let valid_token = ValidatedToken(String::from("abc123"));
println!(
"Valid token: {:?}, is_null: {}",
valid_token,
valid_token.is_null()
);
assert!(!valid_token.is_null());
println!();
}
/// Example 5: Complex nested structures
fn nested_structures() {
println!("5. Complex Nested Structures");
println!("-----------------------------");
// Define nested structures
#[derive(Debug, PartialEq)]
struct Metadata {
version: u8,
flags: u16,
}
#[derive(Debug, PartialEq)]
struct Message {
metadata: Metadata,
sender_id: u64,
message_type: u8,
}
// Implement EncodeInto for Metadata
impl EncodeInto for Metadata {
fn encode_into(&self) -> Vec<u8> {
let mut buffer = Vec::new();
self.version.encode_into_buffer(&mut buffer);
self.flags.encode_into_buffer(&mut buffer);
buffer
}
}
// Implement TryDecodeFrom for Metadata
impl<'a> TryDecodeFrom<'a> for Metadata {
type Error = multitrait::Error;
fn try_decode_from(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
let (version, remaining) = u8::try_decode_from(bytes)?;
let (flags, remaining) = u16::try_decode_from(remaining)?;
Ok((Metadata { version, flags }, remaining))
}
}
// Implement EncodeInto for Message
impl EncodeInto for Message {
fn encode_into(&self) -> Vec<u8> {
let mut buffer = Vec::new();
// Encode nested struct first
buffer.extend_from_slice(&self.metadata.encode_into());
self.sender_id.encode_into_buffer(&mut buffer);
self.message_type.encode_into_buffer(&mut buffer);
buffer
}
}
// Implement TryDecodeFrom for Message
impl<'a> TryDecodeFrom<'a> for Message {
type Error = multitrait::Error;
fn try_decode_from(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
let (metadata, remaining) = Metadata::try_decode_from(bytes)?;
let (sender_id, remaining) = u64::try_decode_from(remaining)?;
let (message_type, remaining) = u8::try_decode_from(remaining)?;
Ok((
Message {
metadata,
sender_id,
message_type,
},
remaining,
))
}
}
// Use the nested structures
let message = Message {
metadata: Metadata {
version: 1,
flags: 0x0F00,
},
sender_id: 999888777,
message_type: 42,
};
println!("Original message: {:#?}", message);
let encoded = message.encode_into();
println!("Encoded ({} bytes): {:?}", encoded.len(), encoded);
let (decoded, remaining) = Message::try_decode_from(&encoded).unwrap();
println!("Decoded message: {:#?}", decoded);
println!("Remaining bytes: {:?}", remaining);
assert_eq!(message, decoded);
println!();
}