X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fliballoc%2Fraw_vec.rs;h=86aed612efe0434cd8ac172cb38df5f73f10844c;hb=5c384ab00c7b4b39e457b75d385e9cbe12e699f5;hp=ee75fc288fee5b7a380b071944c6affc2825103a;hpb=a93c0da8d267117fc893e93f6d35b09b70c181de;p=rust.git diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index ee75fc288fe..86aed612efe 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -1,4 +1,4 @@ -#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "0")] +#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")] #![doc(hidden)] use core::cmp; @@ -7,9 +7,9 @@ use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{Alloc, Layout, Global, AllocErr, handle_alloc_error}; -use crate::collections::TryReserveError::{self, *}; +use crate::alloc::{handle_alloc_error, Alloc, AllocErr, Global, Layout}; use crate::boxed::Box; +use crate::collections::TryReserveError::{self, *}; #[cfg(test)] mod tests; @@ -52,17 +52,10 @@ impl RawVec { /// Like `new`, but parameterized over the choice of allocator for /// the returned `RawVec`. pub const fn new_in(a: A) -> Self { - // `!0` is `usize::MAX`. This branch should be stripped at compile time. - // FIXME(mark-i-m): use this line when `if`s are allowed in `const`: - //let cap = if mem::size_of::() == 0 { !0 } else { 0 }; + let cap = if mem::size_of::() == 0 { core::usize::MAX } else { 0 }; // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation". - RawVec { - ptr: Unique::empty(), - // FIXME(mark-i-m): use `cap` when ifs are allowed in const - cap: [0, !0][(mem::size_of::() == 0) as usize], - a, - } + RawVec { ptr: Unique::empty(), cap, a } } /// Like `with_capacity`, but parameterized over the choice of @@ -92,22 +85,14 @@ fn allocate_in(capacity: usize, zeroed: bool, mut a: A) -> Self { } else { let align = mem::align_of::(); let layout = Layout::from_size_align(alloc_size, align).unwrap(); - let result = if zeroed { - a.alloc_zeroed(layout) - } else { - a.alloc(layout) - }; + let result = if zeroed { a.alloc_zeroed(layout) } else { a.alloc(layout) }; match result { Ok(ptr) => ptr.cast(), Err(_) => handle_alloc_error(layout), } }; - RawVec { - ptr: ptr.into(), - cap: capacity, - a, - } + RawVec { ptr: ptr.into(), cap: capacity, a } } } } @@ -132,19 +117,7 @@ impl RawVec { /// `RawVec` with capacity `usize::MAX`. Useful for implementing /// delayed allocation. pub const fn new() -> Self { - // FIXME(Centril): Reintegrate this with `fn new_in` when we can. - - // `!0` is `usize::MAX`. This branch should be stripped at compile time. - // FIXME(mark-i-m): use this line when `if`s are allowed in `const`: - //let cap = if mem::size_of::() == 0 { !0 } else { 0 }; - - // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation". - RawVec { - ptr: Unique::empty(), - // FIXME(mark-i-m): use `cap` when ifs are allowed in const - cap: [0, !0][(mem::size_of::() == 0) as usize], - a: Global, - } + Self::new_in(Global) } /// Creates a `RawVec` (on the system heap) with exactly the @@ -183,11 +156,7 @@ impl RawVec { /// The `capacity` cannot exceed `isize::MAX` (only a concern on 32-bit systems). /// If the `ptr` and `capacity` come from a `RawVec` created via `a`, then this is guaranteed. pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, a: A) -> Self { - RawVec { - ptr: Unique::new_unchecked(ptr), - cap: capacity, - a, - } + RawVec { ptr: Unique::new_unchecked(ptr), cap: capacity, a } } } @@ -200,11 +169,7 @@ impl RawVec { /// The `capacity` cannot exceed `isize::MAX` (only a concern on 32-bit systems). /// If the `ptr` and `capacity` come from a `RawVec`, then this is guaranteed. pub unsafe fn from_raw_parts(ptr: *mut T, capacity: usize) -> Self { - RawVec { - ptr: Unique::new_unchecked(ptr), - cap: capacity, - a: Global, - } + RawVec { ptr: Unique::new_unchecked(ptr), cap: capacity, a: Global } } /// Converts a `Box<[T]>` into a `RawVec`. @@ -230,11 +195,7 @@ pub fn ptr(&self) -> *mut T { /// This will always be `usize::MAX` if `T` is zero-sized. #[inline(always)] pub fn capacity(&self) -> usize { - if mem::size_of::() == 0 { - !0 - } else { - self.cap - } + if mem::size_of::() == 0 { !0 } else { self.cap } } /// Returns a shared reference to the allocator backing this `RawVec`. @@ -334,14 +295,13 @@ pub fn double(&mut self) { let new_cap = 2 * self.cap; let new_size = new_cap * elem_size; alloc_guard(new_size).unwrap_or_else(|_| capacity_overflow()); - let ptr_res = self.a.realloc(NonNull::from(self.ptr).cast(), - cur, - new_size); + let ptr_res = self.a.realloc(NonNull::from(self.ptr).cast(), cur, new_size); match ptr_res { Ok(ptr) => (new_cap, ptr.cast().into()), - Err(_) => handle_alloc_error( - Layout::from_size_align_unchecked(new_size, cur.align()) - ), + Err(_) => handle_alloc_error(Layout::from_size_align_unchecked( + new_size, + cur.align(), + )), } } None => { @@ -401,17 +361,17 @@ pub fn double_in_place(&mut self) -> bool { self.cap = new_cap; true } - Err(_) => { - false - } + Err(_) => false, } } } /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting. - pub fn try_reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) - -> Result<(), TryReserveError> { - + pub fn try_reserve_exact( + &mut self, + used_capacity: usize, + needed_extra_capacity: usize, + ) -> Result<(), TryReserveError> { self.reserve_internal(used_capacity, needed_extra_capacity, Fallible, Exact) } @@ -440,18 +400,20 @@ pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usi Err(CapacityOverflow) => capacity_overflow(), Err(AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } - } - } + } + } /// Calculates the buffer's new size given that it'll hold `used_capacity + /// needed_extra_capacity` elements. This logic is used in amortized reserve methods. /// Returns `(new_capacity, new_alloc_size)`. - fn amortized_new_size(&self, used_capacity: usize, needed_extra_capacity: usize) - -> Result { - + fn amortized_new_size( + &self, + used_capacity: usize, + needed_extra_capacity: usize, + ) -> Result { // Nothing we can really do about these checks, sadly. - let required_cap = used_capacity.checked_add(needed_extra_capacity) - .ok_or(CapacityOverflow)?; + let required_cap = + used_capacity.checked_add(needed_extra_capacity).ok_or(CapacityOverflow)?; // Cannot overflow, because `cap <= isize::MAX`, and type of `cap` is `usize`. let double_cap = self.cap * 2; // `double_cap` guarantees exponential growth. @@ -459,8 +421,11 @@ fn amortized_new_size(&self, used_capacity: usize, needed_extra_capacity: usize) } /// The same as `reserve`, but returns on errors instead of panicking or aborting. - pub fn try_reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) - -> Result<(), TryReserveError> { + pub fn try_reserve( + &mut self, + used_capacity: usize, + needed_extra_capacity: usize, + ) -> Result<(), TryReserveError> { self.reserve_internal(used_capacity, needed_extra_capacity, Fallible, Amortized) } @@ -558,7 +523,8 @@ pub fn reserve_in_place(&mut self, used_capacity: usize, needed_extra_capacity: return false; } - let new_cap = self.amortized_new_size(used_capacity, needed_extra_capacity) + let new_cap = self + .amortized_new_size(used_capacity, needed_extra_capacity) .unwrap_or_else(|_| capacity_overflow()); // Here, `cap < used_capacity + needed_extra_capacity <= new_cap` @@ -569,15 +535,15 @@ pub fn reserve_in_place(&mut self, used_capacity: usize, needed_extra_capacity: // FIXME: may crash and burn on over-reserve alloc_guard(new_layout.size()).unwrap_or_else(|_| capacity_overflow()); match self.a.grow_in_place( - NonNull::from(self.ptr).cast(), old_layout, new_layout.size(), + NonNull::from(self.ptr).cast(), + old_layout, + new_layout.size(), ) { Ok(_) => { self.cap = new_cap; true } - Err(_) => { - false - } + Err(_) => false, } } } @@ -630,13 +596,11 @@ pub fn shrink_to_fit(&mut self, amount: usize) { let new_size = elem_size * amount; let align = mem::align_of::(); let old_layout = Layout::from_size_align_unchecked(old_size, align); - match self.a.realloc(NonNull::from(self.ptr).cast(), - old_layout, - new_size) { + match self.a.realloc(NonNull::from(self.ptr).cast(), old_layout, new_size) { Ok(p) => self.ptr = p.cast().into(), - Err(_) => handle_alloc_error( - Layout::from_size_align_unchecked(new_size, align) - ), + Err(_) => { + handle_alloc_error(Layout::from_size_align_unchecked(new_size, align)) + } } } self.cap = amount; @@ -680,7 +644,9 @@ fn reserve_internal( // Nothing we can really do about these checks, sadly. let new_cap = match strategy { - Exact => used_capacity.checked_add(needed_extra_capacity).ok_or(CapacityOverflow)?, + Exact => { + used_capacity.checked_add(needed_extra_capacity).ok_or(CapacityOverflow)? + } Amortized => self.amortized_new_size(used_capacity, needed_extra_capacity)?, }; let new_layout = Layout::array::(new_cap).map_err(|_| CapacityOverflow)?; @@ -697,10 +663,12 @@ fn reserve_internal( let ptr = match (res, fallibility) { (Err(AllocErr), Infallible) => handle_alloc_error(new_layout), - (Err(AllocErr), Fallible) => return Err(TryReserveError::AllocError { - layout: new_layout, - non_exhaustive: (), - }), + (Err(AllocErr), Fallible) => { + return Err(TryReserveError::AllocError { + layout: new_layout, + non_exhaustive: (), + }); + } (Ok(ptr), _) => ptr, }; @@ -710,7 +678,6 @@ fn reserve_internal( Ok(()) } } - } impl RawVec { @@ -748,7 +715,9 @@ pub unsafe fn dealloc_buffer(&mut self) { unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { - unsafe { self.dealloc_buffer(); } + unsafe { + self.dealloc_buffer(); + } } }