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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use super::*;
use config::{BincodeByteOrder, IntEncoding, Options};
use serde::{ser::*, serde_if_integer128};

#[cfg(feature = "std")]
use std::error::Error as StdError;

/// Serialize a given `T` type into a given `CoreWrite` writer with the given `B` byte order.
///
/// `T` can be any value that derives `serde::Serialize`.
///
/// `W` can be any value that implements [CoreWrite]. This can e.g. be a fixed-size array, or a
/// serial writer.
///
/// `B` can be any type that implements [byteorder::ByteOrder]. This includes:
/// - BigEndian
/// - LittleEndian
/// - NetworkEndian.
pub fn serialize<T: serde::Serialize + ?Sized, W: CoreWrite, O: Options>(
    value: &T,
    writer: W,
    options: O,
) -> Result<(), SerializeError<W>> {
    let mut serializer = Serializer::<W, O> {
        writer,
        _options: options,
    };
    value.serialize(&mut serializer)
}

/// Return the size that serializing a given `T` type would need to be stored. This is an optimized version of getting the length of the writer after it's done writing.
/// ```
/// # use bincode_core::*;
/// let mut buffer = [0u8; 1000];
/// let mut writer = BufferWriter::new(&mut buffer);
/// let options = DefaultOptions::new();
///
/// let value = "your data structure goes here";
///
/// serialize(value, &mut writer, options).unwrap();
/// let written_len = writer.written_len();
///
/// let measured_len = serialize_size(value, options).unwrap();
///
/// assert_eq!(written_len, measured_len);
/// ```
/// But without actually writing to memory
pub fn serialize_size<T: serde::Serialize + ?Sized, O: Options>(
    value: &T,
    options: O,
) -> Result<usize, SerializeError<()>> {
    let mut size_checker = crate::size_checker::SizeChecker { options, total: 0 };
    value.serialize(&mut size_checker)?;
    Ok(size_checker.total)
}

/// Any error that can be thrown while serializing a type
pub enum SerializeError<W: CoreWrite + ?Sized> {
    /// Generic write error. See the inner `CoreWrite::Error` for more info
    Write(W::Error),

    /// A sequence (e.g. `&str` or `&[u8]`) was requested to serialize, but it has no length.
    SequenceMustHaveLength,
}

impl<W: CoreWrite> core::fmt::Debug for SerializeError<W> {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            SerializeError::Write(w) => write!(fmt, "Write error {:?}", w),
            SerializeError::SequenceMustHaveLength => write!(fmt, "Sequence does not have length"),
        }
    }
}

impl<W: CoreWrite> core::fmt::Display for SerializeError<W> {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        core::fmt::Debug::fmt(self, fmt)
    }
}

impl<W: CoreWrite> serde::ser::Error for SerializeError<W> {
    fn custom<T: core::fmt::Display>(_cause: T) -> Self {
        // Custom errors not supported
        panic!("Custom error: {}", _cause);
    }
}

#[cfg(feature = "std")]
impl<W: CoreWrite> StdError for SerializeError<W> {}

/// A serializer that can serialize any value that implements `serde::Serialize` into a given
/// [CoreWrite] writer.
pub struct Serializer<W: CoreWrite, O: Options> {
    writer: W,
    _options: O,
}

macro_rules! impl_serialize_literal {
    ($ser_method:ident($ty:ty) = $write:ident()) => {
        pub(crate) fn $ser_method(&mut self, v: $ty) -> Result<(), SerializeError<W>> {
            const LEN: usize = core::mem::size_of::<$ty>();

            let mut buf = [0u8; LEN];
            <<O::Endian as BincodeByteOrder>::Endian as byteorder::ByteOrder>::$write(&mut buf, v);
            self.writer.write_all(&buf).map_err(SerializeError::Write)
        }
    };
}

impl<W: CoreWrite, O: Options> Serializer<W, O> {
    pub(crate) fn serialize_byte(&mut self, v: u8) -> Result<(), SerializeError<W>> {
        self.writer.write(v).map_err(SerializeError::Write)
    }

    impl_serialize_literal! {serialize_literal_u16(u16) = write_u16()}
    impl_serialize_literal! {serialize_literal_u32(u32) = write_u32()}
    impl_serialize_literal! {serialize_literal_u64(u64) = write_u64()}

    serde_if_integer128! {
        impl_serialize_literal!{serialize_literal_u128(u128) = write_u128()}
    }
}

macro_rules! impl_serialize_int {
    ($ser_method:ident($ty:ty) = $ser_int:ident()) => {
        fn $ser_method(self, v: $ty) -> Result<Self::Ok, Self::Error> {
            O::IntEncoding::$ser_int(self, v)
        }
    };
}

impl<'a, W: CoreWrite, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
    type Ok = ();
    type Error = SerializeError<W>;
    type SerializeSeq = Compound<'a, W, O>;
    type SerializeTuple = Compound<'a, W, O>;
    type SerializeTupleStruct = Compound<'a, W, O>;
    type SerializeTupleVariant = Compound<'a, W, O>;
    type SerializeMap = Compound<'a, W, O>;
    type SerializeStruct = Compound<'a, W, O>;
    type SerializeStructVariant = Compound<'a, W, O>;

    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
        self.serialize_byte(v as u8)
    }

    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.serialize_byte(v as u8)
    }

    impl_serialize_int! {serialize_u16(u16) = serialize_u16()}
    impl_serialize_int! {serialize_u32(u32) = serialize_u32()}
    impl_serialize_int! {serialize_u64(u64) = serialize_u64()}

    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        self.serialize_byte(v)
    }

    impl_serialize_int! {serialize_i16(i16) = serialize_i16()}
    impl_serialize_int! {serialize_i32(i32) = serialize_i32()}
    impl_serialize_int! {serialize_i64(i64) = serialize_i64()}

    serde_if_integer128! {
        impl_serialize_int!{serialize_u128(u128) = serialize_u128()}
        impl_serialize_int!{serialize_i128(i128) = serialize_i128()}
    }

    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
        let mut buf = [0u8; 4];
        <<O::Endian as BincodeByteOrder>::Endian as byteorder::ByteOrder>::write_f32(&mut buf, v);
        self.writer.write_all(&buf).map_err(SerializeError::Write)
    }

    fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
        let mut buf = [0u8; 8];
        <<O::Endian as BincodeByteOrder>::Endian as byteorder::ByteOrder>::write_f64(&mut buf, v);
        self.writer.write_all(&buf).map_err(SerializeError::Write)
    }

    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
        self.writer
            .write_all(encode_utf8(v).as_slice())
            .map_err(SerializeError::Write)
    }

    fn serialize_str(mut self, v: &str) -> Result<Self::Ok, Self::Error> {
        O::IntEncoding::serialize_len(&mut self, v.len())?;
        self.writer
            .write_all(v.as_bytes())
            .map_err(SerializeError::Write)
    }

    fn serialize_bytes(mut self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        O::IntEncoding::serialize_len(&mut self, v.len())?;
        self.writer.write_all(v).map_err(SerializeError::Write)
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        self.writer.write(0).map_err(SerializeError::Write)
    }

    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        self.writer.write(1).map_err(SerializeError::Write)?;
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn serialize_unit_variant(
        mut self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        O::IntEncoding::serialize_u32(&mut self, variant_index)
    }

    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _name: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T: ?Sized>(
        mut self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        O::IntEncoding::serialize_u32(&mut self, variant_index)?;
        value.serialize(self)
    }

    fn serialize_seq(mut self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        O::IntEncoding::serialize_len(&mut self, len.expect("Sequence has no elements"))?;
        Ok(Compound { ser: self })
    }

    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        Ok(Compound { ser: self })
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        Ok(Compound { ser: self })
    }

    fn serialize_tuple_variant(
        mut self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        O::IntEncoding::serialize_u32(&mut self, variant_index)?;
        Ok(Compound { ser: self })
    }

    fn serialize_map(mut self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        O::IntEncoding::serialize_len(&mut self, len.expect("Sequence has no elements"))?;
        Ok(Compound { ser: self })
    }

    fn serialize_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        Ok(Compound { ser: self })
    }

    fn serialize_struct_variant(
        mut self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        O::IntEncoding::serialize_u32(&mut self, variant_index)?;
        Ok(Compound { ser: self })
    }

    fn collect_str<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: core::fmt::Display,
    {
        panic!("Unimplemented: Serialize::collect_str")
    }

    fn is_human_readable(&self) -> bool {
        false
    }
}

/// Internal struct needed for serialization.
pub struct Compound<'a, W: CoreWrite, O: Options> {
    ser: &'a mut Serializer<W, O>,
}

impl<'a, W: CoreWrite, O: Options> SerializeSeq for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeTuple for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeTupleStruct for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeTupleVariant for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeMap for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_key<K: ?Sized>(&mut self, value: &K) -> Result<(), Self::Error>
    where
        K: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn serialize_value<V: ?Sized>(&mut self, value: &V) -> Result<(), Self::Error>
    where
        V: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeStruct for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    #[inline]
    fn serialize_field<T: ?Sized>(
        &mut self,
        _key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: serde::ser::Serialize,
    {
        value.serialize(&mut *self.ser)
    }

    #[inline]
    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, W: CoreWrite, O: Options> SerializeStructVariant for Compound<'a, W, O> {
    type Ok = ();
    type Error = SerializeError<W>;

    fn serialize_field<T: ?Sized + Serialize>(
        &mut self,
        _key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error> {
        value.serialize(&mut *self.ser)
    }

    fn end(self) -> Result<(), Self::Error> {
        Ok(())
    }
}

const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO_B: u8 = 0b1100_0000;
const TAG_THREE_B: u8 = 0b1110_0000;
const TAG_FOUR_B: u8 = 0b1111_0000;
const MAX_ONE_B: u32 = 0x80;
const MAX_TWO_B: u32 = 0x800;
const MAX_THREE_B: u32 = 0x10000;

fn encode_utf8(c: char) -> EncodeUtf8 {
    let code = c as u32;
    let mut buf = [0; 4];
    let pos = if code < MAX_ONE_B {
        buf[3] = code as u8;
        3
    } else if code < MAX_TWO_B {
        buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
        buf[3] = (code & 0x3F) as u8 | TAG_CONT;
        2
    } else if code < MAX_THREE_B {
        buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
        buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
        buf[3] = (code & 0x3F) as u8 | TAG_CONT;
        1
    } else {
        buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
        buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
        buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
        buf[3] = (code & 0x3F) as u8 | TAG_CONT;
        0
    };
    EncodeUtf8 { buf, pos }
}

struct EncodeUtf8 {
    buf: [u8; 4],
    pos: usize,
}

impl EncodeUtf8 {
    fn as_slice(&self) -> &[u8] {
        &self.buf[self.pos..]
    }
}