[−][src]Trait endio::Serialize
Implement this for your types to be able to write
them.
Examples
Serialize a struct:
Note how the trait bound for W
is EWrite<E>
, as we want to use the functionality of this crate to delegate serialization to the struct's fields.
Note: Rust currently can't recognize sealed traits, so even though the primitive types are implemented, you may need to write where
clauses like below for this to work. If/When the compiler gets smarter about sealed traits this won't be necessary.
struct Example { a: u8, b: bool, c: u32, } { use std::io::Result; use endio::{Endianness, EWrite, Serialize}; impl<E: Endianness, W: EWrite<E>> Serialize<E, W> for &Example where u8 : Serialize<E, W>, bool: Serialize<E, W>, u32 : Serialize<E, W> { fn serialize(self, writer: &mut W) -> Result<()> { writer.write(self.a)?; writer.write(self.b)?; writer.write(self.c) } } } // will then allow you to directly write: { use endio::LEWrite; let mut writer = vec![]; let e = Example { a: 42, b: true, c: 754187983 }; writer.write(&e); assert_eq!(writer, b"\x2a\x01\xcf\xfe\xf3\x2c"); }
Serialize a primitive / something where you need to use the bare std::io::Write
functionality:
Note how the trait bound for W
is Write
.
use std::io::{Result, Write}; use endio::{Endianness, EWrite, Serialize}; struct new_u8(u8); impl<E: Endianness, W: Write> Serialize<E, W> for &new_u8 { fn serialize(self, writer: &mut W) -> Result<()> { let mut buf = [0; 1]; buf[0] = self.0; writer.write_all(&buf); Ok(()) } }
Serialize with endian-specific code:
Note how instead of using a trait bound on Endianness, we implement Serialize twice, once for BigEndian
and once for LittleEndian
.
use std::io::{Result, Write}; use std::mem::size_of; use endio::{BigEndian, Serialize, LittleEndian}; struct new_u16(u16); impl<W: Write> Serialize<BigEndian, W> for new_u16 { fn serialize(self, writer: &mut W) -> Result<()> { let mut buf = [0; size_of::<u16>()]; writer.write_all(&self.0.to_be_bytes())?; Ok(()) } } impl<W: Write> Serialize<LittleEndian, W> for new_u16 { fn serialize(self, writer: &mut W) -> Result<()> { writer.write_all(&self.0.to_le_bytes())?; Ok(()) } }
Required methods
Loading content...Implementations on Foreign Types
impl<E: Endianness, W: Write, '_> Serialize<E, W> for &'_ [u8]
[src]
Writes the entire contents of the byte slice. Equivalent to std::io::Write::write_all
.
impl<E: Endianness, W: Write, '_> Serialize<E, W> for &'_ Vec<u8>
[src]
Writes the entire contents of the Vec
impl<E: Endianness, W: Write> Serialize<E, W> for bool
[src]
Writes a bool by writing a byte.
impl<E: Endianness, W: Write> Serialize<E, W> for u8
[src]
impl<E: Endianness, W: Write> Serialize<E, W> for i8
[src]
impl<W: Write> Serialize<BigEndian, W> for u16
[src]
impl<W: Write> Serialize<LittleEndian, W> for u16
[src]
impl<W: Write> Serialize<BigEndian, W> for u32
[src]
impl<W: Write> Serialize<LittleEndian, W> for u32
[src]
impl<W: Write> Serialize<BigEndian, W> for u64
[src]
impl<W: Write> Serialize<LittleEndian, W> for u64
[src]
impl<W: Write> Serialize<BigEndian, W> for u128
[src]
impl<W: Write> Serialize<LittleEndian, W> for u128
[src]
impl<W: Write> Serialize<BigEndian, W> for i16
[src]
impl<W: Write> Serialize<LittleEndian, W> for i16
[src]
impl<W: Write> Serialize<BigEndian, W> for i32
[src]
impl<W: Write> Serialize<LittleEndian, W> for i32
[src]
impl<W: Write> Serialize<BigEndian, W> for i64
[src]
impl<W: Write> Serialize<LittleEndian, W> for i64
[src]
impl<W: Write> Serialize<BigEndian, W> for i128
[src]
impl<W: Write> Serialize<LittleEndian, W> for i128
[src]
impl<E: Endianness, W: EWrite<E>> Serialize<E, W> for f32 where
u32: Serialize<E, W>,
[src]
u32: Serialize<E, W>,
impl<E: Endianness, W: EWrite<E>> Serialize<E, W> for f64 where
u64: Serialize<E, W>,
[src]
Loading content...
u64: Serialize<E, W>,