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
use crate::{std::fmt, tags, Result, Stream, Value};

/**
An adapter that streams a slice of 8bit unsigned integers as binary.

For fixed-size arrays, see the [`BinaryArray`] type.
*/
#[repr(transparent)]
pub struct BinarySlice([u8]);

impl BinarySlice {
    /**
    Treat a slice of 8bit unsigned integers as binary.
    */
    #[inline(always)]
    pub const fn new<'a>(binary: &'a [u8]) -> &'a Self {
        // SAFETY: `Binary` and `[u8]` have the same ABI
        unsafe { &*(binary as *const _ as *const BinarySlice) }
    }

    /**
    Get a reference to the underlying slice.
    */
    #[inline(always)]
    pub const fn as_slice(&self) -> &[u8] {
        &self.0
    }
}

impl fmt::Debug for BinarySlice {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl AsRef<[u8]> for BinarySlice {
    #[inline(always)]
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Value for BinarySlice {
    fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
        stream.binary_begin(Some(self.0.len()))?;
        stream.binary_fragment(&self.0)?;
        stream.binary_end()
    }

    #[inline(always)]
    fn to_binary(&self) -> Option<&[u8]> {
        Some(&self.0)
    }
}

/**
An adapter that streams a slice of 8bit unsigned integers as binary with a fixed size.

This type is like [`BinarySlice`], but for fixed-size arrays.
*/
#[repr(transparent)]
pub struct BinaryArray<const N: usize>([u8; N]);

impl<const N: usize> BinaryArray<N> {
    /**
    Treat a slice of 8bit unsigned integers as binary.
    */
    #[inline(always)]
    pub const fn new<'a>(binary: &'a [u8; N]) -> &'a Self {
        // SAFETY: `Binary` and `[u8; N]` have the same ABI
        unsafe { &*(binary as *const _ as *const BinaryArray<N>) }
    }

    /**
    Get a reference to the underlying slice.
    */
    #[inline(always)]
    pub const fn as_slice(&self) -> &[u8; N] {
        &self.0
    }
}

impl<const N: usize> fmt::Debug for BinaryArray<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl<const N: usize> AsRef<[u8; N]> for BinaryArray<N> {
    #[inline(always)]
    fn as_ref(&self) -> &[u8; N] {
        &self.0
    }
}

impl<const N: usize> Value for BinaryArray<N> {
    fn stream<'sval, S: Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> Result {
        stream.tagged_begin(Some(&tags::CONSTANT_SIZE), None, None)?;
        stream.binary_begin(Some(self.0.len()))?;
        stream.binary_fragment(&self.0)?;
        stream.binary_end()?;
        stream.tagged_end(Some(&tags::CONSTANT_SIZE), None, None)
    }

    #[inline(always)]
    fn to_binary(&self) -> Option<&[u8]> {
        Some(&self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn binary_cast() {
        assert_eq!(Some(b"abc" as &[u8]), BinarySlice::new(b"abc").to_binary());
        assert_eq!(Some(b"abc" as &[u8]), BinaryArray::new(b"abc").to_binary());
    }
}