Skip to main content

Deserializer

Struct Deserializer 

Source
pub struct Deserializer<'a> { /* private fields */ }
Expand description

Deserializes values of PHP’s serialization format.

A deserializer reads values from a slice. PHP reads a single value but values can be concatenated, so a deserializer can be used to read more than one:

use deser_php::Deserializer;

let mut de = Deserializer::from_slice(b"i:1;s:2:\"hi\";");
assert_eq!(de.deserialize::<u32>().unwrap(), 1);
assert_eq!(de.deserialize::<String>().unwrap(), "hi");
assert!(de.is_end());

To deserialize a single value, use from_slice (or the method of the same name on DeserializerConfig).

Implementations§

Source§

impl<'a> Deserializer<'a>

Source

pub fn from_slice(input: &'a [u8]) -> Deserializer<'a>

Creates a new deserializer for a byte slice.

Source

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.

Source

pub fn config(&self) -> &DeserializerConfig

Returns the configuration.

Source

pub fn offset(&self) -> usize

Returns the current offset in the input.

Source

pub fn is_end(&self) -> bool

Returns true if the entire input was consumed.

Source

pub fn end(&self) -> Result<(), Error>

Fails if the input was not consumed entirely.

Unlike PHP (which ignores data after the value with a warning) the functions that deserialize a single value reject it.

Source

pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>

Deserializes the next value.

This does not check if there is more data after the value. Use end for this or from_slice which does it automatically.

To configure the deserialization (for instance to add layers) use deserialize_with.

Source

pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
where T: Deserialize<'a>, F: FnOnce(&mut DeserializeDriver<'_, 'a>),

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.

Source

pub fn iter<T: Deserialize<'a>>(&mut self) -> Iter<'_, 'a, T> ⓘ

Returns an iterator over the remaining values.

The iterator stops after the first error.

let mut de = deser_php::Deserializer::from_slice(b"i:1;i:2;i:3;");
let items = de.iter::<u32>().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(items, [1, 2, 3]);
Source

pub fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'a>, ) -> Result<(), Error>

Parses the next value 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.

The value is validated before the first event is emitted. Strings are passed on borrowed from the input. The byte ranges of the values 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).

Trait Implementations§

Source§

impl<'a> Deserializer<'a> for Deserializer<'a>

Source§

fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'a>) -> Result<(), Error>

Parses the input and feeds the events of a value into the driver.
Source§

fn deserialize<T>(&mut self) -> Result<T, Error>
where T: Deserialize<'de>, Self: Sized,

Deserializes a value.
Source§

fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Deserializes a value with a configured driver. Read more
Source§

fn update<T>(&mut self, value: &mut T) -> Result<(), Error>
where T: Deserialize<'de>, Self: Sized,

Updates an existing value with the next value. Read more
Source§

fn update_with<T, F>(&mut self, value: &mut T, setup: F) -> Result<(), Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Updates an existing value with the next value after setting up the driver. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Deserializer<'a>

§

impl<'a> RefUnwindSafe for Deserializer<'a>

§

impl<'a> Send for Deserializer<'a>

§

impl<'a> Sync for Deserializer<'a>

§

impl<'a> Unpin for Deserializer<'a>

§

impl<'a> UnsafeUnpin for Deserializer<'a>

§

impl<'a> UnwindSafe for Deserializer<'a>

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.