deser-core 0.10.1

Core traits and types of deser, use the deser crate instead
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::any::Any;
use core::fmt;
use core::marker::PhantomData;

use crate::State;
use crate::adapters::Borrowed;
use crate::de::recording::Capture;
use crate::de::{Deserialize, DeserializeDriver, RecordBuf, Sink, SinkHandle};
use crate::error::{Error, ErrorKind};
use crate::event::Atom;
use crate::ext::{BorrowedExtension, ExtValue};
use crate::ser::{Emit, Serialize, SerializeHandle, SerializeRef};

/// A data format whose encoded values can be held by [`Raw`].
///
/// This is implemented by a type of the crate of the format that stands
/// for the format (for instance `deser_json::Json`).  The format is
/// described at runtime by a [`RawFormatInfo`].
///
/// # Passing on the Input of Values
///
/// Raw values of a format that is deserialized from the same format hold
/// the input of the value, and serializers of the format write them as
/// they are.  For this the format:
///
/// * calls [`State::declare_raw_format`](crate::State::declare_raw_format) with
///   its [`RawFormatId`] in the deserializer (before the first event, it
///   returns the description of the format if the top-level value is
///   wanted as raw value) and in the serializer (before the first value).
/// * checks with [`Error::is_raw_request`](crate::Error::is_raw_request)
///   whether the result of an event requests the next value as raw value
///   and takes the description of the format with
///   [`State::take_raw_request`](crate::State::take_raw_request).
/// * checks that the description has the [`RawFormatId`] of the format
///   (requests can be for raw values of any format, they are an error
///   then), validates a value that is wanted as raw value and emits its
///   input as [`RawInput`] (an [`Atom::Ext`]) with that description
///   rather than its events.
/// * writes the [`RawInput`] of its format as it is when serializing
///   (see [`RawInput::is_format`]).
///
/// The parser and the serializer only refer to the [`RawFormatId`]: the
/// functions of the format (like `replay`, which brings in its parser)
/// are only in programs that use its raw values.
///
/// Formats that do not do this still have raw values: the values are
/// encoded with the format then.
pub trait RawFormat: 'static {
    /// Returns the description of the format.
    ///
    /// This must always return the same static: formats are identified by
    /// the address of their description.
    fn info() -> &'static RawFormatInfo;
}

/// A [`RawFormat`] whose encoding is text.
///
/// [`Raw`] values of such formats can be accessed as strings (see
/// [`Raw::get`]).
///
/// # Safety
///
/// The format must be described as text (see [`RawFormatId::new`]): its
/// parser only passes on and its encoder only produces valid UTF-8.
pub unsafe trait TextRawFormat: RawFormat {}

/// Identifies a [`RawFormat`].
///
/// Formats are identified by the address of a static of this type.  Unlike
/// the [`RawFormatInfo`] of the format it holds no functions: formats
/// declare which raw values they pass on with it (see
/// [`State::declare_raw_format`](crate::State::declare_raw_format)), so a program
/// only contains the functions of the format (like its parser for
/// `replay`) if it uses its raw values.
pub struct RawFormatId {
    name: &'static str,
    is_text: bool,
}

impl RawFormatId {
    /// Creates the identity of a format.
    ///
    /// * `name` is the name of the format (like `"json"`).
    /// * `is_text` is `true` if the encoding is text.  The encoded values
    ///   must then be valid UTF-8.
    pub const fn new(name: &'static str, is_text: bool) -> RawFormatId {
        RawFormatId { name, is_text }
    }

    /// Returns the name of the format.
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Returns `true` if the encoding of the format is text.
    pub fn is_text(&self) -> bool {
        self.is_text
    }
}

impl fmt::Debug for RawFormatId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("RawFormatId").field(&self.name).finish()
    }
}

/// Describes a [`RawFormat`] at runtime.
///
/// Formats that can pass on the input of values define a static of this
/// type.  It travels with the input of values (see [`RawInput`]) so that
/// code which does not know the format can still parse it.  Formats are
/// identified by their [`RawFormatId`].
pub struct RawFormatInfo {
    id: &'static RawFormatId,
    replay: for<'de> fn(&'de [u8], &mut DeserializeDriver<'_, 'de>) -> Result<(), Error>,
    encode: fn(SerializeRef<'_>) -> Result<Vec<u8>, Error>,
    fallback: for<'v> fn(&'v [u8]) -> Atom<'v>,
    data: Option<&'static (dyn Any + Send + Sync)>,
}

impl RawFormatInfo {
    /// Creates the description of a format.
    ///
    /// * `id` identifies the format.
    /// * `replay` parses a value and emits its events into the driver.
    /// * `encode` encodes a value.
    /// * `fallback` returns the fallback atom of an encoded value (see
    ///   [`Extension::fallback`](crate::ext::Extension::fallback)).  It
    ///   must be [`Atom::Null`] for null, so that optionals are `None` for
    ///   it, and must not be an extension value.
    pub const fn new(
        id: &'static RawFormatId,
        replay: for<'de> fn(&'de [u8], &mut DeserializeDriver<'_, 'de>) -> Result<(), Error>,
        encode: fn(SerializeRef<'_>) -> Result<Vec<u8>, Error>,
        fallback: for<'v> fn(&'v [u8]) -> Atom<'v>,
    ) -> RawFormatInfo {
        RawFormatInfo {
            id,
            replay,
            encode,
            fallback,
            data: None,
        }
    }

    /// Attaches data of the format to the description.
    ///
    /// The format gets it back from the description of raw values that are
    /// requested (see [`data`](Self::data)).  Formats keep what only
    /// programs that use their raw values need here (like the scanner of
    /// raw values in the parser): as only raw values refer to the
    /// description, other programs do not contain it.
    pub const fn set_data(&mut self, data: &'static (dyn Any + Send + Sync)) {
        self.data = Some(data);
    }

    /// Returns the data of the format (see [`set_data`](Self::set_data)).
    pub fn data(&self) -> Option<&'static (dyn Any + Send + Sync)> {
        self.data
    }

    /// Returns the identity of the format.
    pub fn id(&self) -> &'static RawFormatId {
        self.id
    }

    /// Returns the name of the format.
    pub fn name(&self) -> &'static str {
        self.id.name
    }

    /// Returns `true` if the encoding of the format is text.
    pub fn is_text(&self) -> bool {
        self.id.is_text
    }

    /// Records an encoded value.
    fn record<'a>(&self, bytes: &'a [u8]) -> Result<RecordBuf<'a>, Error> {
        let mut recording = RecordBuf::new();
        {
            let mut driver = DeserializeDriver::from_fn(|state| recording.recorder(state));
            (self.replay)(bytes, &mut driver)?;
        }
        Ok(recording)
    }
}

impl fmt::Debug for RawFormatInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("RawFormatInfo").field(&self.id.name).finish()
    }
}

/// Returns `true` if two descriptions are the same format.
#[inline(always)]
fn same_format(a: &'static RawFormatInfo, b: &'static RawFormatInfo) -> bool {
    core::ptr::eq(a.id, b.id)
}

/// The encoded input of a value.
///
/// This is a well-known borrowing extension (see [`ext`](crate::ext)) which
/// carries the encoding of a value between formats and [`Raw`] values:
///
/// * formats emit it for values that are requested as raw values (the
///   values of [`Raw`] types).  They validate the value and pass on its
///   input instead of its events.
/// * [`Raw`] values emit it when they are serialized and the serializer
///   writes the format as it is (see [`State::declare_raw_format`]).
///
/// It knows its format, so it's parsed into its value where it ends up in
/// something that does not know it (for instance when a recording that
/// holds it is serialized).
#[derive(Clone)]
pub struct RawInput<'a> {
    bytes: Cow<'a, [u8]>,
    format: &'static RawFormatInfo,
}

impl<'a> RawInput<'a> {
    /// Creates the input of a value.
    ///
    /// # Safety
    ///
    /// The input must be a single, valid value of the format.  If the
    /// format is text, it must be valid UTF-8.  Serializers of the format
    /// write the input as it is.
    ///
    /// Descriptions of formats can be created by anybody (with the
    /// identity of any format) and requests for raw values can be for any
    /// format: formats must check that the description has the identity
    /// of their own format (see
    /// [`State::take_raw_request`](crate::State::take_raw_request)) rather
    /// than rely on where it comes from or on its
    /// [`data`](RawFormatInfo::data).
    pub unsafe fn new<B: Into<Cow<'a, [u8]>>>(
        bytes: B,
        format: &'static RawFormatInfo,
    ) -> RawInput<'a> {
        let bytes = bytes.into();
        debug_assert!(!format.is_text() || core::str::from_utf8(&bytes).is_ok());
        RawInput { bytes, format }
    }

    /// Returns the encoded value.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Returns the encoded value as text if the format is text.
    pub fn as_str(&self) -> Option<&str> {
        // SAFETY: the input of text formats is valid UTF-8, see `new`
        self.format
            .is_text()
            .then(|| unsafe { core::str::from_utf8_unchecked(&self.bytes) })
    }

    /// Returns the format of the value.
    pub fn format(&self) -> &'static RawFormatInfo {
        self.format
    }

    /// Returns `true` if the value is of the format `F`.
    pub fn is<F: RawFormat>(&self) -> bool {
        same_format(self.format, F::info())
    }

    /// Returns `true` if the value is of the format with the identity.
    ///
    /// Serializers check with this whether they write a value as it is
    /// (unlike [`is`](Self::is), this does not refer to the functions of
    /// the format).
    pub fn is_format(&self, id: &'static RawFormatId) -> bool {
        core::ptr::eq(self.format.id, id)
    }

    /// Detaches the input from the data it borrows.
    pub fn into_owned(self) -> RawInput<'static> {
        RawInput {
            bytes: Cow::Owned(self.bytes.into_owned()),
            format: self.format,
        }
    }

    /// Records the value.
    pub fn record(&self) -> Result<RecordBuf<'_>, Error> {
        self.format.record(&self.bytes)
    }

    /// Replays the value into a sink.
    ///
    /// The value is parsed, the sink can borrow its data.
    pub fn replay<'x>(&'x self, sink: SinkHandle<'_, 'x>, state: &mut State) -> Result<(), Error> {
        // the type of the sink is unknown, it does not want a raw value
        self.replay_raw(sink, None, state)
    }

    /// Replays the value, `raw` is the raw value the sink wants.
    fn replay_raw<'x>(
        &'x self,
        sink: SinkHandle<'_, 'x>,
        raw: Option<&'static RawFormatInfo>,
        state: &mut State,
    ) -> Result<(), Error> {
        DeserializeDriver::nested(state, sink, false, |driver| {
            // the top-level value is requested before it starts
            driver.state_mut().raw_requested = raw;
            (self.format.replay)(&self.bytes, driver)
        })
    }

    /// Deserializes the value.
    ///
    /// The value can borrow from the input (and the data it borrows).
    pub fn deserialize<'x, T: Deserialize<'x>>(&'x self) -> Result<T, Error> {
        // the driver requests the value as raw value if `T` wants one
        crate::de::deserialize_value(|driver| (self.format.replay)(&self.bytes, driver))
    }
}

/// Parses the input of a raw value into a sink that does not accept it.
///
/// The sink is finished by the caller (see `NoFinish`).
pub(crate) fn parse_into<'de>(
    input: &RawInput<'_>,
    sink: &mut (dyn Sink<'de> + '_),
    state: &mut State,
) -> Result<(), Error> {
    let mut sink = NoFinish(sink);
    DeserializeDriver::nested(state, SinkHandle::to(&mut sink), false, |driver| {
        driver.state_mut().raw_requested = None;
        // the input lives shorter than the data of the sink
        driver.transient(|driver| (input.format.replay)(&input.bytes, driver))
    })
}

/// Forwards to a sink except for [`Sink::finish`].
///
/// This is used to parse a value into a sink that received an atom (see
/// `parse_into`): the replay finishes the value, the sink is finished by
/// the code that delivered the atom.
struct NoFinish<'a, 'b, 'de>(&'a mut (dyn Sink<'de> + 'b));

impl<'de> Sink<'de> for NoFinish<'_, '_, 'de> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.0.atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        self.0.borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.0.map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        self.0.seq(state)
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.0.next_key(state)
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.0.next_value(state)
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        self.0.value_for_key(key, state)
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        self.0.recover(err, state)
    }

    fn expecting(&self) -> Cow<'_, str> {
        self.0.expecting()
    }

    fn finish(&mut self, _state: &mut State) -> Result<(), Error> {
        Ok(())
    }
}

impl fmt::Debug for RawInput<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("RawInput");
        debug.field("format", &self.format.id.name);
        match self.as_str() {
            Some(text) => debug.field("text", &text),
            None => debug.field("bytes", &self.bytes),
        };
        debug.finish()
    }
}

impl PartialEq for RawInput<'_> {
    fn eq(&self, other: &Self) -> bool {
        same_format(self.format, other.format) && self.bytes == other.bytes
    }
}

impl BorrowedExtension for RawInput<'static> {
    type Value<'a> = RawInput<'a>;

    fn name<'v>(_value: &'v RawInput<'_>) -> &'v str {
        "raw value"
    }

    fn fallback<'v>(value: &'v RawInput<'_>) -> Atom<'v> {
        (value.format.fallback)(&value.bytes)
    }

    fn to_static(value: &RawInput<'_>) -> RawInput<'static> {
        value.clone().into_owned()
    }

    fn shorten<'s, 'l: 's>(value: &'s RawInput<'l>) -> &'s RawInput<'s> {
        value
    }
}

/// Serializes the input of a value.
///
/// The input is passed on as extension value if the serializer writes its
/// format as it is, otherwise the value is parsed and serialized.
fn serialize_input<'a>(input: &'a RawInput<'_>, state: &mut State) -> Result<Emit<'a>, Error> {
    if state.accepts_raw(input.format) {
        return Ok(Emit::Atom(Atom::Ext(ExtValue::borrowed_value::<RawInput>(
            input,
        ))));
    }
    Ok(Emit::Forward(SerializeHandle::arena(
        input.record()?,
        state,
    )))
}

/// Serializes an atom of a recording.
///
/// The input of values that the serializer does not write as it is is
/// parsed and serialized.
pub(crate) fn serialize_recorded_atom<'a>(
    atom: &'a Atom<'_>,
    state: &mut State,
) -> Result<Emit<'a>, Error> {
    if let Atom::Ext(ext) = atom
        && let Some(input) = ext.downcast_value_ref::<RawInput>()
    {
        return serialize_input(input, state);
    }
    Ok(Emit::Atom(atom.as_borrowed()))
}

/// A value encoded in the format `F`.
///
/// A raw value holds a value in the encoding of a format, for instance
/// JSON text with `deser_json::RawJson`.  It can be deserialized later
/// (with [`deserialize`](Self::deserialize)), stored or written out again
/// unchanged:
///
/// * If it's deserialized from the format `F`, it holds the input of the
///   value as it is.  The format only validates the value and does not
///   produce its events, which is fast.  With the
///   [`Borrowed`] adapter the input is
///   borrowed.
/// * Otherwise (from another format or where the format cannot pass on
///   the input, see below) the value is encoded in the format `F`.  The
///   encoding can lose information that `F` cannot express (bytes in JSON
///   for instance).  To keep a value of any format without interpreting
///   it, use [`Recording`](crate::de::Recording).
///
/// When serialized with the format `F`, the encoded value is written as it
/// is.  Other formats serialize the value it holds.
///
/// The input of values can only be passed on if the format knows before
/// the value starts that a raw value is wanted.  Derived structs (for their
/// first 64 fields), maps, sequences, `Option` and `Box` ask for it.
/// Values in other places (like the fields of flattened structs, the
/// variants of enums and values that are buffered, for instance for
/// untagged enums) are encoded.
///
/// Like `Cow`, raw values are deserialized owned, so a `Raw<'static, F>`
/// can be deserialized from any data.
pub struct Raw<'a, F: RawFormat> {
    input: RawInput<'a>,
    _format: PhantomData<fn() -> F>,
}

impl<'a, F: RawFormat> Raw<'a, F> {
    /// Creates a raw value from its encoding.
    ///
    /// The value is validated.
    pub fn new<B: Into<Cow<'a, [u8]>>>(bytes: B) -> Result<Raw<'a, F>, Error> {
        let bytes = bytes.into();
        let info = F::info();
        if info.is_text() && core::str::from_utf8(&bytes).is_err() {
            return Err(Error::new(ErrorKind::Syntax, "invalid utf-8"));
        }
        {
            let mut driver = DeserializeDriver::from_fn(|_| SinkHandle::null());
            (info.replay)(&bytes, &mut driver)?;
        }
        // SAFETY: the value was validated
        Ok(Raw::from_input(unsafe { RawInput::new(bytes, info) }))
    }

    /// Encodes a value.
    pub fn encode<T: Serialize + ?Sized>(value: &T) -> Result<Raw<'static, F>, Error> {
        let info = F::info();
        let bytes = (info.encode)(SerializeRef::new(&value))?;
        if info.is_text() && core::str::from_utf8(&bytes).is_err() {
            return Err(Error::new(
                ErrorKind::InvalidState,
                "the encoding of a text format is not utf-8",
            ));
        }
        // SAFETY: the encoder produces a valid value
        Ok(Raw::from_input(unsafe { RawInput::new(bytes, info) }))
    }

    fn from_input(input: RawInput<'a>) -> Raw<'a, F> {
        debug_assert!(input.is::<F>());
        Raw {
            input,
            _format: PhantomData,
        }
    }

    /// Returns the encoded value.
    pub fn as_bytes(&self) -> &[u8] {
        &self.input.bytes
    }

    /// Returns `true` if the value borrows from the data it was
    /// deserialized from.
    pub fn is_borrowed(&self) -> bool {
        matches!(self.input.bytes, Cow::Borrowed(_))
    }

    /// Replays the value into a sink.
    ///
    /// The value is parsed, the sink can borrow its data.
    pub fn replay<'x>(&'x self, sink: SinkHandle<'_, 'x>, state: &mut State) -> Result<(), Error> {
        self.input.replay(sink, state)
    }

    /// Deserializes the value.
    ///
    /// The value can borrow from the raw value (and the data it borrows).
    pub fn deserialize<'x, T: Deserialize<'x>>(&'x self) -> Result<T, Error> {
        self.input.deserialize()
    }

    /// Detaches the value from the data it borrows.
    pub fn into_owned(self) -> Raw<'static, F> {
        Raw::from_input(self.input.into_owned())
    }

    /// Creates a raw value from an atom that was delivered for it.
    #[inline(never)]
    fn from_atom(atom: Atom<'_>, state: &State) -> Result<Raw<'static, F>, Error> {
        if let Atom::Ext(ref ext) = atom
            && let Some(input) = ext.downcast_value_ref::<RawInput>()
        {
            if input.is::<F>() {
                return Ok(Raw::from_input(input.clone().into_owned()));
            }
            return Raw::encode(&input.record()?);
        }
        let mut recording = RecordBuf::new();
        recording.set_atom(&atom, state);
        Raw::encode(&recording)
    }
}

impl<'de, F: RawFormat> Raw<'de, F> {
    /// Creates a raw value from an atom that was delivered borrowed for it.
    #[inline(never)]
    fn from_borrowed_atom(atom: Atom<'de>, state: &State) -> Result<Raw<'de, F>, Error> {
        if let Atom::Ext(ref ext) = atom {
            // SAFETY: raw inputs are covariant in their lifetime
            if let Some(input) = unsafe { ext.downcast_value_ref_covariant::<RawInput>() }
                && input.is::<F>()
            {
                return Ok(Raw::from_input(input.clone()));
            }
        }
        Raw::from_atom(atom, state)
    }
}

impl<F: TextRawFormat> Raw<'_, F> {
    /// Returns the encoded value as text.
    pub fn get(&self) -> &str {
        // SAFETY: the encoding of text formats is valid UTF-8
        unsafe { core::str::from_utf8_unchecked(&self.input.bytes) }
    }
}

impl<F: RawFormat> Clone for Raw<'_, F> {
    fn clone(&self) -> Self {
        Raw::from_input(self.input.clone())
    }
}

impl<F: RawFormat> fmt::Debug for Raw<'_, F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_tuple("Raw");
        debug.field(&self.input.format.id.name);
        match self.input.as_str() {
            Some(text) => debug.field(&text),
            None => debug.field(&self.input.bytes),
        };
        debug.finish()
    }
}

/// Raw values are equal if their encodings are equal.
impl<F: RawFormat> PartialEq for Raw<'_, F> {
    fn eq(&self, other: &Self) -> bool {
        self.input.bytes == other.input.bytes
    }
}

impl<F: RawFormat> Eq for Raw<'_, F> {}

impl<F: RawFormat> core::hash::Hash for Raw<'_, F> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.input.bytes.hash(state)
    }
}

/// Serialized with the format `F`, the encoded value is written as it is.
/// Other formats serialize the value it holds.
impl<F: RawFormat> Serialize for Raw<'_, F> {
    fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
        serialize_input(&value.input, state)
    }
}

/// Raw values are deserialized owned so that `Raw<'static, F>` can be
/// deserialized from any data.  To borrow use the
/// [`Borrowed`] adapter.
impl<'de, 'a, F: RawFormat> Deserialize<'de> for Raw<'a, F> {
    fn deserialize_into<'out>(
        out: &'out mut Option<Self>,
        state: &mut State,
    ) -> SinkHandle<'out, 'de> {
        RecordBuf::capture_with(RawCapture(out), state)
    }

    fn expecting() -> Cow<'static, str> {
        Cow::Borrowed("any value")
    }

    #[inline]
    fn __private_atom_into(
        out: &mut Option<Self>,
        atom: Atom,
        state: &mut State,
    ) -> Result<(), Error> {
        *out = Some(Raw::from_atom(atom, state)?);
        Ok(())
    }

    #[inline]
    fn __private_borrowed_atom_into(
        out: &mut Option<Self>,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        *out = Some(Raw::from_atom(atom, state)?);
        Ok(())
    }

    #[inline(always)]
    fn __private_raw() -> Option<&'static RawFormatInfo> {
        Some(F::info())
    }
}

/// Borrows raw values from the data if the format passes it on borrowed.
impl<'de: 'a, 'a, F: RawFormat> Deserialize<'de, Raw<'a, F>> for Borrowed {
    fn deserialize_into<'out>(
        out: &'out mut Option<Raw<'a, F>>,
        state: &mut State,
    ) -> SinkHandle<'out, 'de> {
        RecordBuf::capture_with(BorrowedRawCapture(out), state)
    }

    fn expecting() -> Cow<'static, str> {
        Cow::Borrowed("any value")
    }

    #[inline]
    fn __private_atom_into(
        out: &mut Option<Raw<'a, F>>,
        atom: Atom,
        state: &mut State,
    ) -> Result<(), Error> {
        *out = Some(Raw::from_atom(atom, state)?);
        Ok(())
    }

    #[inline]
    fn __private_borrowed_atom_into(
        out: &mut Option<Raw<'a, F>>,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        *out = Some(Raw::from_borrowed_atom(atom, state)?);
        Ok(())
    }

    #[inline(always)]
    fn __private_raw() -> Option<&'static RawFormatInfo> {
        Some(F::info())
    }
}

impl<'a, F: RawFormat> Serialize<Raw<'a, F>> for Borrowed {
    fn serialize<'x>(value: &'x Raw<'a, F>, state: &mut State) -> Result<Emit<'x>, Error> {
        <Raw<'a, F>>::serialize(value, state)
    }
}

/// Places a captured value into the slot of a raw value, owned.
struct RawCapture<'o, 'a, F: RawFormat>(&'o mut Option<Raw<'a, F>>);

impl<'o, 'a, 'de, F: RawFormat> Capture<'de, RecordBuf<'de>> for RawCapture<'o, 'a, F> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        *self.0 = Some(Raw::from_atom(atom, state)?);
        Ok(())
    }

    fn recorded(&mut self, recording: RecordBuf<'de>, _state: &mut State) -> Result<(), Error> {
        *self.0 = Some(Raw::encode(&recording)?);
        Ok(())
    }
}

/// Places a captured value into the slot of a raw value, borrowed.
struct BorrowedRawCapture<'o, 'a, F: RawFormat>(&'o mut Option<Raw<'a, F>>);

impl<'o, 'a, 'de: 'a, F: RawFormat> Capture<'de, RecordBuf<'de>> for BorrowedRawCapture<'o, 'a, F> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        *self.0 = Some(Raw::from_atom(atom, state)?);
        Ok(())
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        *self.0 = Some(Raw::from_borrowed_atom(atom, state)?);
        Ok(())
    }

    fn recorded(&mut self, recording: RecordBuf<'de>, _state: &mut State) -> Result<(), Error> {
        *self.0 = Some(Raw::encode(&recording)?);
        Ok(())
    }
}