Skip to main content

RecordBuf

Struct RecordBuf 

Source
pub struct RecordBuf<'de> { /* private fields */ }
Expand description

A recording which keeps borrowed data borrowed.

This is like Recording, but atoms that are delivered borrowed (see Sink::borrowed_atom) are recorded as they are and replayed or serialized borrowed, all others are recorded as owned. Strings that the format can lend from the input are not copied, but the buffer cannot outlive the input. The derive uses it to buffer values, which allows types that borrow to be deserialized from buffered values (for instance the fields of internally tagged enums that come before the tag).

Like recordings, record buffers implement Deserialize and Serialize. This makes them useful to pass values from a deserializer to a serializer, for instance to convert them from one format to another (which is what deser-transcode does):

use deser::de::{DeserializeDriver, RecordBuf};
use deser::ser::SerializeDriver;
use deser::Event;

let input = String::from("borrowed");
let mut buf = RecordBuf::new();
{
    let mut driver = DeserializeDriver::from_fn(|state| buf.recorder(state));
    driver.emit(Event::seq_start()).unwrap();
    driver.emit_borrowed(input.as_str()).unwrap();
    driver.emit(Event::SeqEnd).unwrap();
}

let mut events = Vec::new();
SerializeDriver::new(&buf)
    .drive(|event, _state| {
        events.push(event.to_static());
        Ok(())
    })
    .unwrap();
// the length of the sequence is known once it was recorded
assert_eq!(
    events,
    [
        Event::SeqStart(deser::ContainerShape::with_len(1)),
        "borrowed".into(),
        Event::SeqEnd,
    ]
);

Implementations§

Source§

impl<'de> RecordBuf<'de>

Source

pub fn new() -> RecordBuf<'de>

Creates an empty buffer.

Source

pub fn recorder(&mut self, state: &mut State) -> SinkHandle<'_, 'de>

Returns a sink that records a value into this buffer.

A previously recorded value is discarded.

Source

pub fn capture<'a, F>(then: F, state: &mut State) -> SinkHandle<'a, 'de>
where F: FnOnce(RecordBuf<'de>, &mut State) -> Result<(), Error> + Send + 'a, 'de: 'a,

Returns a sink that records a value and passes the buffer to a callback once the value is complete (see Recording::capture).

Source

pub fn is_empty(&self) -> bool

Returns true if nothing was recorded.

Source

pub fn as_str(&self) -> Option<&str>

Returns the value if the buffer is a single string.

Source

pub fn replay<'a>( &self, sink: SinkHandle<'_, 'a>, state: &mut State, ) -> Result<(), Error>
where 'de: 'a,

Replays the recorded value into a sink.

Atoms that were delivered borrowed are replayed borrowed.

Trait Implementations§

Source§

impl<'de> Clone for RecordBuf<'de>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RecordBuf<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Default for RecordBuf<'de>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for RecordBuf<'de>

Record buffers deserialize like Recordings but keep borrowed atoms borrowed.

Source§

fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>

Creates a sink that deserializes the value into the given slot. Read more
Source§

fn expecting() -> Cow<'static, str>

Returns what the value expects, for error messages. Read more
Source§

fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom into the slot. Read more
Source§

fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

fn describe_type(d: &mut dyn Describe)

Describes the Rust shape of the type. Read more
Source§

fn initial_value() -> Option<T>

Provides the value of a missing struct field. Read more
Source§

fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>

Creates a sink that updates an existing value. Read more
Source§

impl Serialize for RecordBuf<'_>

Record buffers serialize like Recordings, borrowed atoms are serialized without copying them.

Source§

fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn container_shape(value: &Self) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

fn is_optional(value: &Self) -> bool

Checks if the value represents an optional value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more

Auto Trait Implementations§

§

impl<'de> !RefUnwindSafe for RecordBuf<'de>

§

impl<'de> !UnwindSafe for RecordBuf<'de>

§

impl<'de> Freeze for RecordBuf<'de>

§

impl<'de> Send for RecordBuf<'de>

§

impl<'de> Sync for RecordBuf<'de>

§

impl<'de> Unpin for RecordBuf<'de>

§

impl<'de> UnsafeUnpin for RecordBuf<'de>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.