pub struct Serializer { /* private fields */ }
Expand description

Serializer struct containing a serialization buffer. Please note that this struct expects to have a sufficiently large buffer to perform the ongoing serialization.

Implementations§

Create a new Serializer struct with len byte buffer

Examples found in repository?
src/lib.rs (line 271)
270
271
272
    fn default() -> Serializer {
        Serializer::new(4096)
    }

Returns the current byte length of the ongoing serialization (cursor position)

Examples found in repository?
src/lib.rs (line 304)
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
    pub fn offset(&mut self, offset: usize) -> &mut Self {
        if self.cursor + offset >= self.len() {
        }
        self.cursor += offset; 
        self
    }
    
    /// Try advance the cursor by `offset` bytes. Since the underlying
    /// buffer is zero-initialized, skipped bytes will remain as zero.
    pub fn try_offset(&mut self, offset: usize) -> Result<&mut Self> {
        if self.cursor + offset >= self.data.len() {
            return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
        }
        self.cursor += offset; 
        Ok(self)
    }

    /// Advance the cursor by `offset` bytes while explicitly setting
    /// skipped bytes to zero. This can be useful if manually positioning
    /// cursor within the buffer and the repositioning can result in 
    /// the buffer containing previously serialized data.
    pub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self {
        for _ in 0..offset {
            self.store_u8(0);
        }
        self
    }

    /// Try advance the cursor by `offset` bytes while explicitly setting
    /// skipped bytes to zero. This can be useful if manually positioning
    /// cursor within the buffer and the repositioning can result in 
    /// the buffer containing previously serialized data.
    pub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self> {
        if self.cursor + offset >= self.data.len() {
            return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
        }
        for _ in 0..offset {
            self.store_u8(0);
        }
        Ok(self)
    }

Returns Vec<u8> of the currently serialized data

Returns a slice &[u8] of the currently serialized data

Advance the cursor by offset bytes. Since the underlying buffer is zero-initialized, skipped bytes will remain as zero.

Examples found in repository?
src/lib.rs (line 349)
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    pub fn align_u32(&mut self) -> &mut Self {
        let offset = self.cursor % 4;
        self.offset(offset)
    }

    /// Try advance the cursor to ensure that the current cursor position
    /// is on the 32-bit alignment boundary.
    pub fn try_align_u32(&mut self) -> Result<&mut Self> {
        let offset = self.cursor % 4;
        self.try_offset(offset)
    }

    /// Advance the cursor to ensure that the current cursor position
    /// is on the 64-bit alignment boundary.
    pub fn align_u64(&mut self) -> &mut Self {
        let offset = self.cursor % 8;
        self.offset(offset)
    }

Try advance the cursor by offset bytes. Since the underlying buffer is zero-initialized, skipped bytes will remain as zero.

Examples found in repository?
src/lib.rs (line 356)
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
    pub fn try_align_u32(&mut self) -> Result<&mut Self> {
        let offset = self.cursor % 4;
        self.try_offset(offset)
    }

    /// Advance the cursor to ensure that the current cursor position
    /// is on the 64-bit alignment boundary.
    pub fn align_u64(&mut self) -> &mut Self {
        let offset = self.cursor % 8;
        self.offset(offset)
    }

    /// Try advance the cursor to ensure that the current cursor position
    /// is on the 64-bit alignment boundary.
    pub fn try_align_u64(&mut self) -> Result<&mut Self> {
        let offset = self.cursor % 8;
        self.try_offset(offset)
    }

Advance the cursor by offset bytes while explicitly setting skipped bytes to zero. This can be useful if manually positioning cursor within the buffer and the repositioning can result in the buffer containing previously serialized data.

Try advance the cursor by offset bytes while explicitly setting skipped bytes to zero. This can be useful if manually positioning cursor within the buffer and the repositioning can result in the buffer containing previously serialized data.

Advance the cursor to ensure that the current cursor position is on the 32-bit alignment boundary.

Try advance the cursor to ensure that the current cursor position is on the 32-bit alignment boundary.

Advance the cursor to ensure that the current cursor position is on the 64-bit alignment boundary.

Try advance the cursor to ensure that the current cursor position is on the 64-bit alignment boundary.

Store a single u8 value, advancing the cursor by 1 byte.

Examples found in repository?
src/lib.rs (line 326)
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    pub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self {
        for _ in 0..offset {
            self.store_u8(0);
        }
        self
    }

    /// Try advance the cursor by `offset` bytes while explicitly setting
    /// skipped bytes to zero. This can be useful if manually positioning
    /// cursor within the buffer and the repositioning can result in 
    /// the buffer containing previously serialized data.
    pub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self> {
        if self.cursor + offset >= self.data.len() {
            return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
        }
        for _ in 0..offset {
            self.store_u8(0);
        }
        Ok(self)
    }

Try store a single u8 value, advancing the cursor by 1 byte.

Store a u16 value using little-endian encoding, advancing the cursor by 2 bytes.

Try to store a u16 value using little-endian encoding, advancing the cursor by 2 bytes.

Store a u32 value using little-endian encoding, advancing the cursor by 4 bytes.

Try to store a u32 value using little-endian encoding, advancing the cursor by 4 bytes.

Store a u64 value using little-endian encoding, advancing the cursor by 8 bytes.

Try to store a u64 value using little-endian encoding, advancing the cursor by 8 bytes.

Try to store a Rust String as a zero-terminated sequence of u16 little-endian encoded bytes. This is useful to serialize windows PCWSTR zero-terminated strings.

Try to store a u8 slice

Try to store a u16 slice as a sequence of little-endian encoded u16 values.

Examples found in repository?
src/lib.rs (line 463)
452
453
454
455
456
457
458
459
460
461
462
463
464
465
    pub fn try_store_utf16le_sz(&mut self, text : &String) -> Result<&mut Self> {
        let len = text.len()+1;
        let mut vec: Vec<u16> = Vec::with_capacity(len);
        for c in text.chars() {
            // TODO - proper encoding
            // let buf = [0;2];
            // c.encode_utf16(&mut buf);
            vec.push(c as u16);
        }
        vec.push(0);
        // println!("text: {} vec: {:?}",text,vec);
        self.try_store_u16le_slice(&vec)?;
        Ok(self)
    }

Store a primitive implementing a Serialize trait

Try store a primitive implementing a TrySerialize trait

Trait Implementations§

Default implementation for Serializer that allocates 4096 byte buffer.

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.