Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder<W> { /* private fields */ }
Expand description

A non-allocating CBOR encoder writing encoded bytes to the given Write sink.

Implementations§

Source§

impl<W> Encoder<W>
where W: Write,

Source

pub fn new(writer: W) -> Encoder<W>

Construct an Encoder that writes to the given Write sink.

Source

pub fn writer(&self) -> &W

Access the inner writer.

Source

pub fn writer_mut(&mut self) -> &mut W

Get mutable access to the inner writer.

Source

pub fn into_writer(self) -> W

Get back the Write impl.

Source

pub fn encode<T>( &mut self, x: T, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>
where T: Encode<()>,

Encode any type that implements Encode.

Source

pub fn encode_with<C, T>( &mut self, x: T, ctx: &mut C, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>
where T: Encode<C>,

Encode any type that implements Encode.

Source

pub fn u8( &mut self, x: u8, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a u8 value.

Source

pub fn i8( &mut self, x: i8, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an i8 value.

Source

pub fn u16( &mut self, x: u16, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a u16 value.

Source

pub fn i16( &mut self, x: i16, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an i16 value.

Source

pub fn u32( &mut self, x: u32, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a u32 value.

Source

pub fn i32( &mut self, x: i32, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an i32 value.

Source

pub fn u64( &mut self, x: u64, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a u64 value.

Source

pub fn i64( &mut self, x: i64, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an i64 value.

Source

pub fn int( &mut self, x: Int, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a CBOR integer.

See Int for details regarding the value range of CBOR integers.

Source

pub fn null(&mut self) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a CBOR null value.

Source

pub fn undefined( &mut self, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a CBOR undefined value.

Source

pub fn simple( &mut self, x: u8, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a CBOR simple value.

Source

pub fn f16( &mut self, x: f32, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an f32 value as a half float (f16).

Requires feature "half".

NB: The conversion from f32 to f16 is potentially lossy. Generally values are truncated and rounded to the nearest 16-bit value, except:

  • 32-bit values which do not fit into 16 bit become ±∞.
  • 32-bit subnormal values become ±0.
  • Exponents smaller than the min. 16-bit exponent become 16-bit subnormals or ±0.

For further details please consult the half crate which is used internally for f16 support.

Source

pub fn f32( &mut self, x: f32, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an f32 value.

Source

pub fn f64( &mut self, x: f64, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode an f64 value.

Source

pub fn bool( &mut self, x: bool, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a bool value.

Source

pub fn char( &mut self, x: char, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a char value.

Source

pub fn tag<T>( &mut self, x: T, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>
where T: Into<Tag>,

Encode a CBOR tag.

Source

pub fn bytes( &mut self, x: &[u8], ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a byte slice.

Source

pub fn str( &mut self, x: &str, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Encode a string slice.

Source

pub fn array( &mut self, len: u64, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding an array with len elements.

Source

pub fn map( &mut self, len: u64, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding a map with len entries.

Source

pub fn begin_array( &mut self, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding an array of unknown size.

Use Encoder::end to terminate the array.

Source

pub fn begin_bytes( &mut self, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding an indefinite number of byte slices.

Use Encoder::end to terminate.

Source

pub fn begin_map( &mut self, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding a map of unknown size.

Use Encoder::end to terminate the map.

Source

pub fn begin_str( &mut self, ) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Begin encoding an indefinite number of string slices.

Use Encoder::end to terminate.

Source

pub fn end(&mut self) -> Result<&mut Encoder<W>, Error<<W as Write>::Error>>

Terminate an indefinite collection.

Source

pub fn ok(&mut self) -> Result<(), Error<<W as Write>::Error>>

Syntactic sugar for Ok(()).

Source

pub fn tokens<'a, 'b, I>( &mut self, tokens: I, ) -> Result<(), Error<<W as Write>::Error>>
where 'b: 'a, I: IntoIterator<Item = &'a Token<'b>>,

Encode a sequence of CBOR tokens.

Trait Implementations§

Source§

impl<W> Clone for Encoder<W>
where W: Clone,

Source§

fn clone(&self) -> Encoder<W>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<W> Debug for Encoder<W>
where W: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<W> UnsafeUnpin for Encoder<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for Encoder<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.