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
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Lesser General Public License that can be found
// in the LICENSE file.
/// Data holds an immutable data buffer.
///
/// Not only is the data immutable, but the actual ptr that is returned
/// (by `data()` or `bytes()`) is guaranteed to always be the same for the life
/// of this instance.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Data {
data: Vec<u8>,
}
impl Data {
/// Returns the number of bytes stored.
#[must_use]
pub fn len(&self) -> usize {
self.data.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Returns the ptr to the data.
#[must_use]
pub fn data(&self) -> &[u8] {
&self.data
}
/// Like `data()`, returns a read-only ptr into the data
#[must_use]
pub fn bytes(&self) -> &[u8] {
&self.data
}
/**
* USE WITH CAUTION.
* This call will assert that the refcnt is 1, as a precaution against modifying the
* contents when another client/thread has access to the data.
*/
pub fn data_mut(&mut self) -> &mut [u8] {
&mut self.data
}
/// Helper to copy a range of the data into a caller-provided buffer.
///
/// Returns the actual number of bytes copied, after clamping offset and
/// length to the size of the data. If buffer is empty, it is ignored, and
/// only the computed number of bytes is returned.
#[must_use]
pub fn copy_range(&self, _offset: usize, _buffer: &mut [u8]) -> usize {
unimplemented!()
}
/// Create a new dataref by copying the specified data
#[must_use]
pub fn from(data: &[u8]) -> Self {
Self {
data: data.to_vec(),
}
}
#[must_use]
pub fn with_capacity(cap: usize) -> Self {
Self {
data: Vec::with_capacity(cap),
}
}
/// Create a new data with zero-initialized contents.
///
/// The caller should call `writable_data()` to write into the buffer,
/// but this must be done before another `ref()` is made.
#[must_use]
pub fn with_zero_initialized(len: usize) -> Self {
Self { data: vec![0; len] }
}
/// Create a new dataref by copying the specified string.
///
/// The returned Data will have `size()` equal to length of string.
#[must_use]
pub fn from_string(s: &str) -> Self {
Self::from(s.as_bytes())
}
/// Call this when the data parameter is already const and will outlive
/// the lifetime of the Data.
///
/// Suitable for with const globals.
#[must_use]
pub fn from_data(data: Vec<u8>) -> Self {
Self { data }
}
/// Create a new dataref using a subset of the data in the specified src dataref.
#[must_use]
pub fn new_subset(src: &Self, offset: usize, length: usize) -> Self {
Self {
data: src.data[offset..offset + length].to_vec(),
}
}
/// Returns a new empty dataref (or a reference to a shared empty dataref).
/// New or shared, the caller must see that `unref()` is eventually called.
#[must_use]
pub const fn new() -> Self {
Self { data: Vec::new() }
}
}