Skip to main content

deser_core/ext/
raw.rs

1use alloc::borrow::Cow;
2use alloc::vec::Vec;
3use core::any::Any;
4use core::fmt;
5use core::marker::PhantomData;
6
7use crate::State;
8use crate::adapters::Borrowed;
9use crate::de::recording::Capture;
10use crate::de::{Deserialize, DeserializeDriver, RecordBuf, Sink, SinkHandle};
11use crate::error::{Error, ErrorKind};
12use crate::event::Atom;
13use crate::ext::{BorrowedExtension, ExtValue};
14use crate::ser::{Emit, Serialize, SerializeHandle, SerializeRef};
15
16/// A data format whose encoded values can be held by [`Raw`].
17///
18/// This is implemented by a type of the crate of the format that stands
19/// for the format (for instance `deser_json::Json`).  The format is
20/// described at runtime by a [`RawFormatInfo`].
21///
22/// # Passing on the Input of Values
23///
24/// Raw values of a format that is deserialized from the same format hold
25/// the input of the value, and serializers of the format write them as
26/// they are.  For this the format:
27///
28/// * calls [`State::declare_raw_format`](crate::State::declare_raw_format) with
29///   its [`RawFormatId`] in the deserializer (before the first event, it
30///   returns the description of the format if the top-level value is
31///   wanted as raw value) and in the serializer (before the first value).
32/// * checks with [`Error::is_raw_request`](crate::Error::is_raw_request)
33///   whether the result of an event requests the next value as raw value
34///   and takes the description of the format with
35///   [`State::take_raw_request`](crate::State::take_raw_request).
36/// * validates a value that is wanted as raw value and emits its input as
37///   [`RawInput`] (an [`Atom::Ext`]) with that description rather than
38///   its events.
39/// * writes the [`RawInput`] of its format as it is when serializing
40///   (see [`RawInput::is_format`]).
41///
42/// The parser and the serializer only refer to the [`RawFormatId`]: the
43/// functions of the format (like `replay`, which brings in its parser)
44/// are only in programs that use its raw values.
45///
46/// Formats that do not do this still have raw values: the values are
47/// encoded with the format then.
48pub trait RawFormat: 'static {
49    /// Returns the description of the format.
50    ///
51    /// This must always return the same static: formats are identified by
52    /// the address of their description.
53    fn info() -> &'static RawFormatInfo;
54}
55
56/// A [`RawFormat`] whose encoding is text.
57///
58/// [`Raw`] values of such formats can be accessed as strings (see
59/// [`Raw::get`]).
60///
61/// # Safety
62///
63/// The format must be described as text (see [`RawFormatId::new`]): its
64/// parser only passes on and its encoder only produces valid UTF-8.
65pub unsafe trait TextRawFormat: RawFormat {}
66
67/// Identifies a [`RawFormat`].
68///
69/// Formats are identified by the address of a static of this type.  Unlike
70/// the [`RawFormatInfo`] of the format it holds no functions: formats
71/// declare which raw values they pass on with it (see
72/// [`State::declare_raw_format`](crate::State::declare_raw_format)), so a program
73/// only contains the functions of the format (like its parser for
74/// `replay`) if it uses its raw values.
75pub struct RawFormatId {
76    name: &'static str,
77    is_text: bool,
78}
79
80impl RawFormatId {
81    /// Creates the identity of a format.
82    ///
83    /// * `name` is the name of the format (like `"json"`).
84    /// * `is_text` is `true` if the encoding is text.  The encoded values
85    ///   must then be valid UTF-8.
86    pub const fn new(name: &'static str, is_text: bool) -> RawFormatId {
87        RawFormatId { name, is_text }
88    }
89
90    /// Returns the name of the format.
91    pub fn name(&self) -> &'static str {
92        self.name
93    }
94
95    /// Returns `true` if the encoding of the format is text.
96    pub fn is_text(&self) -> bool {
97        self.is_text
98    }
99}
100
101impl fmt::Debug for RawFormatId {
102    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103        f.debug_tuple("RawFormatId").field(&self.name).finish()
104    }
105}
106
107/// Describes a [`RawFormat`] at runtime.
108///
109/// Formats that can pass on the input of values define a static of this
110/// type.  It travels with the input of values (see [`RawInput`]) so that
111/// code which does not know the format can still parse it.  Formats are
112/// identified by their [`RawFormatId`].
113pub struct RawFormatInfo {
114    id: &'static RawFormatId,
115    replay: for<'de> fn(&'de [u8], &mut DeserializeDriver<'_, 'de>) -> Result<(), Error>,
116    encode: fn(SerializeRef<'_>) -> Result<Vec<u8>, Error>,
117    fallback: for<'v> fn(&'v [u8]) -> Atom<'v>,
118    data: Option<&'static (dyn Any + Send + Sync)>,
119}
120
121impl RawFormatInfo {
122    /// Creates the description of a format.
123    ///
124    /// * `id` identifies the format.
125    /// * `replay` parses a value and emits its events into the driver.
126    /// * `encode` encodes a value.
127    /// * `fallback` returns the fallback atom of an encoded value (see
128    ///   [`Extension::fallback`](crate::ext::Extension::fallback)).  It
129    ///   must be [`Atom::Null`] for null, so that optionals are `None` for
130    ///   it, and must not be an extension value.
131    pub const fn new(
132        id: &'static RawFormatId,
133        replay: for<'de> fn(&'de [u8], &mut DeserializeDriver<'_, 'de>) -> Result<(), Error>,
134        encode: fn(SerializeRef<'_>) -> Result<Vec<u8>, Error>,
135        fallback: for<'v> fn(&'v [u8]) -> Atom<'v>,
136    ) -> RawFormatInfo {
137        RawFormatInfo {
138            id,
139            replay,
140            encode,
141            fallback,
142            data: None,
143        }
144    }
145
146    /// Attaches data of the format to the description.
147    ///
148    /// The format gets it back from the description of raw values that are
149    /// requested (see [`data`](Self::data)).  Formats keep what only
150    /// programs that use their raw values need here (like the scanner of
151    /// raw values in the parser): as only raw values refer to the
152    /// description, other programs do not contain it.
153    pub const fn set_data(&mut self, data: &'static (dyn Any + Send + Sync)) {
154        self.data = Some(data);
155    }
156
157    /// Returns the data of the format (see [`set_data`](Self::set_data)).
158    pub fn data(&self) -> Option<&'static (dyn Any + Send + Sync)> {
159        self.data
160    }
161
162    /// Returns the identity of the format.
163    pub fn id(&self) -> &'static RawFormatId {
164        self.id
165    }
166
167    /// Returns the name of the format.
168    pub fn name(&self) -> &'static str {
169        self.id.name
170    }
171
172    /// Returns `true` if the encoding of the format is text.
173    pub fn is_text(&self) -> bool {
174        self.id.is_text
175    }
176
177    /// Records an encoded value.
178    fn record<'a>(&self, bytes: &'a [u8]) -> Result<RecordBuf<'a>, Error> {
179        let mut recording = RecordBuf::new();
180        {
181            let mut driver = DeserializeDriver::from_fn(|state| recording.recorder(state));
182            (self.replay)(bytes, &mut driver)?;
183        }
184        Ok(recording)
185    }
186}
187
188impl fmt::Debug for RawFormatInfo {
189    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190        f.debug_tuple("RawFormatInfo").field(&self.id.name).finish()
191    }
192}
193
194/// Returns `true` if two descriptions are the same format.
195#[inline(always)]
196fn same_format(a: &'static RawFormatInfo, b: &'static RawFormatInfo) -> bool {
197    core::ptr::eq(a.id, b.id)
198}
199
200/// The encoded input of a value.
201///
202/// This is a well-known borrowing extension (see [`ext`](crate::ext)) which
203/// carries the encoding of a value between formats and [`Raw`] values:
204///
205/// * formats emit it for values that are requested as raw values (the
206///   values of [`Raw`] types).  They validate the value and pass on its
207///   input instead of its events.
208/// * [`Raw`] values emit it when they are serialized and the serializer
209///   writes the format as it is (see [`State::declare_raw_format`]).
210///
211/// It knows its format, so it's parsed into its value where it ends up in
212/// something that does not know it (for instance when a recording that
213/// holds it is serialized).
214#[derive(Clone)]
215pub struct RawInput<'a> {
216    bytes: Cow<'a, [u8]>,
217    format: &'static RawFormatInfo,
218}
219
220impl<'a> RawInput<'a> {
221    /// Creates the input of a value.
222    ///
223    /// # Safety
224    ///
225    /// The input must be a single, valid value of the format.  If the
226    /// format is text, it must be valid UTF-8.  Serializers of the format
227    /// write the input as it is.
228    pub unsafe fn new<B: Into<Cow<'a, [u8]>>>(
229        bytes: B,
230        format: &'static RawFormatInfo,
231    ) -> RawInput<'a> {
232        RawInput {
233            bytes: bytes.into(),
234            format,
235        }
236    }
237
238    /// Returns the encoded value.
239    pub fn as_bytes(&self) -> &[u8] {
240        &self.bytes
241    }
242
243    /// Returns the encoded value as text if the format is text.
244    pub fn as_str(&self) -> Option<&str> {
245        // SAFETY: the input of text formats is valid UTF-8, see `new`
246        self.format
247            .is_text()
248            .then(|| unsafe { core::str::from_utf8_unchecked(&self.bytes) })
249    }
250
251    /// Returns the format of the value.
252    pub fn format(&self) -> &'static RawFormatInfo {
253        self.format
254    }
255
256    /// Returns `true` if the value is of the format `F`.
257    pub fn is<F: RawFormat>(&self) -> bool {
258        same_format(self.format, F::info())
259    }
260
261    /// Returns `true` if the value is of the format with the identity.
262    ///
263    /// Serializers check with this whether they write a value as it is
264    /// (unlike [`is`](Self::is), this does not refer to the functions of
265    /// the format).
266    pub fn is_format(&self, id: &'static RawFormatId) -> bool {
267        core::ptr::eq(self.format.id, id)
268    }
269
270    /// Detaches the input from the data it borrows.
271    pub fn into_owned(self) -> RawInput<'static> {
272        RawInput {
273            bytes: Cow::Owned(self.bytes.into_owned()),
274            format: self.format,
275        }
276    }
277
278    /// Records the value.
279    pub fn record(&self) -> Result<RecordBuf<'_>, Error> {
280        self.format.record(&self.bytes)
281    }
282
283    /// Replays the value into a sink.
284    ///
285    /// The value is parsed, the sink can borrow its data.
286    pub fn replay<'x>(&'x self, sink: SinkHandle<'_, 'x>, state: &mut State) -> Result<(), Error> {
287        // the type of the sink is unknown, it does not want a raw value
288        self.replay_raw(sink, None, state)
289    }
290
291    /// Replays the value, `raw` is the raw value the sink wants.
292    fn replay_raw<'x>(
293        &'x self,
294        sink: SinkHandle<'_, 'x>,
295        raw: Option<&'static RawFormatInfo>,
296        state: &mut State,
297    ) -> Result<(), Error> {
298        DeserializeDriver::nested(state, sink, false, |driver| {
299            // the top-level value is requested before it starts
300            driver.state_mut().raw_requested = raw;
301            (self.format.replay)(&self.bytes, driver)
302        })
303    }
304
305    /// Deserializes the value.
306    ///
307    /// The value can borrow from the input (and the data it borrows).
308    pub fn deserialize<'x, T: Deserialize<'x>>(&'x self) -> Result<T, Error> {
309        // the driver requests the value as raw value if `T` wants one
310        crate::de::deserialize_value(|driver| (self.format.replay)(&self.bytes, driver))
311    }
312}
313
314/// Parses the input of a raw value into a sink that does not accept it.
315///
316/// The sink is finished by the caller (see `NoFinish`).
317pub(crate) fn parse_into<'de>(
318    input: &RawInput<'_>,
319    sink: &mut (dyn Sink<'de> + '_),
320    state: &mut State,
321) -> Result<(), Error> {
322    let mut sink = NoFinish(sink);
323    DeserializeDriver::nested(state, SinkHandle::to(&mut sink), false, |driver| {
324        driver.state_mut().raw_requested = None;
325        // the input lives shorter than the data of the sink
326        driver.transient(|driver| (input.format.replay)(&input.bytes, driver))
327    })
328}
329
330/// Forwards to a sink except for [`Sink::finish`].
331///
332/// This is used to parse a value into a sink that received an atom (see
333/// `parse_into`): the replay finishes the value, the sink is finished by
334/// the code that delivered the atom.
335struct NoFinish<'a, 'b, 'de>(&'a mut (dyn Sink<'de> + 'b));
336
337impl<'de> Sink<'de> for NoFinish<'_, '_, 'de> {
338    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
339        self.0.atom(atom, state)
340    }
341
342    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
343        self.0.borrowed_atom(atom, state)
344    }
345
346    fn map(&mut self, state: &mut State) -> Result<(), Error> {
347        self.0.map(state)
348    }
349
350    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
351        self.0.seq(state)
352    }
353
354    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
355        self.0.next_key(state)
356    }
357
358    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
359        self.0.next_value(state)
360    }
361
362    fn value_for_key(
363        &mut self,
364        key: &str,
365        state: &mut State,
366    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
367        self.0.value_for_key(key, state)
368    }
369
370    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
371        self.0.recover(err, state)
372    }
373
374    fn expecting(&self) -> Cow<'_, str> {
375        self.0.expecting()
376    }
377
378    fn finish(&mut self, _state: &mut State) -> Result<(), Error> {
379        Ok(())
380    }
381}
382
383impl fmt::Debug for RawInput<'_> {
384    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
385        let mut debug = f.debug_struct("RawInput");
386        debug.field("format", &self.format.id.name);
387        match self.as_str() {
388            Some(text) => debug.field("text", &text),
389            None => debug.field("bytes", &self.bytes),
390        };
391        debug.finish()
392    }
393}
394
395impl PartialEq for RawInput<'_> {
396    fn eq(&self, other: &Self) -> bool {
397        same_format(self.format, other.format) && self.bytes == other.bytes
398    }
399}
400
401impl BorrowedExtension for RawInput<'static> {
402    type Value<'a> = RawInput<'a>;
403
404    fn name<'v>(_value: &'v RawInput<'_>) -> &'v str {
405        "raw value"
406    }
407
408    fn fallback<'v>(value: &'v RawInput<'_>) -> Atom<'v> {
409        (value.format.fallback)(&value.bytes)
410    }
411
412    fn to_static(value: &RawInput<'_>) -> RawInput<'static> {
413        value.clone().into_owned()
414    }
415
416    fn shorten<'s, 'l: 's>(value: &'s RawInput<'l>) -> &'s RawInput<'s> {
417        value
418    }
419}
420
421/// Serializes the input of a value.
422///
423/// The input is passed on as extension value if the serializer writes its
424/// format as it is, otherwise the value is parsed and serialized.
425fn serialize_input<'a>(input: &'a RawInput<'_>, state: &mut State) -> Result<Emit<'a>, Error> {
426    if state.accepts_raw(input.format) {
427        return Ok(Emit::Atom(Atom::Ext(ExtValue::borrowed_value::<RawInput>(
428            input,
429        ))));
430    }
431    Ok(Emit::Forward(SerializeHandle::arena(
432        input.record()?,
433        state,
434    )))
435}
436
437/// Serializes an atom of a recording.
438///
439/// The input of values that the serializer does not write as it is is
440/// parsed and serialized.
441pub(crate) fn serialize_recorded_atom<'a>(
442    atom: &'a Atom<'_>,
443    state: &mut State,
444) -> Result<Emit<'a>, Error> {
445    if let Atom::Ext(ext) = atom
446        && let Some(input) = ext.downcast_value_ref::<RawInput>()
447    {
448        return serialize_input(input, state);
449    }
450    Ok(Emit::Atom(atom.as_borrowed()))
451}
452
453/// A value encoded in the format `F`.
454///
455/// A raw value holds a value in the encoding of a format, for instance
456/// JSON text with `deser_json::RawJson`.  It can be deserialized later
457/// (with [`deserialize`](Self::deserialize)), stored or written out again
458/// unchanged:
459///
460/// * If it's deserialized from the format `F`, it holds the input of the
461///   value as it is.  The format only validates the value and does not
462///   produce its events, which is fast.  With the
463///   [`Borrowed`] adapter the input is
464///   borrowed.
465/// * Otherwise (from another format or where the format cannot pass on
466///   the input, see below) the value is encoded in the format `F`.  The
467///   encoding can lose information that `F` cannot express (bytes in JSON
468///   for instance).  To keep a value of any format without interpreting
469///   it, use [`Recording`](crate::de::Recording).
470///
471/// When serialized with the format `F`, the encoded value is written as it
472/// is.  Other formats serialize the value it holds.
473///
474/// The input of values can only be passed on if the format knows before
475/// the value starts that a raw value is wanted.  Derived structs (for their
476/// first 64 fields), maps, sequences, `Option` and `Box` ask for it.
477/// Values in other places (like the fields of flattened structs, the
478/// variants of enums and values that are buffered, for instance for
479/// untagged enums) are encoded.
480///
481/// Like `Cow`, raw values are deserialized owned, so a `Raw<'static, F>`
482/// can be deserialized from any data.
483pub struct Raw<'a, F: RawFormat> {
484    input: RawInput<'a>,
485    _format: PhantomData<fn() -> F>,
486}
487
488impl<'a, F: RawFormat> Raw<'a, F> {
489    /// Creates a raw value from its encoding.
490    ///
491    /// The value is validated.
492    pub fn new<B: Into<Cow<'a, [u8]>>>(bytes: B) -> Result<Raw<'a, F>, Error> {
493        let bytes = bytes.into();
494        let info = F::info();
495        if info.is_text() && core::str::from_utf8(&bytes).is_err() {
496            return Err(Error::new(ErrorKind::Syntax, "invalid utf-8"));
497        }
498        {
499            let mut driver = DeserializeDriver::from_fn(|_| SinkHandle::null());
500            (info.replay)(&bytes, &mut driver)?;
501        }
502        // SAFETY: the value was validated
503        Ok(Raw::from_input(unsafe { RawInput::new(bytes, info) }))
504    }
505
506    /// Encodes a value.
507    pub fn encode<T: Serialize + ?Sized>(value: &T) -> Result<Raw<'static, F>, Error> {
508        let info = F::info();
509        let bytes = (info.encode)(SerializeRef::new(&value))?;
510        if info.is_text() && core::str::from_utf8(&bytes).is_err() {
511            return Err(Error::new(
512                ErrorKind::InvalidState,
513                "the encoding of a text format is not utf-8",
514            ));
515        }
516        // SAFETY: the encoder produces a valid value
517        Ok(Raw::from_input(unsafe { RawInput::new(bytes, info) }))
518    }
519
520    fn from_input(input: RawInput<'a>) -> Raw<'a, F> {
521        debug_assert!(input.is::<F>());
522        Raw {
523            input,
524            _format: PhantomData,
525        }
526    }
527
528    /// Returns the encoded value.
529    pub fn as_bytes(&self) -> &[u8] {
530        &self.input.bytes
531    }
532
533    /// Returns `true` if the value borrows from the data it was
534    /// deserialized from.
535    pub fn is_borrowed(&self) -> bool {
536        matches!(self.input.bytes, Cow::Borrowed(_))
537    }
538
539    /// Replays the value into a sink.
540    ///
541    /// The value is parsed, the sink can borrow its data.
542    pub fn replay<'x>(&'x self, sink: SinkHandle<'_, 'x>, state: &mut State) -> Result<(), Error> {
543        self.input.replay(sink, state)
544    }
545
546    /// Deserializes the value.
547    ///
548    /// The value can borrow from the raw value (and the data it borrows).
549    pub fn deserialize<'x, T: Deserialize<'x>>(&'x self) -> Result<T, Error> {
550        self.input.deserialize()
551    }
552
553    /// Detaches the value from the data it borrows.
554    pub fn into_owned(self) -> Raw<'static, F> {
555        Raw::from_input(self.input.into_owned())
556    }
557
558    /// Creates a raw value from an atom that was delivered for it.
559    #[inline(never)]
560    fn from_atom(atom: Atom<'_>, state: &State) -> Result<Raw<'static, F>, Error> {
561        if let Atom::Ext(ref ext) = atom
562            && let Some(input) = ext.downcast_value_ref::<RawInput>()
563        {
564            if input.is::<F>() {
565                return Ok(Raw::from_input(input.clone().into_owned()));
566            }
567            return Raw::encode(&input.record()?);
568        }
569        let mut recording = RecordBuf::new();
570        recording.set_atom(&atom, state);
571        Raw::encode(&recording)
572    }
573}
574
575impl<'de, F: RawFormat> Raw<'de, F> {
576    /// Creates a raw value from an atom that was delivered borrowed for it.
577    #[inline(never)]
578    fn from_borrowed_atom(atom: Atom<'de>, state: &State) -> Result<Raw<'de, F>, Error> {
579        if let Atom::Ext(ref ext) = atom {
580            // SAFETY: raw inputs are covariant in their lifetime
581            if let Some(input) = unsafe { ext.downcast_value_ref_covariant::<RawInput>() }
582                && input.is::<F>()
583            {
584                return Ok(Raw::from_input(input.clone()));
585            }
586        }
587        Raw::from_atom(atom, state)
588    }
589}
590
591impl<F: TextRawFormat> Raw<'_, F> {
592    /// Returns the encoded value as text.
593    pub fn get(&self) -> &str {
594        // SAFETY: the encoding of text formats is valid UTF-8
595        unsafe { core::str::from_utf8_unchecked(&self.input.bytes) }
596    }
597}
598
599impl<F: RawFormat> Clone for Raw<'_, F> {
600    fn clone(&self) -> Self {
601        Raw::from_input(self.input.clone())
602    }
603}
604
605impl<F: RawFormat> fmt::Debug for Raw<'_, F> {
606    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
607        let mut debug = f.debug_tuple("Raw");
608        debug.field(&self.input.format.id.name);
609        match self.input.as_str() {
610            Some(text) => debug.field(&text),
611            None => debug.field(&self.input.bytes),
612        };
613        debug.finish()
614    }
615}
616
617/// Raw values are equal if their encodings are equal.
618impl<F: RawFormat> PartialEq for Raw<'_, F> {
619    fn eq(&self, other: &Self) -> bool {
620        self.input.bytes == other.input.bytes
621    }
622}
623
624impl<F: RawFormat> Eq for Raw<'_, F> {}
625
626impl<F: RawFormat> core::hash::Hash for Raw<'_, F> {
627    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
628        self.input.bytes.hash(state)
629    }
630}
631
632/// Serialized with the format `F`, the encoded value is written as it is.
633/// Other formats serialize the value it holds.
634impl<F: RawFormat> Serialize for Raw<'_, F> {
635    fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
636        serialize_input(&value.input, state)
637    }
638}
639
640/// Raw values are deserialized owned so that `Raw<'static, F>` can be
641/// deserialized from any data.  To borrow use the
642/// [`Borrowed`] adapter.
643impl<'de, 'a, F: RawFormat> Deserialize<'de> for Raw<'a, F> {
644    fn deserialize_into<'out>(
645        out: &'out mut Option<Self>,
646        state: &mut State,
647    ) -> SinkHandle<'out, 'de> {
648        RecordBuf::capture_with(RawCapture(out), state)
649    }
650
651    fn expecting() -> Cow<'static, str> {
652        Cow::Borrowed("any value")
653    }
654
655    #[inline]
656    fn __private_atom_into(
657        out: &mut Option<Self>,
658        atom: Atom,
659        state: &mut State,
660    ) -> Result<(), Error> {
661        *out = Some(Raw::from_atom(atom, state)?);
662        Ok(())
663    }
664
665    #[inline]
666    fn __private_borrowed_atom_into(
667        out: &mut Option<Self>,
668        atom: Atom<'de>,
669        state: &mut State,
670    ) -> Result<(), Error> {
671        *out = Some(Raw::from_atom(atom, state)?);
672        Ok(())
673    }
674
675    #[inline(always)]
676    fn __private_raw() -> Option<&'static RawFormatInfo> {
677        Some(F::info())
678    }
679}
680
681/// Borrows raw values from the data if the format passes it on borrowed.
682impl<'de: 'a, 'a, F: RawFormat> Deserialize<'de, Raw<'a, F>> for Borrowed {
683    fn deserialize_into<'out>(
684        out: &'out mut Option<Raw<'a, F>>,
685        state: &mut State,
686    ) -> SinkHandle<'out, 'de> {
687        RecordBuf::capture_with(BorrowedRawCapture(out), state)
688    }
689
690    fn expecting() -> Cow<'static, str> {
691        Cow::Borrowed("any value")
692    }
693
694    #[inline]
695    fn __private_atom_into(
696        out: &mut Option<Raw<'a, F>>,
697        atom: Atom,
698        state: &mut State,
699    ) -> Result<(), Error> {
700        *out = Some(Raw::from_atom(atom, state)?);
701        Ok(())
702    }
703
704    #[inline]
705    fn __private_borrowed_atom_into(
706        out: &mut Option<Raw<'a, F>>,
707        atom: Atom<'de>,
708        state: &mut State,
709    ) -> Result<(), Error> {
710        *out = Some(Raw::from_borrowed_atom(atom, state)?);
711        Ok(())
712    }
713
714    #[inline(always)]
715    fn __private_raw() -> Option<&'static RawFormatInfo> {
716        Some(F::info())
717    }
718}
719
720impl<'a, F: RawFormat> Serialize<Raw<'a, F>> for Borrowed {
721    fn serialize<'x>(value: &'x Raw<'a, F>, state: &mut State) -> Result<Emit<'x>, Error> {
722        <Raw<'a, F>>::serialize(value, state)
723    }
724}
725
726/// Places a captured value into the slot of a raw value, owned.
727struct RawCapture<'o, 'a, F: RawFormat>(&'o mut Option<Raw<'a, F>>);
728
729impl<'o, 'a, 'de, F: RawFormat> Capture<'de, RecordBuf<'de>> for RawCapture<'o, 'a, F> {
730    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
731        *self.0 = Some(Raw::from_atom(atom, state)?);
732        Ok(())
733    }
734
735    fn recorded(&mut self, recording: RecordBuf<'de>, _state: &mut State) -> Result<(), Error> {
736        *self.0 = Some(Raw::encode(&recording)?);
737        Ok(())
738    }
739}
740
741/// Places a captured value into the slot of a raw value, borrowed.
742struct BorrowedRawCapture<'o, 'a, F: RawFormat>(&'o mut Option<Raw<'a, F>>);
743
744impl<'o, 'a, 'de: 'a, F: RawFormat> Capture<'de, RecordBuf<'de>> for BorrowedRawCapture<'o, 'a, F> {
745    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
746        *self.0 = Some(Raw::from_atom(atom, state)?);
747        Ok(())
748    }
749
750    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
751        *self.0 = Some(Raw::from_borrowed_atom(atom, state)?);
752        Ok(())
753    }
754
755    fn recorded(&mut self, recording: RecordBuf<'de>, _state: &mut State) -> Result<(), Error> {
756        *self.0 = Some(Raw::encode(&recording)?);
757        Ok(())
758    }
759}