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>
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 end(&self) -> Result<(), Error>
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.
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 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.
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 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]);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 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).