@@ -276,10 +276,6 @@ impl<T, A: Allocator> RawVec<T, A> {
276
276
/// *O*(1) behavior. Will limit this behavior if it would needlessly cause
277
277
/// itself to panic.
278
278
///
279
- /// If `len` exceeds `self.capacity()`, this may fail to actually allocate
280
- /// the requested space. This is not really unsafe, but the unsafe
281
- /// code *you* write that relies on the behavior of this function may break.
282
- ///
283
279
/// This is ideal for implementing a bulk-push operation like `extend`.
284
280
///
285
281
/// # Panics
@@ -289,9 +285,13 @@ impl<T, A: Allocator> RawVec<T, A> {
289
285
/// # Aborts
290
286
///
291
287
/// Aborts on OOM.
288
+ ///
289
+ /// # Safety
290
+ ///
291
+ /// `len` must be less than or equal to the capacity of this [`RawVec`].
292
292
#[ cfg( not( no_global_oom_handling) ) ]
293
293
#[ inline]
294
- pub fn reserve ( & mut self , len : usize , additional : usize ) {
294
+ pub unsafe fn reserve ( & mut self , len : usize , additional : usize ) {
295
295
// Callers expect this function to be very cheap when there is already sufficient capacity.
296
296
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
297
297
// handle_reserve behind a call, while making sure that this function is likely to be
@@ -305,7 +305,7 @@ impl<T, A: Allocator> RawVec<T, A> {
305
305
handle_reserve ( slf. grow_amortized ( len, additional) ) ;
306
306
}
307
307
308
- if self . needs_to_grow ( len, additional) {
308
+ if unsafe { self . needs_to_grow ( len, additional) } {
309
309
do_reserve_and_handle ( self , len, additional) ;
310
310
}
311
311
unsafe {
@@ -323,8 +323,16 @@ impl<T, A: Allocator> RawVec<T, A> {
323
323
}
324
324
325
325
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
326
- pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
327
- if self . needs_to_grow ( len, additional) {
326
+ ///
327
+ /// # Safety
328
+ ///
329
+ /// `len` must be less than or equal to the capacity of this [`RawVec`].
330
+ pub unsafe fn try_reserve (
331
+ & mut self ,
332
+ len : usize ,
333
+ additional : usize ,
334
+ ) -> Result < ( ) , TryReserveError > {
335
+ if unsafe { self . needs_to_grow ( len, additional) } {
328
336
self . grow_amortized ( len, additional) ?;
329
337
}
330
338
unsafe {
@@ -340,29 +348,35 @@ impl<T, A: Allocator> RawVec<T, A> {
340
348
/// exactly the amount of memory necessary, but in principle the allocator
341
349
/// is free to give back more than we asked for.
342
350
///
343
- /// If `len` exceeds `self.capacity()`, this may fail to actually allocate
344
- /// the requested space. This is not really unsafe, but the unsafe code
345
- /// *you* write that relies on the behavior of this function may break.
346
- ///
347
351
/// # Panics
348
352
///
349
353
/// Panics if the new capacity exceeds `isize::MAX` _bytes_.
350
354
///
351
355
/// # Aborts
352
356
///
353
357
/// Aborts on OOM.
358
+ ///
359
+ /// # Safety
360
+ ///
361
+ /// `len` must be less than or equal to the capacity of this [`RawVec`].
354
362
#[ cfg( not( no_global_oom_handling) ) ]
355
- pub fn reserve_exact ( & mut self , len : usize , additional : usize ) {
356
- handle_reserve ( self . try_reserve_exact ( len, additional) ) ;
363
+ pub unsafe fn reserve_exact ( & mut self , len : usize , additional : usize ) {
364
+ unsafe {
365
+ handle_reserve ( self . try_reserve_exact ( len, additional) ) ;
366
+ }
357
367
}
358
368
359
369
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
360
- pub fn try_reserve_exact (
370
+ ///
371
+ /// # Safety
372
+ ///
373
+ /// `len` must be less than or equal to the capacity of this [`RawVec`].
374
+ pub unsafe fn try_reserve_exact (
361
375
& mut self ,
362
376
len : usize ,
363
377
additional : usize ,
364
378
) -> Result < ( ) , TryReserveError > {
365
- if self . needs_to_grow ( len, additional) {
379
+ if unsafe { self . needs_to_grow ( len, additional) } {
366
380
self . grow_exact ( len, additional) ?;
367
381
}
368
382
unsafe {
@@ -391,8 +405,8 @@ impl<T, A: Allocator> RawVec<T, A> {
391
405
impl < T , A : Allocator > RawVec < T , A > {
392
406
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
393
407
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
394
- pub ( crate ) fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
395
- additional > self . capacity ( ) . wrapping_sub ( len)
408
+ pub ( crate ) unsafe fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
409
+ unsafe { additional > self . capacity ( ) . unchecked_sub ( len) }
396
410
}
397
411
398
412
/// # Safety:
0 commit comments