[go: up one dir, main page]

cbor

Struct Decoder

Source
pub struct Decoder<R> { /* private fields */ }
Expand description

Read CBOR data items into Rust values from the underlying reader R.

Implementations§

Source§

impl<R: Read> Decoder<R>

Source

pub fn from_reader(rdr: R) -> Decoder<BufReader<R>>

Create a new CBOR decoder from the underlying reader.

Source§

impl<R: Read> Decoder<R>

Source

pub fn decode<D: Decodable>(&mut self) -> DecodedItems<'_, R, D>

Decode a sequence of top-level CBOR data items into Rust values.

§Example

This shows how to encode and decode a sequence of data items:

use cbor::{Decoder, Encoder};

let data = vec![("a".to_string(), 1), ("b".to_string(), 2),
                ("c".to_string(), 3)];

let mut enc = Encoder::from_memory();
enc.encode(&data).unwrap();

let mut dec = Decoder::from_bytes(enc.as_bytes());
let items: Vec<(String, i32)> = dec.decode()
                                   .collect::<Result<_, _>>()
                                   .unwrap();

assert_eq!(items, data);
Source

pub fn items(&mut self) -> Items<'_, R>

Read a sequence of top-level CBOR data items.

This yields data items represented by the Cbor type, which is its abstract syntax. (Using the decode iterator is probably much more convenient, but this is useful when you need to do more sophisticated analysis on the CBOR data.)

§Example

This shows how to encode and decode a sequence of data items:

use cbor::{Cbor, CborUnsigned, Decoder, Encoder};

let mut enc = Encoder::from_memory();
enc.encode(vec![("a", 1), ("b", 2), ("c", 3)]).unwrap();

let mut dec = Decoder::from_bytes(enc.as_bytes());
let items = dec.items().collect::<Result<Vec<_>, _>>().unwrap();

assert_eq!(items, vec![
    Cbor::Array(vec![
        Cbor::Unicode("a".to_string()),
        Cbor::Unsigned(CborUnsigned::UInt8(1)),
    ]),
    Cbor::Array(vec![
        Cbor::Unicode("b".to_string()),
        Cbor::Unsigned(CborUnsigned::UInt8(2)),
    ]),
    Cbor::Array(vec![
        Cbor::Unicode("c".to_string()),
        Cbor::Unsigned(CborUnsigned::UInt8(3)),
    ]),
]);
Source§

impl Decoder<Cursor<Vec<u8>>>

Source

pub fn from_bytes<T>(bytes: T) -> Decoder<Cursor<Vec<u8>>>
where T: Into<Vec<u8>>,

Create a new CBOR decoder that reads from the buffer given.

The buffer is usually given as either a Vec<u8> or a &[u8].

Auto Trait Implementations§

§

impl<R> Freeze for Decoder<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Decoder<R>
where R: RefUnwindSafe,

§

impl<R> Send for Decoder<R>
where R: Send,

§

impl<R> Sync for Decoder<R>
where R: Sync,

§

impl<R> Unpin for Decoder<R>
where R: Unpin,

§

impl<R> UnwindSafe for Decoder<R>
where R: 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.