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

facet-rs / facet / 15755892858

19 Jun 2025 10:40AM UTC coverage: 59.611% (+0.04%) from 59.571%
15755892858

Pull #798

github

web-flow
Merge 4f7631421 into 1daf9aec7
Pull Request #798: Apply modern clippy fixes (mostly format strings)

79 of 200 new or added lines in 29 files covered. (39.5%)

1 existing line in 1 file now uncovered.

10839 of 18183 relevant lines covered (59.61%)

150.54 hits per line

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

78.99
/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> {
23✔
9
    let mut buffer = Vec::new();
23✔
10
    let peek = Peek::new(value);
23✔
11
    let mut serializer = MessagePackSerializer {
23✔
12
        writer: &mut buffer,
23✔
13
    }; // Create the serializer
23✔
14
    serialize_iterative(peek, &mut serializer).unwrap(); // Use the iterative serializer
23✔
15
    buffer
23✔
16
}
23✔
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<'shape, W: Write> Serializer<'shape> 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> {
3✔
31
        trace!("Serializing u8: {value}");
3✔
32
        write_u8(self.writer, value)
3✔
33
    }
3✔
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
51
    fn serialize_u128(&mut self, _value: u128) -> Result<(), Self::Error> {
×
52
        Err(io::Error::other(
×
53
            "u128 is not directly supported by MessagePack",
×
54
        ))
×
55
    }
×
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

161
    fn end_object(&mut self) -> Result<(), Self::Error> {
24✔
162
        trace!("Ending object");
24✔
163
        // No explicit end marker needed for fixed-length maps in MessagePack
164
        Ok(())
24✔
165
    }
24✔
166

167
    fn start_array(&mut self, len: Option<usize>) -> Result<(), Self::Error> {
2✔
168
        trace!("Starting array, len: {len:?}");
2✔
169
        if let Some(l) = len {
2✔
170
            if l == 0 {
2✔
171
                // In facet's reflection system, unit types `()` are represented as tuples with 0 elements,
172
                // which results in empty arrays being serialized. For MessagePack compatibility with
173
                // rmp_serde, we serialize empty arrays as nil to match how serde treats unit types.
174
                // This ensures consistent behavior between facet-msgpack and rmp_serde.
175
                write_nil(self.writer)
2✔
176
            } else {
177
                write_array_len(self.writer, l)
×
178
            }
179
        } else {
180
            Err(io::Error::other(
×
181
                "MessagePack requires array length upfront",
×
182
            ))
×
183
        }
184
    }
2✔
185

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

570
    #[test]
571
    fn test_nested_struct_none() {
1✔
572
        let value = NestedStruct {
1✔
573
            inner: SimpleStruct {
1✔
574
                a: 789,
1✔
575
                b: "another".to_string(),
1✔
576
                c: true,
1✔
577
            },
1✔
578
            d: None,
1✔
579
            e: vec![0], // rmp can't serialize empty bin8 correctly
1✔
580
        };
1✔
581

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

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

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

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

606
    #[test]
607
    fn test_f32() {
1✔
608
        #[derive(Facet, Serialize, PartialEq, Debug)]
609
        struct FloatStruct {
610
            value: f32,
611
        }
612

613
        let value = FloatStruct { value: 1.23 };
1✔
614
        let facet_bytes = to_vec(&value);
1✔
615
        let rmp_bytes = rmp_serialize(&value);
1✔
616
        assert_eq!(facet_bytes, rmp_bytes);
1✔
617
    }
1✔
618

619
    #[test]
620
    fn test_f64() {
1✔
621
        #[derive(Facet, Serialize, PartialEq, Debug)]
622
        struct DoubleStruct {
623
            value: f64,
624
        }
625

626
        let value = DoubleStruct { value: -4.56e7 };
1✔
627
        let facet_bytes = to_vec(&value);
1✔
628
        let rmp_bytes = rmp_serialize(&value);
1✔
629
        assert_eq!(facet_bytes, rmp_bytes);
1✔
630
    }
1✔
631

632
    #[test]
633
    fn test_i8() {
1✔
634
        #[derive(Facet, Serialize, PartialEq, Debug)]
635
        struct I8Struct {
636
            value: i8,
637
        }
638

639
        let value = I8Struct { value: -10 };
1✔
640
        let facet_bytes = to_vec(&value);
1✔
641
        let rmp_bytes = rmp_serialize(&value);
1✔
642
        assert_eq!(facet_bytes, rmp_bytes);
1✔
643
    }
1✔
644

645
    #[test]
646
    fn test_i16() {
1✔
647
        #[derive(Facet, Serialize, PartialEq, Debug)]
648
        struct I16Struct {
649
            value: i16,
650
        }
651

652
        let value = I16Struct { value: -1000 };
1✔
653
        let facet_bytes = to_vec(&value);
1✔
654
        let rmp_bytes = rmp_serialize(&value);
1✔
655
        assert_eq!(facet_bytes, rmp_bytes);
1✔
656
    }
1✔
657

658
    #[test]
659
    fn test_i32() {
1✔
660
        #[derive(Facet, Serialize, PartialEq, Debug)]
661
        struct I32Struct {
662
            value: i32,
663
        }
664

665
        let value = I32Struct { value: -100000 };
1✔
666
        let facet_bytes = to_vec(&value);
1✔
667
        let rmp_bytes = rmp_serialize(&value);
1✔
668
        assert_eq!(facet_bytes, rmp_bytes);
1✔
669
    }
1✔
670

671
    #[test]
672
    fn test_i64() {
1✔
673
        #[derive(Facet, Serialize, PartialEq, Debug)]
674
        struct I64Struct {
675
            value: i64,
676
        }
677

678
        let value = I64Struct {
1✔
679
            value: -10000000000,
1✔
680
        };
1✔
681
        let facet_bytes = to_vec(&value);
1✔
682
        let rmp_bytes = rmp_serialize(&value);
1✔
683
        assert_eq!(facet_bytes, rmp_bytes);
1✔
684
    }
1✔
685

686
    #[test]
687
    fn test_u8() {
1✔
688
        #[derive(Facet, Serialize, PartialEq, Debug)]
689
        struct U8Struct {
690
            value: u8,
691
        }
692

693
        let value = U8Struct { value: 10 };
1✔
694
        let facet_bytes = to_vec(&value);
1✔
695
        let rmp_bytes = rmp_serialize(&value);
1✔
696
        assert_eq!(facet_bytes, rmp_bytes);
1✔
697
    }
1✔
698

699
    #[test]
700
    fn test_u16() {
1✔
701
        #[derive(Facet, Serialize, PartialEq, Debug)]
702
        struct U16Struct {
703
            value: u16,
704
        }
705

706
        let value = U16Struct { value: 1000 };
1✔
707
        let facet_bytes = to_vec(&value);
1✔
708
        let rmp_bytes = rmp_serialize(&value);
1✔
709
        assert_eq!(facet_bytes, rmp_bytes);
1✔
710
    }
1✔
711

712
    #[test]
713
    fn test_u32() {
1✔
714
        #[derive(Facet, Serialize, PartialEq, Debug)]
715
        struct U32Struct {
716
            value: u32,
717
        }
718

719
        let value = U32Struct { value: 100000 };
1✔
720
        let facet_bytes = to_vec(&value);
1✔
721
        let rmp_bytes = rmp_serialize(&value);
1✔
722
        assert_eq!(facet_bytes, rmp_bytes);
1✔
723
    }
1✔
724

725
    #[test]
726
    fn test_u64() {
1✔
727
        #[derive(Facet, Serialize, PartialEq, Debug)]
728
        struct U64Struct {
729
            value: u64,
730
        }
731

732
        let value = U64Struct { value: 10000000000 };
1✔
733
        let facet_bytes = to_vec(&value);
1✔
734
        let rmp_bytes = rmp_serialize(&value);
1✔
735
        assert_eq!(facet_bytes, rmp_bytes);
1✔
736
    }
1✔
737

738
    #[test]
739
    fn test_bytes() {
1✔
740
        #[derive(Facet, Serialize, PartialEq, Debug)]
741
        struct BytesStruct {
742
            value: Vec<u8>,
743
        }
744

745
        let value = BytesStruct {
1✔
746
            value: b"binary data".to_vec(),
1✔
747
        };
1✔
748
        let facet_bytes = to_vec(&value);
1✔
749
        let rmp_bytes = rmp_serialize(&value);
1✔
750
        assert_eq!(facet_bytes, rmp_bytes);
1✔
751
    }
1✔
752

753
    #[test]
754
    fn test_string() {
1✔
755
        #[derive(Facet, Serialize, PartialEq, Debug)]
756
        struct StringStruct {
757
            value: String,
758
        }
759

760
        let value = StringStruct {
1✔
761
            value: "string data".to_string(),
1✔
762
        };
1✔
763
        let facet_bytes = to_vec(&value);
1✔
764
        let rmp_bytes = rmp_serialize(&value);
1✔
765
        assert_eq!(facet_bytes, rmp_bytes);
1✔
766
    }
1✔
767

768
    #[test]
769
    fn test_char() {
1✔
770
        #[derive(Facet, Serialize, PartialEq, Debug)]
771
        struct CharStruct {
772
            value: char,
773
        }
774

775
        let value = CharStruct { value: '✅' };
1✔
776
        let facet_bytes = to_vec(&value);
1✔
777
        let rmp_bytes = rmp_serialize(&value);
1✔
778
        assert_eq!(facet_bytes, rmp_bytes);
1✔
779
    }
1✔
780

781
    #[test]
782
    fn test_option_some() {
1✔
783
        #[derive(Facet, Serialize, PartialEq, Debug)]
784
        struct OptionSomeStruct {
785
            value: Option<i32>,
786
        }
787

788
        let value = OptionSomeStruct { value: Some(99) };
1✔
789
        let facet_bytes = to_vec(&value);
1✔
790
        let rmp_bytes = rmp_serialize(&value);
1✔
791
        assert_eq!(facet_bytes, rmp_bytes);
1✔
792
    }
1✔
793

794
    #[test]
795
    fn test_option_none() {
1✔
796
        #[derive(Facet, Serialize, PartialEq, Debug)]
797
        struct OptionNoneStruct {
798
            value: Option<String>,
799
        }
800

801
        let value = OptionNoneStruct { value: None };
1✔
802
        let facet_bytes = to_vec(&value);
1✔
803
        let rmp_bytes = rmp_serialize(&value);
1✔
804
        assert_eq!(facet_bytes, rmp_bytes);
1✔
805
    }
1✔
806

807
    #[test]
808
    fn test_unit() {
1✔
809
        #[derive(Facet, Serialize, PartialEq, Debug)]
810
        struct UnitStruct {
811
            value: (),
812
        }
813

814
        let value = UnitStruct { value: () };
1✔
815
        let facet_bytes = to_vec(&value);
1✔
816
        let rmp_bytes = rmp_serialize(&value);
1✔
817
        assert_eq!(facet_bytes, rmp_bytes);
1✔
818
    }
1✔
819

820
    #[test]
821
    fn test_empty_vec() {
1✔
822
        #[derive(Facet, Serialize, PartialEq, Debug)]
823
        struct EmptyVecStruct {
824
            value: Vec<i32>,
825
        }
826

827
        let value = EmptyVecStruct { value: vec![] };
1✔
828
        let facet_bytes = to_vec(&value);
1✔
829

830
        // Empty collections are serialized as nil in facet-msgpack to maintain consistency
831
        // with how unit types are handled. This ensures uniform behavior for "empty" values.
832
        let expected = vec![0x81, 0xa5, b'v', b'a', b'l', b'u', b'e', 0xc0]; // map with "value" -> nil
1✔
833
        assert_eq!(facet_bytes, expected);
1✔
834
    }
1✔
835
}
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