47
47
//! [`std::ptr::read`] to get the `OwnedBuf` by value, transform it into the original sequence type,
48
48
//! and (implicitly) call its destructor.
49
49
//!
50
- //! ## Allocators
51
- //!
52
- //! FIXME(#4)
53
- //!
54
50
//! # Examples
55
51
//!
56
52
//! TODO
57
53
//! create from Vec, write into it, read out of it, convert back to Vec, dtor
58
54
59
- #![ feature( allocator_api) ]
60
55
#![ feature( maybe_uninit_slice) ]
61
56
#![ feature( maybe_uninit_write_slice) ]
57
+ #![ feature( vec_into_raw_parts) ]
62
58
63
59
use core:: {
64
- alloc:: Allocator ,
65
60
cmp:: { max, min} ,
66
61
mem:: { ManuallyDrop , MaybeUninit } ,
67
62
ops, ptr, slice,
68
63
} ;
69
- use std:: {
70
- alloc:: Global ,
71
- io:: { Result , Write } ,
72
- } ;
64
+ use std:: io:: { Result , Write } ;
73
65
74
66
/// An owned, fixed length buffer of bytes.
75
67
///
@@ -92,30 +84,29 @@ use std::{
92
84
/// The API of `OwnedBuf` is focussed on creation and destruction. To read from the filled part,
93
85
/// get a view of the buffer using an `OwnedSlice`. To write into the unfilled part, get a view of
94
86
/// 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 {
96
88
data : * mut MaybeUninit < u8 > ,
97
- dtor : unsafe fn ( & mut OwnedBuf < A > ) ,
89
+ dtor : unsafe fn ( & mut OwnedBuf ) ,
98
90
capacity : usize ,
99
91
/// The length of `self.data` which is known to be filled.
100
92
filled : usize ,
101
93
/// The length of `self.data` which is known to be initialized.
102
94
init : usize ,
103
- allocator : A ,
104
95
}
105
96
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 {
108
99
self . filled ( )
109
100
}
110
101
}
111
102
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 {
114
105
self . unfilled ( )
115
106
}
116
107
}
117
108
118
- impl < A : ' static + Allocator > OwnedBuf < A > {
109
+ impl OwnedBuf {
119
110
/// Create a new OwnedBuf.
120
111
///
121
112
/// # Safety
@@ -128,56 +119,36 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
128
119
capacity : usize ,
129
120
filled : usize ,
130
121
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 {
149
123
OwnedBuf {
150
124
data,
151
125
dtor,
152
126
capacity,
153
127
filled,
154
128
init,
155
- allocator,
156
129
}
157
130
}
158
131
159
132
/// Convert this buffer into a `Vec` of `u8`.
160
133
///
161
134
/// # Safety
162
135
///
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>`.
164
137
#[ 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 > {
167
139
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)
169
141
}
170
142
171
143
/// Convert this buffer into a `Vec` of `MaybeUninit<u8>`.
172
144
///
173
145
/// # Safety
174
146
///
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>>`.
176
148
#[ 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 > > {
179
150
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)
181
152
}
182
153
183
154
/// Returns the length of the initialized part of the buffer.
@@ -188,7 +159,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
188
159
189
160
/// Returns an `OwnedSlice` covering the filled part of this buffer.
190
161
#[ inline]
191
- pub fn filled ( self ) -> OwnedSlice < A > {
162
+ pub fn filled ( self ) -> OwnedSlice {
192
163
OwnedSlice {
193
164
start : 0 ,
194
165
end : self . filled ,
@@ -204,7 +175,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
204
175
///
205
176
/// This function will panic if `range` is outside the bounds of the filled part of the buffer.
206
177
#[ inline]
207
- pub fn filled_slice ( self , range : impl UsizeRange ) -> OwnedSlice < A > {
178
+ pub fn filled_slice ( self , range : impl UsizeRange ) -> OwnedSlice {
208
179
let ( start, end) = range. absolute_indices ( 0 , self . filled ) ;
209
180
OwnedSlice {
210
181
start,
@@ -215,7 +186,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
215
186
216
187
/// Returns a cursor over the unfilled part of the buffer.
217
188
#[ inline]
218
- pub fn unfilled ( self ) -> OwnedCursor < A > {
189
+ pub fn unfilled ( self ) -> OwnedCursor {
219
190
OwnedCursor {
220
191
end : self . capacity ,
221
192
buf : self ,
@@ -232,7 +203,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
232
203
/// Where `buf` has a capacity (total length) of 16 bytes and the first 4 bytes are filled,
233
204
/// `buf.unfilled_slice(..8)` will return a cursor over the first 4 unfilled bytes.
234
205
#[ inline]
235
- pub fn unfilled_slice ( self , range : ops:: RangeTo < usize > ) -> OwnedCursor < A > {
206
+ pub fn unfilled_slice ( self , range : ops:: RangeTo < usize > ) -> OwnedCursor {
236
207
assert ! ( range. end >= self . filled && range. end <= self . capacity) ;
237
208
OwnedCursor {
238
209
end : range. end ,
@@ -265,7 +236,7 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
265
236
266
237
/// Decomposes this `OwnedBuf` into its raw components.
267
238
///
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
269
240
/// data in memory, `dtor` is the buffer's destructor function, `filled` is the number of bytes
270
241
/// which are filled with data, `init` is the number of bytes which have been initialised (i.e.,
271
242
/// 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> {
274
245
/// # Safety
275
246
///
276
247
/// See module docs for safety requirements on the returned destructor function.
277
- // TODO allocator version
278
248
#[ inline]
279
249
pub fn into_raw_parts (
280
250
self ,
281
251
) -> (
282
252
* mut MaybeUninit < u8 > ,
283
- unsafe fn ( & mut OwnedBuf < A > ) ,
253
+ unsafe fn ( & mut OwnedBuf ) ,
284
254
usize ,
285
255
usize ,
286
256
usize ,
@@ -290,41 +260,39 @@ impl<A: 'static + Allocator> OwnedBuf<A> {
290
260
}
291
261
}
292
262
293
- impl < A : ' static + Allocator > Drop for OwnedBuf < A > {
263
+ impl Drop for OwnedBuf {
294
264
fn drop ( & mut self ) {
295
265
unsafe { ( self . dtor ) ( self ) }
296
266
}
297
267
}
298
268
299
- unsafe fn drop_vec < A : ' static + Allocator > ( buf : & mut OwnedBuf < A > ) {
269
+ unsafe fn drop_vec ( buf : & mut OwnedBuf ) {
300
270
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) } ;
302
272
}
303
273
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 ( ) ;
307
277
OwnedBuf {
308
278
data,
309
279
dtor : drop_vec,
310
280
capacity,
311
281
filled : len,
312
282
init : len,
313
- allocator,
314
283
}
315
284
}
316
285
}
317
286
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 ( ) ;
321
290
OwnedBuf {
322
291
data : data as * mut MaybeUninit < u8 > ,
323
292
dtor : drop_vec,
324
293
capacity,
325
294
filled : len,
326
295
init : len,
327
- allocator,
328
296
}
329
297
}
330
298
}
@@ -345,15 +313,15 @@ impl<A: 'static + Allocator> From<Vec<u8, A>> for OwnedBuf<A> {
345
313
/// let slice = buf.filled();
346
314
/// assert_eq!(slice[1], 2);
347
315
/// ```
348
- pub struct OwnedSlice < A : ' static + Allocator > {
349
- buf : OwnedBuf < A > ,
316
+ pub struct OwnedSlice {
317
+ buf : OwnedBuf ,
350
318
// Invariant: start <= buf.filled
351
319
start : usize ,
352
320
// Invariant: end >= start && end <= buf.filled
353
321
end : usize ,
354
322
}
355
323
356
- impl < A : ' static + Allocator > OwnedSlice < A > {
324
+ impl OwnedSlice {
357
325
/// Take an (owned) subslice of this `OwnedSlice`.
358
326
#[ inline]
359
327
pub fn slice ( self , range : impl UsizeRange ) -> Self {
@@ -367,12 +335,12 @@ impl<A: 'static + Allocator> OwnedSlice<A> {
367
335
368
336
/// Convert this slice back into an `OwnedBuf`.
369
337
#[ inline]
370
- pub fn into_buf ( self ) -> OwnedBuf < A > {
338
+ pub fn into_buf ( self ) -> OwnedBuf {
371
339
self . buf
372
340
}
373
341
}
374
342
375
- impl < A : ' static + Allocator > ops:: Deref for OwnedSlice < A > {
343
+ impl ops:: Deref for OwnedSlice {
376
344
type Target = [ u8 ] ;
377
345
378
346
fn deref ( & self ) -> & [ u8 ] {
@@ -394,16 +362,16 @@ impl<A: 'static + Allocator> ops::Deref for OwnedSlice<A> {
394
362
///
395
363
/// An `OwnedCursor` takes ownership of the `OwnedBuf` when it is created. It can always be converted
396
364
/// back into and `OwnedBuf`.
397
- pub struct OwnedCursor < A : ' static + Allocator > {
398
- buf : OwnedBuf < A > ,
365
+ pub struct OwnedCursor {
366
+ buf : OwnedBuf ,
399
367
// Invariant: end >= buf.filled && end <= buf.capacity
400
368
end : usize ,
401
369
}
402
370
403
- impl < A : ' static + Allocator > OwnedCursor < A > {
371
+ impl OwnedCursor {
404
372
/// Convert this cursor back into its underlying buffer.
405
373
#[ inline]
406
- pub fn into_buf ( self ) -> OwnedBuf < A > {
374
+ pub fn into_buf ( self ) -> OwnedBuf {
407
375
self . buf
408
376
}
409
377
@@ -513,7 +481,7 @@ impl<A: 'static + Allocator> OwnedCursor<A> {
513
481
}
514
482
}
515
483
516
- impl < A : ' static + Allocator > Write for OwnedCursor < A > {
484
+ impl Write for OwnedCursor {
517
485
fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize > {
518
486
let len = min ( self . capacity ( ) , buf. len ( ) ) ;
519
487
self . write_slice ( & buf[ ..len] ) ;
0 commit comments