-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.rs
147 lines (128 loc) · 3.67 KB
/
data.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
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
// Copyright (C) 2020 Matthew Waters <matthew@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Data handling
//!
//! Provides a CoW interface for slices of `[u8]` and `Box<[u8]>`
/// A slice of data
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct DataSlice<'a>(&'a [u8]);
impl<'a> DataSlice<'a> {
/// Consume this slice and return the underlying data.
pub fn take(self) -> &'a [u8] {
self.0
}
/// Copy this borrowed slice into a new owned allocation.
pub fn to_owned(&self) -> DataOwned {
DataOwned(self.0.into())
}
}
impl<'a> std::ops::Deref for DataSlice<'a> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.0
}
}
impl<'a> From<DataSlice<'a>> for &'a [u8] {
fn from(value: DataSlice<'a>) -> Self {
value.0
}
}
impl<'a> From<&'a [u8]> for DataSlice<'a> {
fn from(value: &'a [u8]) -> Self {
Self(value)
}
}
/// An owned piece of data
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct DataOwned(Box<[u8]>);
impl DataOwned {
/// Consume this slice and return the underlying data.
pub fn take(self) -> Box<[u8]> {
self.0
}
}
impl std::ops::Deref for DataOwned {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<DataOwned> for Box<[u8]> {
fn from(value: DataOwned) -> Self {
value.0
}
}
impl From<Box<[u8]>> for DataOwned {
fn from(value: Box<[u8]>) -> Self {
Self(value)
}
}
/// An owned or borrowed piece of data
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Data<'a> {
Borrowed(DataSlice<'a>),
Owned(DataOwned),
}
impl<'a> std::ops::Deref for Data<'a> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
match self {
Self::Borrowed(data) => data.0,
Self::Owned(data) => &data.0,
}
}
}
impl<'a> Data<'a> {
/// Create a new owned version of this data
pub fn into_owned<'b>(self) -> Data<'b> {
match self {
Self::Borrowed(data) => Data::Owned(data.to_owned()),
Self::Owned(data) => Data::Owned(data),
}
}
}
impl<'a> From<&'a [u8]> for Data<'a> {
fn from(value: &'a [u8]) -> Self {
Self::Borrowed(value.into())
}
}
impl<'a> From<Box<[u8]>> for Data<'a> {
fn from(value: Box<[u8]>) -> Self {
Self::Owned(value.into())
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[test]
fn data_access() {
let _log = crate::tests::test_init_log();
let array = [0, 1, 2, 3];
let borrowed_data = Data::from(array.as_slice());
assert_eq!(array.as_slice(), &*borrowed_data);
let owned_data = borrowed_data.into_owned();
assert_eq!(array.as_slice(), &*owned_data);
let Data::Owned(owned) = owned_data else {
unreachable!();
};
let inner = <Box<[u8]>>::from(owned.clone());
assert_eq!(array.as_slice(), &*inner);
let owned = DataOwned::take(owned);
assert_eq!(array.as_slice(), &*owned);
let data = Data::from(owned);
assert_eq!(array.as_slice(), &*data);
let borrowed = DataSlice::from(&*data);
assert_eq!(array.as_slice(), &*borrowed);
let inner = <&[u8]>::from(borrowed.clone());
assert_eq!(array.as_slice(), inner);
let inner = borrowed.take();
assert_eq!(array.as_slice(), inner);
}
}