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
use std::fmt::{Debug, Formatter};
use std::mem;
use std::ops::Deref;

use bytecheck::CheckBytes;
use rkyv::de::deserializers::SharedDeserializeMap;
use rkyv::validation::validators::DefaultValidator;
use rkyv::{AlignedVec, Archive, Deserialize};

#[derive(Debug, thiserror::Error)]
#[error("View cannot be made for type with provided data.")]
/// The data provided is unable to be presented as the archived version
/// of the view type.
pub struct InvalidView;

/// A block of data that can be accessed as if it is the archived value `T`.
///
/// This allows for safe, true zero-copy deserialization avoiding unnecessary
/// allocations if the situation does not require having an owned version of the value.
pub struct DataView<T, D = AlignedVec>
where
    T: Archive,
    T::Archived: 'static,
    D: Deref<Target = [u8]> + Send + Sync,
{
    /// The owned buffer itself.
    ///
    /// This must live as long as the view derived from it.
    data: D,

    /// The view reference which lives as long as `data: D`.
    view: &'static rkyv::Archived<T>,
}

impl<T, D> DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
    D: Deref<Target = [u8]> + Send + Sync,
{
    /// Creates a new view using a provided buffer.
    pub(crate) fn using(data: D) -> Result<Self, InvalidView> {
        // SAFETY:
        //  This is safe as we own the data and keep it apart
        //  of the view itself.
        let extended_buf = unsafe { mem::transmute::<&[u8], &'static [u8]>(&data) };

        let view =
            rkyv::check_archived_root::<'_, T>(extended_buf).map_err(|_| InvalidView)?;

        Ok(Self { data, view })
    }

    /// Gets the bytes representation of the dataview.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }
}

impl<T, D> DataView<T, D>
where
    T: Archive,
    T::Archived: Deserialize<T, SharedDeserializeMap> + 'static,
    D: Deref<Target = [u8]> + Send + Sync,
{
    /// Deserializes the view into it's owned value T.
    pub fn to_owned(&self) -> Result<T, InvalidView> {
        self.view
            .deserialize(&mut SharedDeserializeMap::default())
            .map_err(|_| InvalidView)
    }
}

impl<T, D> Clone for DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>> + Debug,
    D: Deref<Target = [u8]> + Send + Sync + Clone,
{
    fn clone(&self) -> Self {
        Self::using(self.data.clone()).expect("BUG: Valid data has become invalid?")
    }
}

impl<T, D> Debug for DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>> + Debug,
    D: Deref<Target = [u8]> + Send + Sync,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.view.fmt(f)
    }
}

impl<T, D> Deref for DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>>,
    D: Deref<Target = [u8]> + Send + Sync,
{
    type Target = T::Archived;

    fn deref(&self) -> &Self::Target {
        self.view
    }
}

impl<T, D> PartialEq for DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>> + PartialEq,
    D: Deref<Target = [u8]> + Send + Sync,
{
    fn eq(&self, other: &Self) -> bool {
        self.view == other.view
    }
}

impl<T, D> PartialEq<T> for DataView<T, D>
where
    T: Archive,
    T::Archived: CheckBytes<DefaultValidator<'static>> + PartialEq<T>,
    D: Deref<Target = [u8]> + Send + Sync,
{
    fn eq(&self, other: &T) -> bool {
        self.view == other
    }
}

#[cfg(test)]
mod tests {
    use bytecheck::CheckBytes;
    use rkyv::{Archive, Deserialize, Serialize};

    use super::*;

    #[repr(C)]
    #[derive(Serialize, Deserialize, Archive, PartialEq, Eq, Debug)]
    #[archive(compare(PartialEq))]
    #[archive_attr(derive(CheckBytes, Debug, PartialEq, Eq))]
    struct Demo {
        a: String,
        b: u64,
    }

    #[test]
    fn test_view() {
        let demo = Demo {
            a: "Jello".to_string(),
            b: 133,
        };

        let bytes = rkyv::to_bytes::<_, 1024>(&demo).unwrap();
        let view: DataView<Demo, _> = DataView::using(bytes).unwrap();
        assert!(view == demo, "Original and view must match.");
    }

    #[test]
    fn test_invalid_view() {
        let res = DataView::<Demo, _>::using(b"Hello, world!".to_vec());
        assert!(res.is_err(), "View should be rejected");
    }

    #[test]
    fn test_deserialize() {
        let demo = Demo {
            a: "Jello".to_string(),
            b: 133,
        };

        let bytes = rkyv::to_bytes::<_, 1024>(&demo).unwrap();
        let view: DataView<Demo, _> = DataView::using(bytes).unwrap();
        assert!(view == demo, "Original and view must match.");

        let value = view.to_owned().unwrap();
        assert_eq!(value, demo, "Deserialized and original value should match.")
    }
}