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>
impl<R: Read> Decoder<R>
Sourcepub fn from_reader(rdr: R) -> Decoder<BufReader<R>>
pub fn from_reader(rdr: R) -> Decoder<BufReader<R>>
Create a new CBOR decoder from the underlying reader.
Source§impl<R: Read> Decoder<R>
impl<R: Read> Decoder<R>
Sourcepub fn decode<D: Decodable>(&mut self) -> DecodedItems<'_, R, D> ⓘ
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);Sourcepub fn items(&mut self) -> Items<'_, R> ⓘ
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)),
]),
]);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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more