Skip to main content

OwnedSink

Struct OwnedSink 

Source
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>

Source

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>

Source

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.

Source§

impl<'de, T> OwnedSink<'de, T>

Source

pub fn get(&self) -> &(dyn Sink<'de> + '_)

Returns a reference to the sink.

Source

pub fn get_mut(&mut self) -> &mut (dyn Sink<'de> + '_)

Returns a mutable reference to the sink.

Source

pub fn take(&mut self) -> Option<T>

Takes the value produced by the sink.

This finishes the use of the sink. After calling this method the sink will drop all values it receives.

Trait Implementations§

Source§

impl<'de, T> Drop for OwnedSink<'de, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'de, T> !RefUnwindSafe for OwnedSink<'de, T>

§

impl<'de, T> !Sync for OwnedSink<'de, T>

§

impl<'de, T> !Unpin for OwnedSink<'de, T>

§

impl<'de, T> !UnwindSafe for OwnedSink<'de, T>

§

impl<'de, T> Freeze for OwnedSink<'de, T>
where ArenaBox<Option<T>>: Freeze,

§

impl<'de, T> Send for OwnedSink<'de, T>
where ArenaBox<Option<T>>: Send,

§

impl<'de, T> UnsafeUnpin for OwnedSink<'de, T>
where ArenaBox<Option<T>>: UnsafeUnpin,

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.