tokio-dbus 0.2.0

Pure Rust D-Bus implementation for Tokio.
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
pub use self::load_array::LoadArray;
mod load_array;

pub use self::as_body::AsBody;
mod as_body;

use core::fmt;

#[cfg(feature = "alloc")]
use crate::BodyBuf;
use crate::buf::Aligned;
use crate::error::Result;
use crate::ty;
use crate::{Endianness, Frame, Read, Signature};

/// A read-only view into a buffer suitable for use as a body in a [`Message`].
///
/// [`Message`]: crate::Message
///
/// # Examples
///
/// ```
/// use tokio_dbus::{Result, Body};
///
/// fn read(buf: &mut Body<'_>) -> Result<()> {
///     assert_eq!(buf.load::<u32>()?, 7u32);
///     assert_eq!(buf.load::<u8>()?, b'f');
///     assert_eq!(buf.load::<u8>()?, b'o');
///     assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
///     Ok(())
/// }
/// # Ok::<_, tokio_dbus::Error>(())
/// ```
pub struct Body<'a> {
    data: Aligned<'a>,
    endianness: Endianness,
    signature: &'a Signature,
}

impl<'a> Body<'a> {
    /// Construct an empty buffer.
    pub(crate) const fn empty() -> Self {
        Self::from_raw_parts(Aligned::empty(), Endianness::NATIVE, Signature::EMPTY)
    }

    /// Construct a new buffer wrapping pointed to data.
    #[inline]
    pub(crate) const fn from_raw_parts(
        data: Aligned<'a>,
        endianness: Endianness,
        signature: &'a Signature,
    ) -> Self {
        Self {
            data,
            endianness,
            signature,
        }
    }

    /// Deconstruct into raw parts.
    #[cfg(feature = "alloc")]
    #[inline]
    pub(crate) const fn into_raw_parts(self) -> (Aligned<'a>, Endianness, &'a Signature) {
        (self.data, self.endianness, self.signature)
    }

    /// Get the endianness of the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Body, BodyBuf, Endianness};
    ///
    /// let buf = BodyBuf::new();
    ///
    /// let buf: Body<'_> = buf.as_body();
    /// assert_eq!(buf.endianness(), Endianness::NATIVE);
    ///
    /// let buf = buf.with_endianness(Endianness::BIG);
    /// assert_eq!(buf.endianness(), Endianness::BIG);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }

    /// Adjust endianness of buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Body, BodyBuf, Endianness};
    ///
    /// let buf = BodyBuf::new();
    ///
    /// let buf: Body<'_> = buf.as_body();
    /// assert_eq!(buf.endianness(), Endianness::NATIVE);
    ///
    /// let buf = buf.with_endianness(Endianness::BIG);
    /// assert_eq!(buf.endianness(), Endianness::BIG);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn with_endianness(self, endianness: Endianness) -> Self {
        Self { endianness, ..self }
    }

    /// Get the signature of the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Body, BodyBuf};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// buf.store(10u16)?;
    /// buf.store(10u32)?;
    ///
    /// let buf: Body<'_> = buf.as_body();
    ///
    /// assert_eq!(buf.signature(), "qu");
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn signature(&self) -> &'a Signature {
        self.signature
    }

    /// Adjust the signature of buffer.
    #[cfg(feature = "alloc")]
    pub(crate) fn with_signature(self, signature: &'a Signature) -> Self {
        Self { signature, ..self }
    }

    /// Get a slice out of the buffer that has ben written to.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Result, Body};
    ///
    /// fn read(buf: &mut Body<'_>) -> Result<()> {
    ///     assert_eq!(buf.load::<u32>()?, 7u32);
    ///     assert_eq!(buf.load::<u8>()?, b'f');
    ///     assert_eq!(buf.load::<u8>()?, b'o');
    ///     assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
    ///     Ok(())
    /// }
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn get(&self) -> &'a [u8] {
        self.data.get()
    }

    /// Test if the buffer is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Body, BodyBuf, Endianness};
    ///
    /// let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
    /// let b: Body<'_> = buf.as_body();
    /// assert!(b.is_empty());
    ///
    /// buf.store(10u16)?;
    /// buf.store(10u32)?;
    ///
    /// let b: Body<'_> = buf.as_body();
    /// assert!(!b.is_empty());
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Remaining data to be read from the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Body, BodyBuf, Endianness};
    ///
    /// let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
    /// assert!(buf.is_empty());
    ///
    /// buf.store(10u16)?;
    /// buf.store(10u32)?;
    ///
    /// let b: Body<'_> = buf.as_body();
    /// assert_eq!(b.len(), 8);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Read a reference from the buffer.
    ///
    /// This is possible for unaligned types such as `str` and `[u8]` which
    /// implement [`Read`].
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Result, Body};
    ///
    /// fn read(buf: &mut Body<'_>) -> Result<()> {
    ///     assert_eq!(buf.load::<u32>()?, 4);
    ///     assert_eq!(buf.read::<str>()?, "hi");
    ///     assert!(buf.is_empty());
    ///     Ok(())
    /// }
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ````
    pub fn read<T>(&mut self) -> Result<&'a T>
    where
        T: ?Sized + Read,
    {
        T::read_from(self)
    }

    /// Read `len` bytes from the buffer and make accessible through another
    /// [`Body`] instance constituting that sub-slice.
    ///
    /// # Panics
    ///
    /// This panics if `len` is larger than [`len()`].
    ///
    /// [`len()`]: Self::len
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Result, Body};
    ///
    /// fn read(buf: &mut Body<'_>) -> Result<()> {
    ///     let mut read_buf = buf.read_until(6);
    ///     assert_eq!(read_buf.load::<u32>()?, 4);
    ///
    ///     let mut read_buf2 = read_buf.read_until(2);
    ///     assert_eq!(read_buf2.load::<u8>()?, 1);
    ///     assert_eq!(read_buf2.load::<u8>()?, 2);
    ///
    ///     assert!(read_buf.is_empty());
    ///     assert!(read_buf2.is_empty());
    ///
    ///     assert_eq!(buf.get(), &[3, 4, 0]);
    ///     Ok(())
    /// }
    /// ```
    pub fn read_until(&mut self, len: usize) -> Body<'a> {
        Body::from_raw_parts(self.data.read_until(len), self.endianness, self.signature)
    }

    /// Read an array from the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, BodyBuf, Endianness};
    ///
    /// let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
    /// let mut array = buf.store_array::<u32>()?;
    /// array.store(10u32);
    /// array.store(20u32);
    /// array.store(30u32);
    /// array.finish();
    ///
    /// let mut array = buf.store_array::<ty::Array<ty::Str>>()?;
    /// let mut inner = array.store_array();
    /// inner.store("foo");
    /// inner.store("bar");
    /// inner.store("baz");
    /// inner.finish();
    /// array.finish();
    ///
    /// assert_eq!(buf.signature(), b"auaas");
    ///
    /// let mut buf = buf.as_body();
    /// let mut array = buf.load_array::<u32>()?;
    /// assert_eq!(array.load()?, Some(10));
    /// assert_eq!(array.load()?, Some(20));
    /// assert_eq!(array.load()?, Some(30));
    /// assert_eq!(array.load()?, None);
    ///
    /// let mut array = buf.load_array::<ty::Array<ty::Str>>()?;
    ///
    /// let Some(mut inner) = array.load_array()? else {
    ///     panic!("Missing inner array");
    /// };
    ///
    /// assert_eq!(inner.read()?, Some("foo"));
    /// assert_eq!(inner.read()?, Some("bar"));
    /// assert_eq!(inner.read()?, Some("baz"));
    /// assert_eq!(inner.read()?, None);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn load_array<E>(&mut self) -> Result<LoadArray<'a, E>>
    where
        E: ty::Marker,
    {
        LoadArray::from_mut(self)
    }

    /// Read a struct from the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, BodyBuf, Endianness};
    ///
    /// let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
    /// buf.store(10u8);
    ///
    /// buf.store_struct::<(u16, u32, ty::Array<u8>, ty::Str)>()?
    ///     .store(20u16)
    ///     .store(30u32)
    ///     .store_array(|w| {
    ///         w.store(1u8);
    ///         w.store(2u8);
    ///         w.store(3u8);
    ///     })
    ///     .store("Hello World")
    ///     .finish();
    ///
    /// assert_eq!(buf.signature(), "y(quays)");
    ///
    /// let mut buf = buf.as_body();
    /// assert_eq!(buf.load::<u8>()?, 10u8);
    ///
    /// let (a, b, mut array, string) = buf.load_struct::<(u16, u32, ty::Array<u8>, ty::Str)>()?;
    /// assert_eq!(a, 20u16);
    /// assert_eq!(b, 30u32);
    ///
    /// assert_eq!(array.load()?, Some(1));
    /// assert_eq!(array.load()?, Some(2));
    /// assert_eq!(array.load()?, Some(3));
    /// assert_eq!(array.load()?, None);
    ///
    /// assert_eq!(string, "Hello World");
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn load_struct<E>(&mut self) -> Result<E::Return<'a>>
    where
        E: ty::Fields,
    {
        self.align::<u64>()?;
        E::load_struct(self)
    }

    /// Read a struct whose fields are read by the given closure.
    ///
    /// This aligns the buffer as a struct and then hands it to `f`. It is an
    /// escape hatch for structs which [`load_struct()`] cannot describe, such as
    /// ones containing a variant of an unknown type.
    ///
    /// [`load_struct()`]: Self::load_struct
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, BodyBuf, Signature};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// buf.store_struct::<(u32, ty::Variant)>()?
    ///     .store(42u32)
    ///     .store_variant(Signature::new("as")?, |w| {
    ///         w.store_array::<ty::Str>().store("Hello");
    ///     })
    ///     .finish();
    ///
    /// let mut buf = buf.as_body();
    ///
    /// let n = buf.load_struct_with(|b| {
    ///     let n = b.load::<u32>()?;
    ///     b.skip_variant()?;
    ///     Ok(n)
    /// })?;
    ///
    /// assert_eq!(n, 42);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn load_struct_with<F, O>(&mut self, f: F) -> Result<O>
    where
        F: FnOnce(&mut Body<'a>) -> Result<O>,
    {
        self.align::<u64>()?;
        f(self)
    }

    /// Load a frame of the given type.
    ///
    /// This advances the read cursor of the buffer by the alignment and size of
    /// the type. The return value has been endian-adjusted as per
    /// [`endianness()`].
    ///
    /// [`endianness()`]: Self::endianness
    ///
    /// # Error
    ///
    /// Errors if the underlying buffer does not have enough space to represent
    /// the type `T`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{Result, Body};
    ///
    /// fn read(buf: &mut Body<'_>) -> Result<()> {
    ///     assert_eq!(buf.load::<u32>()?, 7u32);
    ///     assert_eq!(buf.load::<u8>()?, b'f');
    ///     assert_eq!(buf.load::<u8>()?, b'o');
    ///     assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
    ///     Ok(())
    /// }
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn load<T>(&mut self) -> Result<T>
    where
        T: Frame,
    {
        let mut frame = self.data.load::<T>()?;
        frame.adjust(self.endianness);
        Ok(frame)
    }

    /// Load a [`bool`] from the buffer.
    ///
    /// The D-Bus `BOOLEAN` type is marshalled as a 32-bit integer, which is why
    /// it cannot be loaded through [`load()`].
    ///
    /// [`load()`]: Self::load
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::BodyBuf;
    ///
    /// let mut buf = BodyBuf::new();
    /// buf.store(true)?;
    /// buf.store(false)?;
    ///
    /// let mut buf = buf.as_body();
    /// assert!(buf.load_bool()?);
    /// assert!(!buf.load_bool()?);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn load_bool(&mut self) -> Result<bool> {
        Ok(self.load::<u32>()? != 0)
    }

    /// Read a [`Variant`] holding a value of a basic type from the buffer.
    ///
    /// [`Variant`]: crate::Variant
    ///
    /// # Errors
    ///
    /// Errors if the variant holds a container. Use [`skip_variant()`] to skip
    /// over a variant of an unknown type instead.
    ///
    /// [`skip_variant()`]: Self::skip_variant
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{BodyBuf, Variant};
    ///
    /// let mut buf = BodyBuf::new();
    /// buf.store(Variant::U32(42))?;
    ///
    /// let mut buf = buf.as_body();
    /// assert_eq!(buf.read_variant()?, Variant::U32(42));
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn read_variant(&mut self) -> Result<crate::Variant<'a>> {
        <ty::Variant as ty::Marker>::load_struct(self)
    }

    /// Read a variant which is expected to contain a value of type `T`.
    ///
    /// Unlike [`read_variant()`] this can read containers, but requires the
    /// caller to know which type the variant contains.
    ///
    /// [`read_variant()`]: Self::read_variant
    ///
    /// # Errors
    ///
    /// Errors if the variant does not contain a value of type `T`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, BodyBuf, Signature};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// let mut array = buf.store_variant(Signature::new("as")?)?.store_array::<ty::Str>();
    /// array.store("Hello");
    /// array.store("World");
    /// array.finish();
    ///
    /// let mut buf = buf.as_body();
    /// let mut array = buf.read_variant_as::<ty::Array<ty::Str>>()?;
    ///
    /// assert_eq!(array.read()?, Some("Hello"));
    /// assert_eq!(array.read()?, Some("World"));
    /// assert_eq!(array.read()?, None);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    pub fn read_variant_as<T>(&mut self) -> Result<T::Return<'a>>
    where
        T: ty::Marker,
    {
        let signature = self.read::<Signature>()?;

        let mut expected = crate::signature::SignatureBuilder::new();
        T::write_signature(&mut expected)?;

        if signature != expected.to_signature() {
            #[cfg(feature = "alloc")]
            return Err(crate::Error::new(
                crate::error::ErrorKind::UnsupportedVariant(signature.into()),
            ));
            #[cfg(not(feature = "alloc"))]
            return Err(crate::Error::new(
                crate::error::ErrorKind::UnsupportedVariantNoAlloc,
            ));
        }

        self.align::<T::Alignment>()?;
        T::load_struct(self)
    }

    /// Skip over a variant of any type, returning the signature of the value it
    /// contained.
    ///
    /// This is useful for arguments which are declared as variants but which
    /// the receiver has no interest in, such as the `data` argument of the
    /// `com.canonical.dbusmenu.Event` method.
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, BodyBuf, Signature};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// buf.store_variant(Signature::new("as")?)?
    ///     .store_array::<ty::Str>()
    ///     .store("Hello");
    /// buf.store(42u32)?;
    ///
    /// assert_eq!(buf.signature(), "vu");
    ///
    /// let mut buf = buf.as_body();
    /// assert_eq!(buf.skip_variant()?, Signature::new("as")?);
    /// assert_eq!(buf.load::<u32>()?, 42);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    #[cfg(feature = "alloc")]
    pub fn skip_variant(&mut self) -> Result<&'a Signature> {
        let signature = self.read::<Signature>()?;
        crate::signature::skip(signature, self)?;
        Ok(signature)
    }

    /// Align the read cursor to the given alignment.
    ///
    /// This is the counterpart of [`Raw::align`], and is needed before reading
    /// the fields of a struct or a dict entry whose shape is only known at
    /// runtime.
    ///
    /// [`Raw::align`]: crate::Raw::align
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, Alignment, BodyBuf};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// buf.store(1u8)?;
    /// buf.store_struct::<(u32, u32)>()?.store(2u32).store(3u32).finish();
    ///
    /// let mut buf = buf.as_body();
    /// assert_eq!(buf.load::<u8>()?, 1);
    ///
    /// buf.align_to(Alignment::U64)?;
    /// assert_eq!(buf.load::<u32>()?, 2);
    /// assert_eq!(buf.load::<u32>()?, 3);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    #[cfg(feature = "alloc")]
    pub fn align_to(&mut self, alignment: crate::Alignment) -> Result<()> {
        self.data.align_to(alignment.in_bytes())
    }

    /// Read an array whose elements have the given alignment, returning a
    /// [`Body`] over its contents.
    ///
    /// This is the counterpart of [`Raw::store_array`].
    ///
    /// [`Raw::store_array`]: crate::Raw::store_array
    ///
    /// # Examples
    ///
    /// ```
    /// use tokio_dbus::{ty, Alignment, BodyBuf};
    ///
    /// let mut buf = BodyBuf::new();
    ///
    /// let mut array = buf.store_array::<ty::Str>()?;
    /// array.store("Hello");
    /// array.store("World");
    /// array.finish();
    ///
    /// let mut buf = buf.as_body();
    /// let mut array = buf.load_raw_array(Alignment::U32)?;
    ///
    /// let mut out = Vec::new();
    ///
    /// while !array.is_empty() {
    ///     out.push(array.read::<str>()?);
    /// }
    ///
    /// assert_eq!(out, ["Hello", "World"]);
    /// # Ok::<_, tokio_dbus::Error>(())
    /// ```
    #[cfg(feature = "alloc")]
    pub fn load_raw_array(&mut self, alignment: crate::Alignment) -> Result<Body<'a>> {
        let bytes = self.load::<u32>()?;

        if bytes > crate::buf::MAX_ARRAY_LENGTH {
            return Err(crate::Error::new(crate::error::ErrorKind::ArrayTooLong(
                bytes,
            )));
        }

        self.align_to(alignment)?;
        Ok(self.read_until(bytes as usize))
    }

    /// Advance the read cursor by `n`.
    #[cfg(feature = "alloc")]
    #[inline]
    pub(crate) fn advance(&mut self, n: usize) -> Result<()> {
        self.data.advance(n)
    }

    /// Align the read side of the buffer.
    #[inline]
    pub(crate) fn align<T>(&mut self) -> Result<()> {
        self.data.align::<T>()
    }

    /// Load a slice.
    #[inline]
    pub(crate) fn load_slice(&mut self, len: usize) -> Result<&'a [u8]> {
        self.data.load_slice(len)
    }

    /// Load a slice ending with a NUL byte, excluding the null byte.
    #[inline]
    pub(crate) fn load_slice_nul(&mut self, len: usize) -> Result<&'a [u8]> {
        self.data.load_slice_nul(len)
    }
}

// SAFETY: Body is equivalent to `&[u8]`.
unsafe impl Send for Body<'_> {}
// SAFETY: Body is equivalent to `&[u8]`.
unsafe impl Sync for Body<'_> {}

impl Clone for Body<'_> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            endianness: self.endianness,
            signature: self.signature,
        }
    }
}

impl fmt::Debug for Body<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Body")
            .field("data", &self.data)
            .field("endianness", &self.endianness)
            .finish()
    }
}

impl<'a> PartialEq<Body<'a>> for Body<'_> {
    #[inline]
    fn eq(&self, other: &Body<'a>) -> bool {
        self.get() == other.get() && self.endianness == other.endianness
    }
}

#[cfg(feature = "alloc")]
impl PartialEq<BodyBuf> for Body<'_> {
    #[inline]
    fn eq(&self, other: &BodyBuf) -> bool {
        self.get() == other.get() && self.endianness == other.endianness()
    }
}

impl Eq for Body<'_> {}