Skip to content

Commit a490e78

Browse files
committed
remove allocator support
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 38a8d3f commit a490e78

File tree

1 file changed

+41
-73
lines changed

1 file changed

+41
-73
lines changed

src/lib.rs

Lines changed: 41 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,21 @@
4747
//! [`std::ptr::read`] to get the `OwnedBuf` by value, transform it into the original sequence type,
4848
//! and (implicitly) call its destructor.
4949
//!
50-
//! ## Allocators
51-
//!
52-
//! FIXME(#4)
53-
//!
5450
//! # Examples
5551
//!
5652
//! TODO
5753
//! create from Vec, write into it, read out of it, convert back to Vec, dtor
5854
59-
#![feature(allocator_api)]
6055
#![feature(maybe_uninit_slice)]
6156
#![feature(maybe_uninit_write_slice)]
57+
#![feature(vec_into_raw_parts)]
6258

6359
use core::{
64-
alloc::Allocator,
6560
cmp::{max, min},
6661
mem::{ManuallyDrop, MaybeUninit},
6762
ops, ptr, slice,
6863
};
69-
use std::{
70-
alloc::Global,
71-
io::{Result, Write},
72-
};
64+
use std::io::{Result, Write};
7365

7466
/// An owned, fixed length buffer of bytes.
7567
///
@@ -92,30 +84,29 @@ use std::{
9284
/// The API of `OwnedBuf` is focussed on creation and destruction. To read from the filled part,
9385
/// get a view of the buffer using an `OwnedSlice`. To write into the unfilled part, get a view of
9486
/// the buffer using and `OwnedCursor`. Both of these types can be converted back into an `OwnedBuf`.
95-
pub struct OwnedBuf<A: 'static + Allocator = Global> {
87+
pub struct OwnedBuf {
9688
data: *mut MaybeUninit<u8>,
97-
dtor: unsafe fn(&mut OwnedBuf<A>),
89+
dtor: unsafe fn(&mut OwnedBuf),
9890
capacity: usize,
9991
/// The length of `self.data` which is known to be filled.
10092
filled: usize,
10193
/// The length of `self.data` which is known to be initialized.
10294
init: usize,
103-
allocator: A,
10495
}
10596

106-
impl<A: 'static + Allocator> Into<OwnedSlice<A>> for OwnedBuf<A> {
107-
fn into(self) -> OwnedSlice<A> {
97+
impl Into<OwnedSlice> for OwnedBuf {
98+
fn into(self) -> OwnedSlice {
10899
self.filled()
109100
}
110101
}
111102

112-
impl<A: 'static + Allocator> Into<OwnedCursor<A>> for OwnedBuf<A> {
113-
fn into(self) -> OwnedCursor<A> {
103+
impl Into<OwnedCursor> for OwnedBuf {
104+
fn into(self) -> OwnedCursor {
114105
self.unfilled()
115106
}
116107
}
117108

118-
impl<A: 'static + Allocator> OwnedBuf<A> {
109+
impl OwnedBuf {
119110
/// Create a new OwnedBuf.
120111
///
121112
/// # Safety
@@ -128,56 +119,36 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
128119
capacity: usize,
129120
filled: usize,
130121
init: usize,
131-
) -> OwnedBuf<Global> {
132-
OwnedBuf::new_in(data, dtor, capacity, filled, init, Global)
133-
}
134-
135-
/// Create a new OwnedBuf with a specific allocator.
136-
///
137-
/// # Safety
138-
///
139-
/// See module docs for safety requirements on the destructor function.
140-
#[inline]
141-
pub fn new_in(
142-
data: *mut MaybeUninit<u8>,
143-
dtor: unsafe fn(&mut OwnedBuf<A>),
144-
capacity: usize,
145-
filled: usize,
146-
init: usize,
147-
allocator: A,
148-
) -> OwnedBuf<A> {
122+
) -> OwnedBuf {
149123
OwnedBuf {
150124
data,
151125
dtor,
152126
capacity,
153127
filled,
154128
init,
155-
allocator,
156129
}
157130
}
158131

159132
/// Convert this buffer into a `Vec` of `u8`.
160133
///
161134
/// # Safety
162135
///
163-
/// It is only safe to use this method if the buffer was created from a `Vec<u8, A>`.
136+
/// It is only safe to use this method if the buffer was created from a `Vec<u8>`.
164137
#[inline]
165-
pub unsafe fn into_vec(self) -> Vec<u8, A> {
166-
let a = unsafe { ptr::read(&self.allocator) };
138+
pub unsafe fn into_vec(self) -> Vec<u8> {
167139
let (data, _, filled, _, capacity) = self.into_raw_parts();
168-
Vec::from_raw_parts_in(data as *mut u8, filled, capacity, a)
140+
Vec::from_raw_parts(data as *mut u8, filled, capacity)
169141
}
170142

171143
/// Convert this buffer into a `Vec` of `MaybeUninit<u8>`.
172144
///
173145
/// # Safety
174146
///
175-
/// It is only safe to use this method if the buffer was created from a `Vec<MaybeUninit<u8>, A>`.
147+
/// It is only safe to use this method if the buffer was created from a `Vec<MaybeUninit<u8>>`.
176148
#[inline]
177-
pub unsafe fn into_maybe_uninit_vec(self) -> Vec<MaybeUninit<u8>, A> {
178-
let a = unsafe { ptr::read(&self.allocator) };
149+
pub unsafe fn into_maybe_uninit_vec(self) -> Vec<MaybeUninit<u8>> {
179150
let (data, _, filled, _, capacity) = self.into_raw_parts();
180-
Vec::from_raw_parts_in(data, filled, capacity, a)
151+
Vec::from_raw_parts(data, filled, capacity)
181152
}
182153

183154
/// Returns the length of the initialized part of the buffer.
@@ -188,7 +159,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
188159

189160
/// Returns an `OwnedSlice` covering the filled part of this buffer.
190161
#[inline]
191-
pub fn filled(self) -> OwnedSlice<A> {
162+
pub fn filled(self) -> OwnedSlice {
192163
OwnedSlice {
193164
start: 0,
194165
end: self.filled,
@@ -204,7 +175,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
204175
///
205176
/// This function will panic if `range` is outside the bounds of the filled part of the buffer.
206177
#[inline]
207-
pub fn filled_slice(self, range: impl UsizeRange) -> OwnedSlice<A> {
178+
pub fn filled_slice(self, range: impl UsizeRange) -> OwnedSlice {
208179
let (start, end) = range.absolute_indices(0, self.filled);
209180
OwnedSlice {
210181
start,
@@ -215,7 +186,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
215186

216187
/// Returns a cursor over the unfilled part of the buffer.
217188
#[inline]
218-
pub fn unfilled(self) -> OwnedCursor<A> {
189+
pub fn unfilled(self) -> OwnedCursor {
219190
OwnedCursor {
220191
end: self.capacity,
221192
buf: self,
@@ -232,7 +203,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
232203
/// Where `buf` has a capacity (total length) of 16 bytes and the first 4 bytes are filled,
233204
/// `buf.unfilled_slice(..8)` will return a cursor over the first 4 unfilled bytes.
234205
#[inline]
235-
pub fn unfilled_slice(self, range: ops::RangeTo<usize>) -> OwnedCursor<A> {
206+
pub fn unfilled_slice(self, range: ops::RangeTo<usize>) -> OwnedCursor {
236207
assert!(range.end >= self.filled && range.end <= self.capacity);
237208
OwnedCursor {
238209
end: range.end,
@@ -265,7 +236,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
265236

266237
/// Decomposes this `OwnedBuf` into its raw components.
267238
///
268-
/// returns `(data, dtor, filled, init, capacity)` where `data` is a pointer to the buffer's
239+
/// Returns `(data, dtor, filled, init, capacity)` where `data` is a pointer to the buffer's
269240
/// data in memory, `dtor` is the buffer's destructor function, `filled` is the number of bytes
270241
/// which are filled with data, `init` is the number of bytes which have been initialised (i.e.,
271242
/// which are safe to treat as `u8` rather than `MaybeUninit<u8>`), and `capacity` is the total
@@ -274,13 +245,12 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
274245
/// # Safety
275246
///
276247
/// See module docs for safety requirements on the returned destructor function.
277-
// TODO allocator version
278248
#[inline]
279249
pub fn into_raw_parts(
280250
self,
281251
) -> (
282252
*mut MaybeUninit<u8>,
283-
unsafe fn(&mut OwnedBuf<A>),
253+
unsafe fn(&mut OwnedBuf),
284254
usize,
285255
usize,
286256
usize,
@@ -290,41 +260,39 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
290260
}
291261
}
292262

293-
impl<A: 'static + Allocator> Drop for OwnedBuf<A> {
263+
impl Drop for OwnedBuf {
294264
fn drop(&mut self) {
295265
unsafe { (self.dtor)(self) }
296266
}
297267
}
298268

299-
unsafe fn drop_vec<A: 'static + Allocator>(buf: &mut OwnedBuf<A>) {
269+
unsafe fn drop_vec(buf: &mut OwnedBuf) {
300270
let (data, _, filled, _, capacity) = unsafe { ptr::read(buf) }.into_raw_parts();
301-
let _vec = unsafe { Vec::from_raw_parts_in(data, filled, capacity, ptr::read(&buf.allocator)) };
271+
let _vec = unsafe { Vec::from_raw_parts(data, filled, capacity) };
302272
}
303273

304-
impl<A: 'static + Allocator> From<Vec<MaybeUninit<u8>, A>> for OwnedBuf<A> {
305-
fn from(v: Vec<MaybeUninit<u8>, A>) -> OwnedBuf<A> {
306-
let (data, len, capacity, allocator) = v.into_raw_parts_with_alloc();
274+
impl From<Vec<MaybeUninit<u8>>> for OwnedBuf {
275+
fn from(v: Vec<MaybeUninit<u8>>) -> OwnedBuf {
276+
let (data, len, capacity) = v.into_raw_parts();
307277
OwnedBuf {
308278
data,
309279
dtor: drop_vec,
310280
capacity,
311281
filled: len,
312282
init: len,
313-
allocator,
314283
}
315284
}
316285
}
317286

318-
impl<A: 'static + Allocator> From<Vec<u8, A>> for OwnedBuf<A> {
319-
fn from(v: Vec<u8, A>) -> OwnedBuf<A> {
320-
let (data, len, capacity, allocator) = v.into_raw_parts_with_alloc();
287+
impl From<Vec<u8>> for OwnedBuf {
288+
fn from(v: Vec<u8>) -> OwnedBuf {
289+
let (data, len, capacity) = v.into_raw_parts();
321290
OwnedBuf {
322291
data: data as *mut MaybeUninit<u8>,
323292
dtor: drop_vec,
324293
capacity,
325294
filled: len,
326295
init: len,
327-
allocator,
328296
}
329297
}
330298
}
@@ -345,15 +313,15 @@ impl<A: 'static + Allocator> From<Vec<u8, A>> for OwnedBuf<A> {
345313
/// let slice = buf.filled();
346314
/// assert_eq!(slice[1], 2);
347315
/// ```
348-
pub struct OwnedSlice<A: 'static + Allocator> {
349-
buf: OwnedBuf<A>,
316+
pub struct OwnedSlice {
317+
buf: OwnedBuf,
350318
// Invariant: start <= buf.filled
351319
start: usize,
352320
// Invariant: end >= start && end <= buf.filled
353321
end: usize,
354322
}
355323

356-
impl<A: 'static + Allocator> OwnedSlice<A> {
324+
impl OwnedSlice {
357325
/// Take an (owned) subslice of this `OwnedSlice`.
358326
#[inline]
359327
pub fn slice(self, range: impl UsizeRange) -> Self {
@@ -367,12 +335,12 @@ impl<A: 'static + Allocator> OwnedSlice<A> {
367335

368336
/// Convert this slice back into an `OwnedBuf`.
369337
#[inline]
370-
pub fn into_buf(self) -> OwnedBuf<A> {
338+
pub fn into_buf(self) -> OwnedBuf {
371339
self.buf
372340
}
373341
}
374342

375-
impl<A: 'static + Allocator> ops::Deref for OwnedSlice<A> {
343+
impl ops::Deref for OwnedSlice {
376344
type Target = [u8];
377345

378346
fn deref(&self) -> &[u8] {
@@ -394,16 +362,16 @@ impl<A: 'static + Allocator> ops::Deref for OwnedSlice<A> {
394362
///
395363
/// An `OwnedCursor` takes ownership of the `OwnedBuf` when it is created. It can always be converted
396364
/// back into and `OwnedBuf`.
397-
pub struct OwnedCursor<A: 'static + Allocator> {
398-
buf: OwnedBuf<A>,
365+
pub struct OwnedCursor {
366+
buf: OwnedBuf,
399367
// Invariant: end >= buf.filled && end <= buf.capacity
400368
end: usize,
401369
}
402370

403-
impl<A: 'static + Allocator> OwnedCursor<A> {
371+
impl OwnedCursor {
404372
/// Convert this cursor back into its underlying buffer.
405373
#[inline]
406-
pub fn into_buf(self) -> OwnedBuf<A> {
374+
pub fn into_buf(self) -> OwnedBuf {
407375
self.buf
408376
}
409377

@@ -513,7 +481,7 @@ impl<A: 'static + Allocator> OwnedCursor<A> {
513481
}
514482
}
515483

516-
impl<A: 'static + Allocator> Write for OwnedCursor<A> {
484+
impl Write for OwnedCursor {
517485
fn write(&mut self, buf: &[u8]) -> Result<usize> {
518486
let len = min(self.capacity(), buf.len());
519487
self.write_slice(&buf[..len]);

0 commit comments

Comments
 (0)