pub struct OwnedSink<'de, T> { /* private fields */ }Expand description
Utility to bundle a sink with a slot.
There are situations where one wants to deserialize into a slot that
does not exist yet (for instance the value inside a wrapper, which is
only built once the value is complete) and hold the slot together with
the sink that borrows it. Rust’s lifetimes make this impossible so this
abstraction is provided to allow this. The slot and the sink are
allocated in the arena of the state, the value is taken out with
take.
§Example
This example demonstrates the use of an OwnedSink to implement
Deserialize for a newtype wrapper. For simplicity’s sake only
atoms have been implemented here.
use deser::{Atom, Error};
use deser::de::{OwnedSink, SinkHandle, Sink, Deserialize};
use deser::State;
struct AtomWrapper<T>(T);
impl<'de, T: Deserialize<'de>> Deserialize<'de> for AtomWrapper<T> {
fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
SinkHandle::arena(
WrapperSink { out, sink: OwnedSink::deserialize(state) },
state,
)
}
}
struct WrapperSink<'a, 'de, T> {
out: &'a mut Option<AtomWrapper<T>>,
sink: OwnedSink<'de, T>,
}
impl<'a, 'de, T: Deserialize<'de>> Sink<'de> for WrapperSink<'a, 'de, T> {
fn atom(
&mut self,
atom: Atom,
state: &mut State,
) -> Result<(), Error> {
self.sink.get_mut().atom(atom, state)
}
fn finish(&mut self, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().finish(state)?;
*self.out = self.sink.take().map(AtomWrapper);
Ok(())
}
}Implementations§
Source§impl<'de, T: Deserialize<'de>> OwnedSink<'de, T>
impl<'de, T: Deserialize<'de>> OwnedSink<'de, T>
Sourcepub fn deserialize(state: &mut State) -> OwnedSink<'de, T>
pub fn deserialize(state: &mut State) -> OwnedSink<'de, T>
Creates a new owned sink for a given type.
This begins the deserialization with Deserialize::deserialize_into
into a slot contained within the owned sink. To extract the final
value use take.
The sink is allocated in the arena of the state (see
SinkHandle::arena). If the owned sink outlives the
deserialization, the chunk of the arena it’s in is freed when it’s
dropped.
Source§impl<'de, T: Send> OwnedSink<'de, T>
impl<'de, T: Send> OwnedSink<'de, T>
Sourcepub fn deserialize_as<A: Deserialize<'de, T>>(
state: &mut State,
) -> OwnedSink<'de, T>
pub fn deserialize_as<A: Deserialize<'de, T>>( state: &mut State, ) -> OwnedSink<'de, T>
Creates a new owned sink that deserializes with an adapter.
This is like deserialize but begins the
deserialization with
Deserialize::deserialize_into of the adapter A.