pub struct Deserializer<'a> { /* private fields */ }Expand description
Deserializes a deserializable from CBOR.
A deserializer reads data items from a slice. Because CBOR sequences (RFC 8742) are just data items following each other, a deserializer can be used to read more than one item:
use deser_cbor::Deserializer;
let mut de = Deserializer::from_slice(&[0x01, 0x62, b'h', b'i']);
assert_eq!(de.deserialize::<u32>().unwrap(), 1);
assert_eq!(de.deserialize::<String>().unwrap(), "hi");
assert!(de.is_end());To deserialize a single data item, use from_slice
(or the method of the same name on DeserializerConfig).
Implementations§
Source§impl<'a> Deserializer<'a>
impl<'a> Deserializer<'a>
Sourcepub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
pub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
Creates a new deserializer for a byte slice.
Sourcepub fn from_slice_with_config(
input: &'a [u8],
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_slice_with_config( input: &'a [u8], config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a byte slice with the given configuration.
Sourcepub fn config(&self) -> &DeserializerConfig
pub fn config(&self) -> &DeserializerConfig
Returns the configuration.
Sourcepub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
Deserializes the next data item.
This does not check if there is more data after the item. Use
end for this or from_slice which does it
automatically.
To configure the deserialization (for instance to add layers) use
deserialize_with.
Sourcepub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
Deserializes the next value with a configured driver.
The callback is invoked with the driver before the value is
deserialized, for instance to add Layers.
Sourcepub fn iter<T: Deserialize<'a>>(&mut self) -> Iter<'_, 'a, T> ⓘ
pub fn iter<T: Deserialize<'a>>(&mut self) -> Iter<'_, 'a, T> ⓘ
Returns an iterator over the remaining data items.
This is useful to read CBOR sequences. The iterator stops after the first error.
let mut de = deser_cbor::Deserializer::from_slice(&[0x01, 0x02, 0x03]);
let items = de.iter::<u32>().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(items, [1, 2, 3]);Sourcepub fn drive(
&mut self,
driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<(), Error>
pub fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'a>, ) -> Result<(), Error>
Parses the next data item and feeds the events into the given driver.
This is useful to deserialize into a custom
Sink or to wrap the sink of a value.
Definite length strings and byte strings are passed on borrowed from
the input (see
emit_borrowed). The byte
ranges of the data items are published as input ranges (see
State::input_range) and errors carry
the offset in the input (see Error::offset).
The context of the configuration is given to the driver (values that
the context of the driver has take precedence, see
DeserializeDriver::set_default_context).