@@ -276,6 +276,10 @@ 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
+ ///
279
283
/// This is ideal for implementing a bulk-push operation like `extend`.
280
284
///
281
285
/// # Panics
@@ -285,13 +289,9 @@ impl<T, A: Allocator> RawVec<T, A> {
285
289
/// # Aborts
286
290
///
287
291
/// 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 unsafe fn reserve ( & mut self , len : usize , additional : usize ) {
294
+ pub 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 unsafe { self . needs_to_grow ( len, additional) } {
308
+ if self . needs_to_grow ( len, additional) {
309
309
do_reserve_and_handle ( self , len, additional) ;
310
310
}
311
311
unsafe {
@@ -323,16 +323,8 @@ 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
- ///
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) } {
326
+ pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
327
+ if self . needs_to_grow ( len, additional) {
336
328
self . grow_amortized ( len, additional) ?;
337
329
}
338
330
unsafe {
@@ -348,35 +340,29 @@ impl<T, A: Allocator> RawVec<T, A> {
348
340
/// exactly the amount of memory necessary, but in principle the allocator
349
341
/// is free to give back more than we asked for.
350
342
///
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
+ ///
351
347
/// # Panics
352
348
///
353
349
/// Panics if the new capacity exceeds `isize::MAX` _bytes_.
354
350
///
355
351
/// # Aborts
356
352
///
357
353
/// Aborts on OOM.
358
- ///
359
- /// # Safety
360
- ///
361
- /// `len` must be less than or equal to the capacity of this [`RawVec`].
362
354
#[ cfg( not( no_global_oom_handling) ) ]
363
- pub unsafe fn reserve_exact ( & mut self , len : usize , additional : usize ) {
364
- unsafe {
365
- handle_reserve ( self . try_reserve_exact ( len, additional) ) ;
366
- }
355
+ pub fn reserve_exact ( & mut self , len : usize , additional : usize ) {
356
+ handle_reserve ( self . try_reserve_exact ( len, additional) ) ;
367
357
}
368
358
369
359
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
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 (
360
+ pub fn try_reserve_exact (
375
361
& mut self ,
376
362
len : usize ,
377
363
additional : usize ,
378
364
) -> Result < ( ) , TryReserveError > {
379
- if unsafe { self . needs_to_grow ( len, additional) } {
365
+ if self . needs_to_grow ( len, additional) {
380
366
self . grow_exact ( len, additional) ?;
381
367
}
382
368
unsafe {
@@ -405,8 +391,8 @@ impl<T, A: Allocator> RawVec<T, A> {
405
391
impl < T , A : Allocator > RawVec < T , A > {
406
392
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
407
393
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
408
- pub ( crate ) unsafe fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
409
- unsafe { additional > self . capacity ( ) . unchecked_sub ( len) }
394
+ pub ( crate ) fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
395
+ additional > self . capacity ( ) . wrapping_sub ( len)
410
396
}
411
397
412
398
/// # Safety:
0 commit comments