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>
impl<'de> RecordBuf<'de>
Sourcepub fn recorder(&mut self, state: &mut State) -> SinkHandle<'_, 'de>
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.
Sourcepub fn capture<'a, F>(then: F, state: &mut State) -> SinkHandle<'a, 'de>
pub fn capture<'a, F>(then: F, state: &mut State) -> SinkHandle<'a, 'de>
Returns a sink that records a value and passes the buffer to a
callback once the value is complete (see Recording::capture).
Trait Implementations§
Source§impl<'de> Deserialize<'de> for RecordBuf<'de>
Record buffers deserialize like Recordings but keep borrowed atoms
borrowed.
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>
fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Source§impl Serialize for RecordBuf<'_>
Record buffers serialize like Recordings, borrowed atoms are
serialized without copying them.
impl Serialize for RecordBuf<'_>
Record buffers serialize like Recordings, borrowed atoms are
serialized without copying them.