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
use crate::{Error, Packed, Packet};
use std::{any, fmt, hash, marker};

/// view of a slice in memory as a packed structure of type `T`
pub struct View<'a, T: ?Sized> {
    slice: &'a [u8],
    marker: marker::PhantomData<T>,
}

impl<'a, T> View<'a, T>
where
    T: Packed,
{
    /// create the [`View`] from the slice
    /// without performing any checks
    #[inline]
    pub(crate) fn new(slice: &'a [u8]) -> Self {
        Self {
            slice,
            marker: marker::PhantomData,
        }
    }

    /// reconstruct the object `T` from the given [`View`]
    ///
    /// this function will involve some hoops and loops and may
    /// involve some heap allocation.
    #[inline]
    #[must_use = "this will clone data from the slice, it is often expensive"]
    pub fn unpack(self) -> T {
        T::unchecked_read_from_slice(self.slice)
    }

    /// create a clone of the given slice that is going to be owned
    /// by the given [`Packet`].
    ///
    /// Only do that if you need to hold onto the serialized data.
    #[inline]
    #[must_use = "this will copy the memory slice"]
    pub fn to_owned(self) -> Packet<T> {
        Packet::new(self.slice.to_owned().into_boxed_slice())
    }

    /// create a [`View`] from the given slice.
    ///
    /// this function will perform all the necessary checks
    /// in order to make sure there's no invalid data.
    ///
    /// Once the [`View`] is created, it is possible to use it
    /// safely across the board.
    pub fn try_from_slice(slice: &'a [u8]) -> Result<Self, Error> {
        if T::SIZE != slice.len() {
            return Err(Error::invalid_size::<T>(slice.len(), T::SIZE));
        }

        T::check(slice)?;
        Ok(View::new(slice))
    }
}

impl<'a, T> Clone for View<'a, T> {
    fn clone(&self) -> Self {
        Self {
            slice: self.slice,
            marker: self.marker,
        }
    }
}

impl<'a, T> Copy for View<'a, T> {}

impl<'a, T> AsRef<[u8]> for View<'a, T> {
    fn as_ref(&self) -> &[u8] {
        self.slice
    }
}

impl<'a, 'b, T, U> PartialEq<View<'b, U>> for View<'a, T> {
    fn eq(&self, other: &View<'b, U>) -> bool {
        self.slice.eq(other.slice)
    }
}

impl<'a, T> Eq for View<'a, T> {}

impl<'a, 'b, T, U> PartialOrd<View<'b, U>> for View<'a, T> {
    fn partial_cmp(&self, other: &View<'b, U>) -> Option<std::cmp::Ordering> {
        self.slice.partial_cmp(other.slice)
    }
}

impl<'a, T> Ord for View<'a, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.slice.cmp(other.slice)
    }
}

impl<'a, T> hash::Hash for View<'a, T> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.slice.hash(state);
        self.marker.hash(state);
    }
}

impl<'a, T> fmt::Debug for View<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ty = any::type_name::<T>();
        f.debug_struct(&format!("View<'a, {}>", ty))
            .field("slice", &self.slice)
            .field("marker", &self.marker)
            .finish()
    }
}