@@ -276,6 +276,10 @@ impl<T, A: Allocator> RawVec<T, A> {
276276 /// *O*(1) behavior. Will limit this behavior if it would needlessly cause
277277 /// itself to panic.
278278 ///
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+ ///
279283 /// This is ideal for implementing a bulk-push operation like `extend`.
280284 ///
281285 /// # Panics
@@ -285,13 +289,9 @@ impl<T, A: Allocator> RawVec<T, A> {
285289 /// # Aborts
286290 ///
287291 /// Aborts on OOM.
288- ///
289- /// # Safety
290- ///
291- /// `len` must be less than or equal to the capacity of this [`RawVec`].
292292 #[ cfg( not( no_global_oom_handling) ) ]
293293 #[ inline]
294- pub unsafe fn reserve ( & mut self , len : usize , additional : usize ) {
294+ pub fn reserve ( & mut self , len : usize , additional : usize ) {
295295 // Callers expect this function to be very cheap when there is already sufficient capacity.
296296 // Therefore, we move all the resizing and error-handling logic from grow_amortized and
297297 // 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> {
305305 handle_reserve ( slf. grow_amortized ( len, additional) ) ;
306306 }
307307
308- if unsafe { self . needs_to_grow ( len, additional) } {
308+ if self . needs_to_grow ( len, additional) {
309309 do_reserve_and_handle ( self , len, additional) ;
310310 }
311311 unsafe {
@@ -323,16 +323,8 @@ impl<T, A: Allocator> RawVec<T, A> {
323323 }
324324
325325 /// 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) {
336328 self . grow_amortized ( len, additional) ?;
337329 }
338330 unsafe {
@@ -348,35 +340,29 @@ impl<T, A: Allocator> RawVec<T, A> {
348340 /// exactly the amount of memory necessary, but in principle the allocator
349341 /// is free to give back more than we asked for.
350342 ///
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+ ///
351347 /// # Panics
352348 ///
353349 /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
354350 ///
355351 /// # Aborts
356352 ///
357353 /// Aborts on OOM.
358- ///
359- /// # Safety
360- ///
361- /// `len` must be less than or equal to the capacity of this [`RawVec`].
362354 #[ 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) ) ;
367357 }
368358
369359 /// 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 (
375361 & mut self ,
376362 len : usize ,
377363 additional : usize ,
378364 ) -> Result < ( ) , TryReserveError > {
379- if unsafe { self . needs_to_grow ( len, additional) } {
365+ if self . needs_to_grow ( len, additional) {
380366 self . grow_exact ( len, additional) ?;
381367 }
382368 unsafe {
@@ -405,8 +391,8 @@ impl<T, A: Allocator> RawVec<T, A> {
405391impl < T , A : Allocator > RawVec < T , A > {
406392 /// Returns if the buffer needs to grow to fulfill the needed extra capacity.
407393 /// 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)
410396 }
411397
412398 /// # Safety:
0 commit comments