-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
117 lines (99 loc) · 2.43 KB
/
mod.rs
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
mod builder;
use alloc::{boxed::Box, vec::Vec};
use core::marker::PhantomData;
pub use builder::ByteBuilder;
/// A typed slice of bytes
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ByteSlice<'a, T> {
slice: &'a [u8],
phantom: PhantomData<T>,
}
/// A typed Array of bytes
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ByteArray<T: ?Sized> {
array: Box<[u8]>,
phantom: PhantomData<T>,
}
impl<T> AsRef<[u8]> for ByteArray<T> {
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a, T> AsRef<[u8]> for ByteSlice<'a, T> {
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl<T> ByteArray<T> {
pub fn as_byteslice(&self) -> ByteSlice<'_, T> {
ByteSlice {
slice: &self.array[..],
phantom: self.phantom,
}
}
pub fn as_slice(&self) -> &[u8] {
&self.array[..]
}
fn from_vec(v: Vec<u8>) -> Self {
ByteArray {
array: v.into(),
phantom: PhantomData,
}
}
}
impl<T> From<Vec<u8>> for ByteArray<T> {
fn from(v: Vec<u8>) -> Self {
ByteArray::from_vec(v)
}
}
impl<'a, T> ByteSlice<'a, T> {
pub fn as_slice(&self) -> &[u8] {
self.slice
}
fn sub_byteslice<U>(&'a self, start: usize, size: usize) -> ByteSlice<'a, U> {
ByteSlice {
slice: &self.slice[start..start + size],
phantom: PhantomData,
}
}
}
pub trait ByteAccessor<A> {
const START_SIZE: (usize, usize);
}
impl<T> ByteArray<T> {
pub fn sub<U>(&self) -> ByteSlice<'_, U>
where
T: ByteAccessor<U>,
{
let (start, size) = <T as ByteAccessor<U>>::START_SIZE;
ByteSlice {
slice: &self.array[start..start + size],
phantom: PhantomData,
}
}
}
impl<'a, T> ByteSlice<'a, T> {
pub fn sub<U>(&'a self) -> ByteSlice<'a, U>
where
T: ByteAccessor<U>,
{
let (start, sz) = <T as ByteAccessor<U>>::START_SIZE;
self.sub_byteslice(start, sz)
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
pub struct Big;
pub struct Little;
impl ByteAccessor<Little> for Big {
const START_SIZE: (usize, usize) = (2, 3);
}
#[test]
pub fn typed_accessor() {
let v: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let b: ByteArray<Big> = v.into();
assert_eq!(b.sub::<Little>().as_slice(), [2, 3, 4])
}
}