Serializer

Struct Serializer 

Source
pub struct Serializer<W: Write + Sized>(/* private fields */);
Expand description

simple CBOR serializer into any std::io::Write.

Implementations§

Source§

impl Serializer<Vec<u8>>

Source

pub fn new_vec() -> Self

create a new serializer.

use cbor_event::se::{Serializer};

let serializer = Serializer::new_vec();
Source§

impl<W: Write + Sized> Serializer<W>

Source

pub fn write_raw_bytes(&mut self, bytes: &[u8]) -> Result<&mut Self>

extend the serializer with the given bytes

This is not encoding the given bytes in the CBOR format. More a way to add already CBOR encoded data or to add any bytes that may suite your protocol.

Source

pub fn new(w: W) -> Self

Source

pub fn finalize(self) -> W

finalize the serializer, returning the serializer bytes

use cbor_event::se::{Serializer};

let serializer = Serializer::new_vec();

let bytes = serializer.finalize();
Source

pub fn write_unsigned_integer(&mut self, value: u64) -> Result<&mut Self>

serialise the given unsigned integer

§Example
use cbor_event::se::{Serializer};

let mut serializer = Serializer::new_vec();
serializer.write_unsigned_integer(0x12)
    .expect("write a negative integer");
Source

pub fn write_unsigned_integer_sz( &mut self, value: u64, sz: Sz, ) -> Result<&mut Self>

serialise the given unsigned integer using a specific encoding

see write_unsigned_integer and Sz

Source

pub fn write_negative_integer(&mut self, value: i64) -> Result<&mut Self>

write a negative integer

This function fails if one tries to write a non negative value.

use cbor_event::se::{Serializer};

let mut serializer = Serializer::new_vec();
serializer.write_negative_integer(-12)
    .expect("write a negative integer");
Source

pub fn write_negative_integer_sz( &mut self, value: i128, sz: Sz, ) -> Result<&mut Self>

write a negative integer using a specific encoding

see write_negative_integer and Sz

value must be within -1 and -u64::MAX -1 to fit into CBOR nint

Source

pub fn write_bytes<B: AsRef<[u8]>>(&mut self, bytes: B) -> Result<&mut Self>

write the given object as bytes

use cbor_event::se::{Serializer};

let mut serializer = Serializer::new_vec();
serializer.write_bytes(vec![0,1,2,3])
    .expect("write bytes");
Source

pub fn write_bytes_sz<B: AsRef<[u8]>>( &mut self, bytes: B, sz: StringLenSz, ) -> Result<&mut Self>

write the given object as bytes using a specific bytestring encoding

see write_bytes and StringLenSz

Source

pub fn write_text<S: AsRef<str>>(&mut self, text: S) -> Result<&mut Self>

write the given object as text

use cbor_event::se::{Serializer};

let mut serializer = Serializer::new_vec();
serializer.write_text(r"hello world")
    .expect("write text");
Source

pub fn write_text_sz<S: AsRef<str>>( &mut self, text: S, sz: StringLenSz, ) -> Result<&mut Self>

write the given object as text using a specific string encoding

see write_text and StringLenSz

Source

pub fn write_array(&mut self, len: Len) -> Result<&mut Self>

start to write an array

Either you know the length of your array and you can pass it to the funtion or use an indefinite length.

  • if you set a fixed length of element, you are responsible to set the correct amount of elements.
  • if you set an indefinite length, you are responsible to write the Special::Break when your stream ends.
§Example
use cbor_event::{se::{Serializer}, Len};

let mut serializer = Serializer::new_vec();
serializer
    .write_array(Len::Len(2)).expect("write an array")
    .write_text(r"hello").expect("write text")
    .write_text(r"world").expect("write text");
use cbor_event::{se::{Serializer}, Len, Special};

let mut serializer = Serializer::new_vec();
serializer
    .write_array(Len::Indefinite).expect("write an array")
    .write_text(r"hello").expect("write text")
    .write_text(r"world").expect("write text")
    .write_special(Special::Break).expect("write break");
Source

pub fn write_array_sz(&mut self, len: LenSz) -> Result<&mut Self>

start to write an array using a specific length encoding

see write_array and LenSz

Source

pub fn write_map(&mut self, len: Len) -> Result<&mut Self>

start to write a map

Either you know the length of your map and you can pass it to the funtion or use an indefinite length.

  • if you set a fixed length of element, you are responsible to set the correct amount of elements.
  • if you set an indefinite length, you are responsible to write the Special::Break when your stream ends.

A map is like an array but works by pair of element, so the length is half of the number of element you are going to write, i.e. the number of pairs, not the number of elements.

§Example
use cbor_event::{se::{Serializer}, Len};

let mut serializer = Serializer::new_vec();
serializer
    .write_map(Len::Len(2)).expect("write a map")
    .write_unsigned_integer(1).expect("write unsigned integer")
    .write_text(r"hello").expect("write text")
    .write_unsigned_integer(2).expect("write unsigned integer")
    .write_text(r"world").expect("write text");
use cbor_event::{se::{Serializer}, Len, Special};

let mut serializer = Serializer::new_vec();
serializer
    .write_map(Len::Indefinite).expect("write a map")
    .write_unsigned_integer(1).expect("write unsigned integer")
    .write_text(r"hello").expect("write text")
    .write_unsigned_integer(2).expect("write unsigned integer")
    .write_text(r"world").expect("write text")
    .write_special(Special::Break).expect("write the break");
Source

pub fn write_map_sz(&mut self, len: LenSz) -> Result<&mut Self>

start to write a map using a specific length encoding

see write_map and LenSz

Source

pub fn write_tag(&mut self, tag: u64) -> Result<&mut Self>

write a tag

in cbor a tag should be followed by a tagged object. You are responsible to making sure you are writing the tagged object just after this

§Example
use cbor_event::{se::{Serializer}, Len};

let mut serializer = Serializer::new_vec();
serializer
    .write_tag(24).expect("write a tag")
    .write_text(r"hello").expect("write text");
Source

pub fn write_tag_sz(&mut self, tag: u64, sz: Sz) -> Result<&mut Self>

write a tag using a specific encoding

see write_tag and Sz

Source

pub fn write_set_tag(&mut self) -> Result<&mut Self>

Write a tag that indicates that the following list is a finite set. See https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml.

Source

pub fn write_special(&mut self, special: Special) -> Result<&mut Self>

write a special value in cbor

§Example
use cbor_event::{se::{Serializer}, Len, Special};

let mut serializer = Serializer::new_vec();
serializer
    .write_array(Len::Indefinite).expect("write an array")
    .write_special(Special::Bool(false)).expect("write false")
    .write_special(Special::Bool(true)).expect("write true")
    .write_special(Special::Null).expect("write null")
    .write_special(Special::Undefined).expect("write undefined")
    .write_special(Special::Break).expect("write the break");
Source

pub fn serialize<T: Serialize>(&mut self, t: &T) -> Result<&mut Self>

Convenient member function to chain serialisation

Trait Implementations§

Source§

impl<W: Debug + Write + Sized> Debug for Serializer<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Serializer<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Serializer<W>
where W: RefUnwindSafe,

§

impl<W> Send for Serializer<W>
where W: Send,

§

impl<W> Sync for Serializer<W>
where W: Sync,

§

impl<W> Unpin for Serializer<W>
where W: Unpin,

§

impl<W> UnwindSafe for Serializer<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.