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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use core::alloc::Layout;
use core::cmp::Ordering;
use core::ffi::c_int;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::mem;
use core::ops::Deref;
use core::ptr::NonNull;
use core::slice;
use alloc::alloc::handle_alloc_error;
use crate::ffi;
use crate::{Code, Error, Result};
const MAX_CAP: usize = if mem::size_of::<isize>() > mem::size_of::<c_int>() {
c_int::MAX as usize
} else {
isize::MAX as usize
};
struct AllocError;
impl fmt::Display for AllocError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to allocate memory")
}
}
/// A slice of bytes that has been allocated with the sqlite allocator.
///
/// This dereferences to a byte slice.
pub struct OwnedBytes {
ptr: NonNull<u8>,
len: usize,
cap: usize,
}
impl OwnedBytes {
/// Creates a new, empty `OwnedBytes`.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let bytes = OwnedBytes::new();
/// assert_eq!(bytes.len(), 0);
/// assert!(bytes.is_empty());
/// # Ok::<_, sqll::Error>(())
/// ```
pub const fn new() -> Self {
Self {
ptr: NonNull::dangling(),
len: 0,
cap: 0,
}
}
/// Creates a new `OwnedBytes` with the specified capacity.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let bytes = OwnedBytes::with_capacity(100)?;
/// assert!(bytes.capacity() >= 100);
/// # Ok::<_, sqll::Error>(())
/// ```
pub fn with_capacity(cap: usize) -> Result<Self> {
let mut this = Self::new();
if let Err(error) = this.reserve(cap) {
return Err(Error::new(Code::NOMEM, error));
}
Ok(this)
}
/// Returns the length of the byte slice.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let mut bytes = OwnedBytes::new();
/// assert_eq!(bytes.len(), 0);
///
/// bytes.extend_from_slice(b"hello ")?;
/// assert_eq!(bytes.len(), 6);
///
/// assert_eq!(bytes[..], b"hello "[..]);
/// bytes.extend_from_slice(b"world")?;
/// assert_eq!(bytes.len(), 11);
/// assert_eq!(bytes[..], b"hello world"[..]);
/// # Ok::<_, sqll::Error>(())
/// ```
pub fn len(&self) -> usize {
self.len
}
/// Returns `true` if the byte slice is empty.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let mut bytes = OwnedBytes::new();
/// assert!(bytes.is_empty());
///
/// bytes.extend_from_slice(b"hello world")?;
/// assert!(!bytes.is_empty());
/// # Ok::<_, sqll::Error>(())
/// ```
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Creates a new `OwnedBytes` from the given slice, copying the provided
/// data into it.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let mut bytes = OwnedBytes::new();
/// bytes.extend_from_slice(b"hello world")?;
/// assert_eq!(bytes[..], b"hello world"[..]);
/// # Ok::<_, sqll::Error>(())
/// ```
pub fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<()> {
if let Err(error) = self.try_extend_from_slice(bytes) {
return Err(Error::new(Code::NOMEM, error));
}
Ok(())
}
fn try_extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), AllocError> {
self.reserve(bytes.len())?;
// SAFETY: We trust that the provided sqlite_* methods work as
// advertised, and are abiding by the guarantees provided by the passed
// in slice.
unsafe {
bytes
.as_ptr()
.copy_to_nonoverlapping(self.ptr.as_ptr().add(self.len), bytes.len());
self.len += bytes.len();
Ok(())
}
}
/// Returns the capacity of the byte slice.
///
/// # Examples
///
/// ```
/// use sqll::OwnedBytes;
///
/// let bytes = OwnedBytes::with_capacity(100)?;
/// assert!(bytes.capacity() >= 100);
/// # Ok::<_, sqll::Error>(())
/// ```
#[inline]
pub fn capacity(&self) -> usize {
self.cap
}
/// Creates a new `OwnedBytes` from the given raw parts.
///
/// # Safety
///
/// The caller must ensure that the provided pointer is initialized up to
/// `len`, and that it has been constructed using the sqlite allocator.
#[inline]
pub(super) unsafe fn from_raw(ptr: NonNull<u8>, len: usize) -> Self {
Self { ptr, len, cap: len }
}
fn reserve(&mut self, additional: usize) -> Result<(), AllocError> {
fn checked_grow(base: usize, needed: usize) -> Option<usize> {
let new_cap = base.checked_add(needed)?.max(16);
let new_cap = match new_cap.checked_next_power_of_two() {
Some(cap) => cap,
None if new_cap <= MAX_CAP => new_cap,
None => return None,
};
Some(new_cap)
}
unsafe {
let Some(new_cap) = checked_grow(self.len, additional) else {
return Err(AllocError);
};
if new_cap < self.cap {
return Ok(());
}
let ptr = if self.cap == 0 {
ffi::sqlite3_malloc(new_cap as c_int)
} else {
ffi::sqlite3_realloc(self.ptr.as_ptr().cast(), new_cap as c_int)
};
if ptr.is_null() {
return Err(AllocError);
}
self.ptr = NonNull::new_unchecked(ptr.cast());
self.cap = new_cap;
Ok(())
}
}
}
impl Clone for OwnedBytes {
#[inline]
fn clone(&self) -> Self {
let mut this = Self::new();
if let Err(AllocError) = this.try_extend_from_slice(self) {
let layout = unsafe { Layout::from_size_align_unchecked(self.len, 1) };
handle_alloc_error(layout);
}
this
}
}
impl fmt::Debug for OwnedBytes {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self[..].fmt(f)
}
}
impl AsRef<[u8]> for OwnedBytes {
#[inline]
fn as_ref(&self) -> &[u8] {
self
}
}
impl Deref for OwnedBytes {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
}
}
impl Drop for OwnedBytes {
#[inline]
fn drop(&mut self) {
if self.cap == 0 {
return;
}
// SAFETY: All ways we have to construct OwnedBytes require that it's
// done through sqlite's allocator.
unsafe {
ffi::sqlite3_free(self.ptr.as_ptr().cast());
}
}
}
impl PartialEq for OwnedBytes {
#[inline]
fn eq(&self, other: &Self) -> bool {
self[..] == other[..]
}
}
impl Eq for OwnedBytes {}
impl PartialOrd for OwnedBytes {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OwnedBytes {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self[..].cmp(&other[..])
}
}
impl Hash for OwnedBytes {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self[..].hash(state);
}
}
impl PartialEq<[u8]> for OwnedBytes {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
self[..] == other[..]
}
}
impl PartialEq<OwnedBytes> for [u8] {
#[inline]
fn eq(&self, other: &OwnedBytes) -> bool {
self[..] == other[..]
}
}