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
use std::ops::{Range, RangeBounds};
use std::sync::Arc;
#[derive(Clone)]
pub struct Bytes {
ptr: *const u8,
len: usize,
owner: Option<Arc<dyn BytesOwner>>,
}
pub trait BytesOwner: AsRef<[u8]> + Send + Sync + 'static {}
unsafe impl Send for Bytes {}
unsafe impl Sync for Bytes {}
impl Bytes {
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
use std::ops::Bound;
let start = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&n) => n + 1,
Bound::Excluded(&n) => n,
Bound::Unbounded => self.len,
};
assert!(start <= end, "invalid slice {}..{}", start, end);
assert!(end <= self.len, "{} exceeds Bytes length {}", end, self.len);
if start == end {
Self::new()
} else {
Self {
ptr: unsafe { self.ptr.offset(start as isize) },
len: end - start,
owner: self.owner.clone(),
}
}
}
pub fn slice_to_bytes(&self, slice: &[u8]) -> Self {
match self.range_of_slice(slice) {
Some(range) => self.slice(range),
None => Self::copy_from_slice(slice),
}
}
pub fn range_of_slice(&self, slice: &[u8]) -> Option<Range<usize>> {
let slice_start = slice.as_ptr() as usize;
let slice_end = slice_start + slice.len();
let bytes_start = self.ptr as usize;
let bytes_end = bytes_start + self.len;
if slice_start >= bytes_start && slice_end <= bytes_end {
let start = slice_start - bytes_start;
Some(start..start + slice.len())
} else {
None
}
}
#[inline]
pub fn new() -> Self {
Self::from_static(b"")
}
#[inline]
pub fn from_static(value: &'static [u8]) -> Self {
let slice: &[u8] = value.as_ref();
Self {
ptr: slice.as_ptr(),
len: slice.len(),
owner: None,
}
}
pub fn from_owner(value: impl BytesOwner) -> Self {
let slice: &[u8] = value.as_ref();
Self {
ptr: slice.as_ptr(),
len: slice.len(),
owner: Some(Arc::new(value)),
}
}
pub fn copy_from_slice(data: &[u8]) -> Self {
Self::from_owner(data.to_vec())
}
#[inline]
pub(crate) fn as_slice(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}
}