[go: up one dir, main page]

cbor

Struct Encoder

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

Encodes Rust values to CBOR bytes in the underlying writer W.

Note that currently, using the serialization infrastructure is the only way to write CBOR in this crate.

Implementations§

Source§

impl<W: Write> Encoder<W>

Source

pub fn from_writer(wtr: W) -> Encoder<BufWriter<W>>

Encode CBOR to an arbitrary writer.

Source

pub fn encode<I>(&mut self, it: I) -> CborResult<()>

Encode an iterator of Rust values to CBOR in the underlying writer.

Every value in the iterator must satisfy Encodable (from the rustc-serialize crate).

Note that this encodes top-level CBOR data items. They can be decoded in a streaming fashion.

§Example

Encode a list of numbers.

use cbor::Encoder;

let mut enc = Encoder::from_memory();
enc.encode(&[1, 2, 3, 4, 5]);
assert_eq!(vec![1, 2, 3, 4, 5], enc.as_bytes());
Source

pub fn flush(&mut self) -> CborResult<()>

Flush the underlying writer.

Source§

impl Encoder<Vec<u8>>

Source

pub fn from_memory() -> Encoder<Vec<u8>>

Encode CBOR to an in memory buffer.

Source

pub fn as_bytes(&mut self) -> &[u8]

Flush and retrieve the CBOR bytes that have been written.

Source

pub fn into_bytes(self) -> Vec<u8>

Flush and retrieve the CBOR bytes that have been written without copying.

Trait Implementations§

Source§

impl<W: Write> Encoder for Encoder<W>

Source§

type Error = CborError

The error type for method results.
Source§

fn emit_nil(&mut self) -> CborResult<()>

Emit a nil value. Read more
Source§

fn emit_usize(&mut self, v: usize) -> CborResult<()>

Emit a usize value.
Source§

fn emit_u64(&mut self, v: u64) -> CborResult<()>

Emit a u64 value.
Source§

fn emit_u32(&mut self, v: u32) -> CborResult<()>

Emit a u32 value.
Source§

fn emit_u16(&mut self, v: u16) -> CborResult<()>

Emit a u16 value.
Source§

fn emit_u8(&mut self, v: u8) -> CborResult<()>

Emit a u8 value.
Source§

fn emit_isize(&mut self, v: isize) -> CborResult<()>

Emit a isize value.
Source§

fn emit_i64(&mut self, v: i64) -> CborResult<()>

Emit a i64 value.
Source§

fn emit_i32(&mut self, v: i32) -> CborResult<()>

Emit a i32 value.
Source§

fn emit_i16(&mut self, v: i16) -> CborResult<()>

Emit a i16 value.
Source§

fn emit_i8(&mut self, v: i8) -> CborResult<()>

Emit a i8 value.
Source§

fn emit_f64(&mut self, v: f64) -> CborResult<()>

Emit a f64 value.
Source§

fn emit_f32(&mut self, v: f32) -> CborResult<()>

Emit a f32 value.
Source§

fn emit_bool(&mut self, v: bool) -> CborResult<()>

Emit a bool value. Read more
Source§

fn emit_char(&mut self, v: char) -> CborResult<()>

Emit a char value. Read more
Source§

fn emit_str(&mut self, v: &str) -> CborResult<()>

Emit a string value.
Source§

fn emit_enum<F>(&mut self, _name: &str, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit an enumeration value. Read more
Source§

fn emit_enum_variant<F>( &mut self, v_name: &str, _v_id: usize, len: usize, f: F, ) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a enumeration variant value with no or unnamed data. Read more
Source§

fn emit_enum_variant_arg<F>(&mut self, _idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit an unnamed data item for an enumeration variant. Read more
Source§

fn emit_enum_struct_variant<F>( &mut self, v_name: &str, v_id: usize, len: usize, f: F, ) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a enumeration variant value with no or named data. Read more
Source§

fn emit_enum_struct_variant_field<F>( &mut self, _f_name: &str, idx: usize, f: F, ) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a named data item for an enumeration variant. Read more
Source§

fn emit_struct<F>(&mut self, name: &str, len: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a struct value. Read more
Source§

fn emit_struct_field<F>( &mut self, f_name: &str, _f_idx: usize, f: F, ) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a field item for a struct. Read more
Source§

fn emit_tuple<F>(&mut self, len: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a tuple value. Read more
Source§

fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a data item for a tuple. Read more
Source§

fn emit_tuple_struct<F>( &mut self, _name: &str, len: usize, f: F, ) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a tuple struct value. Read more
Source§

fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a data item for a tuple struct. Read more
Source§

fn emit_option<F>(&mut self, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit an optional value. Read more
Source§

fn emit_option_none(&mut self) -> CborResult<()>

Emit the None optional value. Read more
Source§

fn emit_option_some<F>(&mut self, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit the Some(x) optional value. Read more
Source§

fn emit_seq<F>(&mut self, len: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit a sequence of values. Read more
Source§

fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit an element in a sequence. Read more
Source§

fn emit_map<F>(&mut self, len: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit an associative container (map). Read more
Source§

fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit the key for an entry in a map. Read more
Source§

fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> CborResult<()>
where F: FnOnce(&mut Encoder<W>) -> CborResult<()>,

Emit the value for an entry in a map. 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> 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<S> EncoderHelpers for S
where S: Encoder,

Source§

fn emit_from_vec<T, F>( &mut self, v: &[T], f: F, ) -> Result<(), <S as Encoder>::Error>
where F: FnMut(&mut S, &T) -> Result<(), <S as Encoder>::Error>,

Emit a vector as a sequence. 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.