• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 14750644827

30 Apr 2025 08:52AM UTC coverage: 56.952% (+0.6%) from 56.307%
14750644827

Pull #488

github

web-flow
Merge faa4eaf46 into 22af2b63d
Pull Request #488: Use facet-serialize in facet-msgpack

288 of 397 new or added lines in 2 files covered. (72.54%)

20 existing lines in 1 file now uncovered.

7729 of 13571 relevant lines covered (56.95%)

84.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.7
/facet-msgpack/src/serialize.rs
1
use facet_core::Facet;
2
use facet_reflect::Peek;
3
use facet_serialize::{Serializer, serialize_iterative}; // Import the necessary items from facet-serialize
4
use log::trace;
5
use std::io::{self, Write};
6

7
/// Serializes any Facet type to MessagePack bytes
8
pub fn to_vec<'a, T: Facet<'a>>(value: &'a T) -> Vec<u8> {
7✔
9
    let mut buffer = Vec::new();
7✔
10
    let peek = Peek::new(value);
7✔
11
    let mut serializer = MessagePackSerializer {
7✔
12
        writer: &mut buffer,
7✔
13
    }; // Create the serializer
7✔
14
    serialize_iterative(peek, &mut serializer).unwrap(); // Use the iterative serializer
7✔
15
    buffer
7✔
16
}
7✔
17

18
// Define the MessagePackSerializer struct
19
struct MessagePackSerializer<'w, W: Write> {
20
    writer: &'w mut W,
21
}
22

23
// Implement the Serializer trait for MessagePackSerializer
24
impl<W: Write> Serializer for MessagePackSerializer<'_, W> {
25
    type Error = io::Error; // Use io::Error as the error type
26

27
    // Implement all methods required by the Serializer trait
28
    // Most implementations will simply call the existing write_* helper functions.
29

30
    fn serialize_u8(&mut self, value: u8) -> Result<(), Self::Error> {
19✔
31
        trace!("Serializing u8: {}", value);
19✔
32
        write_u8(self.writer, value)
19✔
33
    }
19✔
34

35
    fn serialize_u16(&mut self, value: u16) -> Result<(), Self::Error> {
2✔
36
        trace!("Serializing u16: {}", value);
2✔
37
        write_u16(self.writer, value)
2✔
38
    }
2✔
39

40
    fn serialize_u32(&mut self, value: u32) -> Result<(), Self::Error> {
5✔
41
        trace!("Serializing u32: {}", value);
5✔
42
        write_u32(self.writer, value)
5✔
43
    }
5✔
44

45
    fn serialize_u64(&mut self, value: u64) -> Result<(), Self::Error> {
3✔
46
        trace!("Serializing u64: {}", value);
3✔
47
        write_u64(self.writer, value)
3✔
48
    }
3✔
49

50
    // TODO: Implement serialize_u128 if needed for MessagePack, otherwise return error or panic
NEW
51
    fn serialize_u128(&mut self, _value: u128) -> Result<(), Self::Error> {
×
NEW
52
        Err(io::Error::new(
×
NEW
53
            io::ErrorKind::Other,
×
NEW
54
            "u128 is not directly supported by MessagePack",
×
NEW
55
        ))
×
NEW
56
    }
×
57

58
    // Map usize to u64 as MessagePack doesn't have a specific usize type
NEW
59
    fn serialize_usize(&mut self, value: usize) -> Result<(), Self::Error> {
×
NEW
60
        trace!("Serializing usize: {}", value);
×
NEW
61
        write_u64(self.writer, value as u64) // Assuming usize fits in u64
×
NEW
62
    }
×
63

64
    fn serialize_i8(&mut self, value: i8) -> Result<(), Self::Error> {
4✔
65
        trace!("Serializing i8: {}", value);
4✔
66
        write_i8(self.writer, value)
4✔
67
    }
4✔
68

69
    fn serialize_i16(&mut self, value: i16) -> Result<(), Self::Error> {
2✔
70
        trace!("Serializing i16: {}", value);
2✔
71
        write_i16(self.writer, value)
2✔
72
    }
2✔
73

74
    fn serialize_i32(&mut self, value: i32) -> Result<(), Self::Error> {
3✔
75
        trace!("Serializing i32: {}", value);
3✔
76
        write_i32(self.writer, value)
3✔
77
    }
3✔
78

79
    fn serialize_i64(&mut self, value: i64) -> Result<(), Self::Error> {
2✔
80
        trace!("Serializing i64: {}", value);
2✔
81
        write_i64(self.writer, value)
2✔
82
    }
2✔
83

84
    // TODO: Implement serialize_i128 if needed for MessagePack, otherwise return error or panic
NEW
85
    fn serialize_i128(&mut self, _value: i128) -> Result<(), Self::Error> {
×
NEW
86
        Err(io::Error::new(
×
NEW
87
            io::ErrorKind::Other,
×
NEW
88
            "i128 is not directly supported by MessagePack",
×
NEW
89
        ))
×
NEW
90
    }
×
91

92
    // Map isize to i64 as MessagePack doesn't have a specific isize type
NEW
93
    fn serialize_isize(&mut self, value: isize) -> Result<(), Self::Error> {
×
NEW
94
        trace!("Serializing isize: {}", value);
×
NEW
95
        write_i64(self.writer, value as i64) // Assuming isize fits in i64
×
NEW
96
    }
×
97

98
    fn serialize_f32(&mut self, value: f32) -> Result<(), Self::Error> {
1✔
99
        trace!("Serializing f32: {}", value);
1✔
100
        write_f32(self.writer, value)
1✔
101
    }
1✔
102

103
    fn serialize_f64(&mut self, value: f64) -> Result<(), Self::Error> {
1✔
104
        trace!("Serializing f64: {}", value);
1✔
105
        write_f64(self.writer, value)
1✔
106
    }
1✔
107

108
    fn serialize_bool(&mut self, value: bool) -> Result<(), Self::Error> {
3✔
109
        trace!("Serializing bool: {}", value);
3✔
110
        write_bool(self.writer, value)
3✔
111
    }
3✔
112

113
    // Characters are often serialized as strings in MessagePack
114
    fn serialize_char(&mut self, value: char) -> Result<(), Self::Error> {
1✔
115
        trace!("Serializing char: {}", value);
1✔
116
        let mut buf = [0; 4];
1✔
117
        write_str(self.writer, value.encode_utf8(&mut buf))
1✔
118
    }
1✔
119

120
    fn serialize_str(&mut self, value: &str) -> Result<(), Self::Error> {
5✔
121
        trace!("Serializing str: {}", value);
5✔
122
        write_str(self.writer, value)
5✔
123
    }
5✔
124

NEW
125
    fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
×
NEW
126
        trace!("Serializing bytes, len: {}", value.len());
×
NEW
127
        write_bin(self.writer, value)
×
NEW
128
    }
×
129

130
    fn serialize_none(&mut self) -> Result<(), Self::Error> {
2✔
131
        trace!("Serializing none");
2✔
132
        write_nil(self.writer)
2✔
133
    }
2✔
134

135
    fn serialize_unit(&mut self) -> Result<(), Self::Error> {
1✔
136
        trace!("Serializing unit");
1✔
137
        write_nil(self.writer) // Represent unit as nil
1✔
138
    }
1✔
139

140
    // Unit variants can be represented as strings or specific codes if needed.
141
    // Using string representation for now.
142
    fn serialize_unit_variant(
1✔
143
        &mut self,
1✔
144
        _variant_index: usize,
1✔
145
        variant_name: &'static str,
1✔
146
    ) -> Result<(), Self::Error> {
1✔
147
        trace!("Serializing unit variant: {}", variant_name);
1✔
148
        write_str(self.writer, variant_name)
1✔
149
    }
1✔
150

151
    fn start_object(&mut self, len: Option<usize>) -> Result<(), Self::Error> {
8✔
152
        trace!("Starting object, len: {:?}", len);
8✔
153
        if let Some(l) = len {
8✔
154
            write_map_len(self.writer, l)
8✔
155
        } else {
156
            // MessagePack doesn't have an indefinite length map marker.
157
            // This might require buffering or a different approach if the length is unknown.
158
            // For now, assume length is always known by `facet-serialize`.
NEW
159
            Err(io::Error::new(
×
NEW
160
                io::ErrorKind::Other,
×
NEW
161
                "MessagePack requires map length upfront",
×
NEW
162
            ))
×
163
        }
164
    }
8✔
165

166
    fn end_object(&mut self) -> Result<(), Self::Error> {
8✔
167
        trace!("Ending object");
8✔
168
        // No explicit end marker needed for fixed-length maps in MessagePack
169
        Ok(())
8✔
170
    }
8✔
171

172
    fn start_array(&mut self, len: Option<usize>) -> Result<(), Self::Error> {
3✔
173
        trace!("Starting array, len: {:?}", len);
3✔
174
        if let Some(l) = len {
3✔
175
            write_array_len(self.writer, l)
3✔
176
        } else {
NEW
177
            Err(io::Error::new(
×
NEW
178
                io::ErrorKind::Other,
×
NEW
179
                "MessagePack requires array length upfront",
×
NEW
180
            ))
×
181
        }
182
    }
3✔
183

184
    fn end_array(&mut self) -> Result<(), Self::Error> {
3✔
185
        trace!("Ending array");
3✔
186
        // No explicit end marker needed for fixed-length arrays in MessagePack
187
        Ok(())
3✔
188
    }
3✔
189

190
    // Maps in facet-serialize correspond to MessagePack maps
NEW
191
    fn start_map(&mut self, len: Option<usize>) -> Result<(), Self::Error> {
×
NEW
192
        trace!("Starting map, len: {:?}", len);
×
NEW
193
        if let Some(l) = len {
×
NEW
194
            write_map_len(self.writer, l)
×
195
        } else {
NEW
196
            Err(io::Error::new(
×
NEW
197
                io::ErrorKind::Other,
×
NEW
198
                "MessagePack requires map length upfront",
×
NEW
199
            ))
×
200
        }
NEW
201
    }
×
202

NEW
203
    fn end_map(&mut self) -> Result<(), Self::Error> {
×
NEW
204
        trace!("Ending map");
×
205
        // No explicit end marker needed for fixed-length maps in MessagePack
NEW
206
        Ok(())
×
NEW
207
    }
×
208

209
    // Field names are serialized as strings (keys) in MessagePack maps
210
    fn serialize_field_name(&mut self, name: &'static str) -> Result<(), Self::Error> {
43✔
211
        trace!("Serializing field name: {}", name);
43✔
212
        write_str(self.writer, name)
43✔
213
    }
43✔
214
}
215

216
fn write_nil<W: Write>(writer: &mut W) -> io::Result<()> {
3✔
217
    writer.write_all(&[0xc0])
3✔
218
}
3✔
219

220
fn write_bool<W: Write>(writer: &mut W, val: bool) -> io::Result<()> {
3✔
221
    if val {
3✔
222
        writer.write_all(&[0xc3]) // true
2✔
223
    } else {
224
        writer.write_all(&[0xc2]) // false
1✔
225
    }
226
}
3✔
227

228
fn write_f32<W: Write>(writer: &mut W, n: f32) -> io::Result<()> {
1✔
229
    writer.write_all(&[0xca])?; // float 32
1✔
230
    writer.write_all(&n.to_be_bytes())
1✔
231
}
1✔
232

233
fn write_f64<W: Write>(writer: &mut W, n: f64) -> io::Result<()> {
1✔
234
    writer.write_all(&[0xcb])?; // float 64
1✔
235
    writer.write_all(&n.to_be_bytes())
1✔
236
}
1✔
237

NEW
238
fn write_bin<W: Write>(writer: &mut W, bytes: &[u8]) -> io::Result<()> {
×
NEW
239
    let len = bytes.len();
×
NEW
240
    match len {
×
NEW
241
        0..=255 => {
×
242
            // bin 8
NEW
243
            writer.write_all(&[0xc4, len as u8])?;
×
244
        }
NEW
245
        256..=65535 => {
×
246
            // bin 16
NEW
247
            writer.write_all(&[0xc5])?;
×
NEW
248
            writer.write_all(&(len as u16).to_be_bytes())?;
×
249
        }
250
        _ => {
251
            // bin 32
NEW
252
            writer.write_all(&[0xc6])?;
×
NEW
253
            writer.write_all(&(len as u32).to_be_bytes())?;
×
254
        }
255
    }
NEW
256
    writer.write_all(bytes)
×
NEW
257
}
×
258

259
fn write_array_len<W: Write>(writer: &mut W, len: usize) -> io::Result<()> {
3✔
260
    match len {
3✔
261
        0..=15 => {
3✔
262
            // fixarray
263
            writer.write_all(&[(0x90 | len as u8)])
3✔
264
        }
NEW
265
        16..=65535 => {
×
266
            // array 16
NEW
267
            writer.write_all(&[0xdc])?;
×
NEW
268
            writer.write_all(&(len as u16).to_be_bytes())
×
269
        }
270
        _ => {
271
            // array 32
NEW
272
            writer.write_all(&[0xdd])?;
×
NEW
273
            writer.write_all(&(len as u32).to_be_bytes())
×
274
        }
275
    }
276
}
3✔
277

278
// --- Existing write_* functions from the original file ---
279
// (write_str, write_u8, write_u16, write_u32, write_u64, write_i8, write_i16, write_i32, write_i64, write_map_len)
280
// These remain largely unchanged.
281

282
fn write_str<W: Write>(writer: &mut W, s: &str) -> io::Result<()> {
50✔
283
    let bytes = s.as_bytes();
50✔
284
    let len = bytes.len();
50✔
285

50✔
286
    match len {
50✔
287
        0..=31 => {
50✔
288
            // fixstr
289
            writer.write_all(&[(0xa0 | len as u8)])?;
50✔
290
        }
NEW
291
        32..=255 => {
×
292
            // str8
NEW
293
            writer.write_all(&[0xd9, len as u8])?;
×
294
        }
NEW
295
        256..=65535 => {
×
296
            // str16
NEW
297
            writer.write_all(&[0xda])?;
×
NEW
298
            writer.write_all(&(len as u16).to_be_bytes())?;
×
299
        }
300
        _ => {
301
            // str32
NEW
302
            writer.write_all(&[0xdb])?;
×
NEW
303
            writer.write_all(&(len as u32).to_be_bytes())?;
×
304
        }
305
    }
306
    writer.write_all(bytes)
50✔
307
}
50✔
308

309
fn write_u8<W: Write>(writer: &mut W, n: u8) -> io::Result<()> {
19✔
310
    match n {
19✔
311
        0..=127 => {
19✔
312
            // positive fixint
313
            writer.write_all(&[n])
18✔
314
        }
315
        _ => {
316
            // uint8
317
            writer.write_all(&[0xcc, n])
1✔
318
        }
319
    }
320
}
19✔
321

322
fn write_u16<W: Write>(writer: &mut W, n: u16) -> io::Result<()> {
2✔
323
    match n {
2✔
324
        0..=127 => {
2✔
325
            // positive fixint
NEW
326
            writer.write_all(&[n as u8])
×
327
        }
328
        128..=255 => {
2✔
329
            // uint8
NEW
330
            writer.write_all(&[0xcc, n as u8])
×
331
        }
332
        _ => {
333
            // uint16
334
            writer.write_all(&[0xcd])?;
2✔
335
            writer.write_all(&n.to_be_bytes())
2✔
336
        }
337
    }
338
}
2✔
339

340
fn write_u32<W: Write>(writer: &mut W, n: u32) -> io::Result<()> {
6✔
341
    match n {
6✔
342
        0..=127 => {
6✔
343
            // positive fixint
344
            writer.write_all(&[n as u8])
2✔
345
        }
346
        128..=255 => {
4✔
347
            // uint8
NEW
348
            writer.write_all(&[0xcc, n as u8])
×
349
        }
350
        256..=65535 => {
4✔
351
            // uint16
352
            writer.write_all(&[0xcd])?;
2✔
353
            writer.write_all(&(n as u16).to_be_bytes())
2✔
354
        }
355
        _ => {
356
            // uint32
357
            writer.write_all(&[0xce])?;
2✔
358
            writer.write_all(&n.to_be_bytes())
2✔
359
        }
360
    }
361
}
6✔
362

363
fn write_u64<W: Write>(writer: &mut W, n: u64) -> io::Result<()> {
3✔
364
    match n {
3✔
365
        0..=127 => {
3✔
366
            // positive fixint
367
            writer.write_all(&[n as u8])
1✔
368
        }
369
        128..=255 => {
2✔
370
            // uint8
NEW
371
            writer.write_all(&[0xcc, n as u8])
×
372
        }
373
        256..=65535 => {
2✔
374
            // uint16
NEW
375
            writer.write_all(&[0xcd])?;
×
NEW
376
            writer.write_all(&(n as u16).to_be_bytes())
×
377
        }
378
        65536..=4294967295 => {
2✔
379
            // uint32
NEW
380
            writer.write_all(&[0xce])?;
×
NEW
381
            writer.write_all(&(n as u32).to_be_bytes())
×
382
        }
383
        _ => {
384
            // uint64
385
            writer.write_all(&[0xcf])?;
2✔
386
            writer.write_all(&n.to_be_bytes())
2✔
387
        }
388
    }
389
}
3✔
390

391
fn write_i8<W: Write>(writer: &mut W, n: i8) -> io::Result<()> {
4✔
392
    match n {
4✔
393
        -32..=-1 => {
3✔
394
            // negative fixint
395
            writer.write_all(&[n as u8])
3✔
396
        }
397
        -128..=-33 => {
1✔
398
            // int8
399
            writer.write_all(&[0xd0, n as u8])
1✔
400
        }
NEW
401
        0..=127 => {
×
402
            // positive fixint or uint8
NEW
403
            write_u8(writer, n as u8) // Reuse u8 logic for positive values
×
404
        }
405
    }
406
}
4✔
407

408
fn write_i16<W: Write>(writer: &mut W, n: i16) -> io::Result<()> {
2✔
409
    match n {
2✔
NEW
410
        -32..=-1 => {
×
411
            // negative fixint
NEW
412
            writer.write_all(&[n as u8])
×
413
        }
NEW
414
        -128..=-33 => {
×
415
            // int8
NEW
416
            writer.write_all(&[0xd0, n as u8])
×
417
        }
418
        -32768..=-129 => {
2✔
419
            // int16
420
            writer.write_all(&[0xd1])?;
2✔
421
            writer.write_all(&n.to_be_bytes())
2✔
422
        }
NEW
423
        0..=32767 => {
×
424
            // Use unsigned logic for positive range
NEW
425
            write_u16(writer, n as u16)
×
426
        }
427
    }
428
}
2✔
429

430
fn write_i32<W: Write>(writer: &mut W, n: i32) -> io::Result<()> {
3✔
431
    match n {
3✔
432
        -32..=-1 => {
1✔
433
            // negative fixint
NEW
434
            writer.write_all(&[n as u8])
×
435
        }
436
        -128..=-33 => {
1✔
437
            // int8
NEW
438
            writer.write_all(&[0xd0, n as u8])
×
439
        }
440
        -32768..=-129 => {
1✔
441
            // int16
NEW
442
            writer.write_all(&[0xd1])?;
×
NEW
443
            writer.write_all(&(n as i16).to_be_bytes())
×
444
        }
445
        -2147483648..=-32769 => {
3✔
446
            // int32
447
            writer.write_all(&[0xd2])?;
2✔
448
            writer.write_all(&n.to_be_bytes())
2✔
449
        }
450
        0..=2147483647 => {
1✔
451
            // Use unsigned logic for positive range
452
            write_u32(writer, n as u32)
1✔
453
        }
454
    }
455
}
3✔
456

457
fn write_i64<W: Write>(writer: &mut W, n: i64) -> io::Result<()> {
2✔
458
    match n {
2✔
NEW
459
        -32..=-1 => {
×
460
            // negative fixint
NEW
461
            writer.write_all(&[n as u8])
×
462
        }
NEW
463
        -128..=-33 => {
×
464
            // int8
NEW
465
            writer.write_all(&[0xd0, n as u8])
×
466
        }
NEW
467
        -32768..=-129 => {
×
468
            // int16
NEW
469
            writer.write_all(&[0xd1])?;
×
NEW
470
            writer.write_all(&(n as i16).to_be_bytes())
×
471
        }
NEW
472
        -2147483648..=-32769 => {
×
473
            // int32
NEW
474
            writer.write_all(&[0xd2])?;
×
NEW
475
            writer.write_all(&(n as i32).to_be_bytes())
×
476
        }
477
        i64::MIN..=-2147483649 => {
2✔
478
            // int64
479
            writer.write_all(&[0xd3])?;
2✔
480
            writer.write_all(&n.to_be_bytes())
2✔
481
        }
NEW
482
        0..=i64::MAX => {
×
483
            // Use unsigned logic for positive range
NEW
484
            write_u64(writer, n as u64)
×
485
        }
486
    }
487
}
2✔
488

489
fn write_map_len<W: Write>(writer: &mut W, len: usize) -> io::Result<()> {
8✔
490
    match len {
8✔
491
        0..=15 => {
8✔
492
            // fixmap
493
            writer.write_all(&[(0x80 | len as u8)])
7✔
494
        }
495
        16..=65535 => {
1✔
496
            // map16
497
            writer.write_all(&[0xde])?;
1✔
498
            writer.write_all(&(len as u16).to_be_bytes())
1✔
499
        }
500
        _ => {
501
            // map32
NEW
502
            writer.write_all(&[0xdf])?;
×
NEW
503
            writer.write_all(&(len as u32).to_be_bytes())
×
504
        }
505
    }
506
}
8✔
507

508
#[cfg(test)]
509
mod tests {
510
    use super::*;
511
    use facet::Facet;
512
    use serde::Serialize; // Import serde::Serialize
513

514
    // Helper function to serialize with rmp_serde
515
    fn rmp_serialize<T: Serialize>(value: &T) -> Vec<u8> {
5✔
516
        // Configure rmp_serde to serialize structs as maps
5✔
517
        let mut buf = Vec::new();
5✔
518
        let mut ser = rmp_serde::Serializer::new(&mut buf).with_struct_map();
5✔
519
        value.serialize(&mut ser).unwrap();
5✔
520
        buf
5✔
521
    }
5✔
522

NEW
523
    #[derive(Facet, Serialize, PartialEq, Debug)] // Add Serialize
×
524
    struct SimpleStruct {
525
        a: u32,
526
        b: String,
527
        c: bool,
528
    }
529

530
    #[test]
531
    fn test_simple_struct() {
1✔
532
        let value = SimpleStruct {
1✔
533
            a: 123,
1✔
534
            b: "hello".to_string(),
1✔
535
            c: true,
1✔
536
        };
1✔
537

1✔
538
        let facet_bytes = to_vec(&value);
1✔
539
        let rmp_bytes = rmp_serialize(&value);
1✔
540

1✔
541
        assert_eq!(facet_bytes, rmp_bytes);
1✔
542
    }
1✔
543

NEW
544
    #[derive(Facet, Serialize, PartialEq, Debug)] // Add Serialize
×
545
    struct NestedStruct {
546
        inner: SimpleStruct,
547
        d: Option<i8>,
548
        e: Vec<u8>,
549
    }
550

551
    #[test]
552
    fn test_nested_struct() {
1✔
553
        let value = NestedStruct {
1✔
554
            inner: SimpleStruct {
1✔
555
                a: 456,
1✔
556
                b: "world".to_string(),
1✔
557
                c: false,
1✔
558
            },
1✔
559
            d: Some(-5),
1✔
560
            e: vec![1, 2, 3, 4, 5],
1✔
561
        };
1✔
562

1✔
563
        let facet_bytes = to_vec(&value);
1✔
564
        let rmp_bytes = rmp_serialize(&value);
1✔
565

1✔
566
        assert_eq!(facet_bytes, rmp_bytes);
1✔
567
    }
1✔
568

569
    #[test]
570
    fn test_nested_struct_none() {
1✔
571
        let value = NestedStruct {
1✔
572
            inner: SimpleStruct {
1✔
573
                a: 789,
1✔
574
                b: "another".to_string(),
1✔
575
                c: true,
1✔
576
            },
1✔
577
            d: None,
1✔
578
            e: vec![],
1✔
579
        };
1✔
580

1✔
581
        let facet_bytes = to_vec(&value);
1✔
582
        let rmp_bytes = rmp_serialize(&value);
1✔
583

1✔
584
        assert_eq!(facet_bytes, rmp_bytes);
1✔
585
    }
1✔
586

NEW
587
    #[derive(Facet, Serialize, PartialEq, Debug)] // Add Serialize
×
588
    #[repr(u8)]
589
    enum TestEnum {
590
        Unit,
591
        Tuple(u32, String),
592
        Struct { name: String, value: i64 },
593
    }
594

595
    #[test]
596
    fn test_enum_unit() {
1✔
597
        let value = TestEnum::Unit;
1✔
598
        let facet_bytes = to_vec(&value);
1✔
599
        // rmp-serde serializes unit variants as just the string name
1✔
600
        let rmp_bytes = rmp_serialize(&"Unit");
1✔
601
        assert_eq!(facet_bytes, rmp_bytes);
1✔
602
    }
1✔
603

604
    #[test]
605
    fn test_various_types() {
1✔
NEW
606
        #[derive(Facet, Serialize, PartialEq, Debug)]
×
607
        struct Various {
608
            f1: f32,
609
            f2: f64,
610
            i1: i8,
611
            i2: i16,
612
            i3: i32,
613
            i4: i64,
614
            u1: u8,
615
            u2: u16,
616
            u3: u32,
617
            u4: u64,
618
            b: Vec<u8>,
619
            s: String,
620
            c: char,
621
            opt_some: Option<i32>,
622
            opt_none: Option<String>,
623
            unit: (),
624
        }
625

626
        let value = Various {
1✔
627
            f1: 1.23,
1✔
628
            f2: -4.56e7,
1✔
629
            i1: -10,
1✔
630
            i2: -1000,
1✔
631
            i3: -100000,
1✔
632
            i4: -10000000000,
1✔
633
            u1: 10,
1✔
634
            u2: 1000,
1✔
635
            u3: 100000,
1✔
636
            u4: 10000000000,
1✔
637
            b: b"binary data".to_vec(),
1✔
638
            s: "string data".to_string(),
1✔
639
            c: '✅',
1✔
640
            opt_some: Some(99),
1✔
641
            opt_none: None,
1✔
642
            unit: (),
1✔
643
        };
1✔
644

1✔
645
        let facet_bytes = to_vec(&value);
1✔
646
        let rmp_bytes = rmp_serialize(&value);
1✔
647

1✔
648
        assert_eq!(facet_bytes, rmp_bytes);
1✔
649
    }
1✔
650
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc