@@ -276,10 +276,6 @@ 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- ///
283279 /// This is ideal for implementing a bulk-push operation like `extend`.
284280 ///
285281 /// # Panics
@@ -289,9 +285,13 @@ impl<T, A: Allocator> RawVec<T, A> {
289285 /// # Aborts
290286 ///
291287 /// 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 fn reserve ( & mut self , len : usize , additional : usize ) {
294+ pub unsafe 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 self . needs_to_grow ( len, additional) {
308+ if unsafe { self . needs_to_grow ( len, additional) } {
309309 do_reserve_and_handle ( self , len, additional) ;
310310 }
311311 unsafe {
@@ -323,8 +323,16 @@ impl<T, A: Allocator> RawVec<T, A> {
323323 }
324324
325325 /// 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) } {
328336 self . grow_amortized ( len, additional) ?;
329337 }
330338 unsafe {
@@ -340,29 +348,35 @@ impl<T, A: Allocator> RawVec<T, A> {
340348 /// exactly the amount of memory necessary, but in principle the allocator
341349 /// is free to give back more than we asked for.
342350 ///
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- ///
347351 /// # Panics
348352 ///
349353 /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
350354 ///
351355 /// # Aborts
352356 ///
353357 /// Aborts on OOM.
358+ ///
359+ /// # Safety
360+ ///
361+ /// `len` must be less than or equal to the capacity of this [`RawVec`].
354362 #[ 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+ }
357367 }
358368
359369 /// 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 (
361375 & mut self ,
362376 len : usize ,
363377 additional : usize ,
364378 ) -> Result < ( ) , TryReserveError > {
365- if self . needs_to_grow ( len, additional) {
379+ if unsafe { self . needs_to_grow ( len, additional) } {
366380 self . grow_exact ( len, additional) ?;
367381 }
368382 unsafe {
@@ -391,8 +405,8 @@ impl<T, A: Allocator> RawVec<T, A> {
391405impl < T , A : Allocator > RawVec < T , A > {
392406 /// Returns if the buffer needs to grow to fulfill the needed extra capacity.
393407 /// 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) }
396410 }
397411
398412 /// # Safety:
0 commit comments