From a97abb40ab390465b1cd845e735d58956d28eb16 Mon Sep 17 00:00:00 2001 From: Jacob Hughes Date: Thu, 24 Sep 2020 18:56:57 -0400 Subject: [PATCH] Rename LayoutErr to LayoutError in core --- library/core/src/alloc/layout.rs | 53 +++++++++++++++++--------------- library/core/src/alloc/mod.rs | 3 ++ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index a3fbed2ec12..e338baea645 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -39,7 +39,7 @@ pub struct Layout { impl Layout { /// Constructs a `Layout` from a given `size` and `align`, - /// or returns `LayoutErr` if any of the following conditions + /// or returns `LayoutError` if any of the following conditions /// are not met: /// /// * `align` must not be zero, @@ -52,9 +52,9 @@ impl Layout { #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub const fn from_size_align(size: usize, align: usize) -> Result { + pub const fn from_size_align(size: usize, align: usize) -> Result { if !align.is_power_of_two() { - return Err(LayoutErr { private: () }); + return Err(LayoutError { private: () }); } // (power-of-two implies align != 0.) @@ -72,7 +72,7 @@ pub const fn from_size_align(size: usize, align: usize) -> Result usize::MAX - (align - 1) { - return Err(LayoutErr { private: () }); + return Err(LayoutError { private: () }); } // SAFETY: the conditions for `from_size_align_unchecked` have been @@ -200,7 +200,7 @@ pub const fn dangling(&self) -> NonNull { /// `align` violates the conditions listed in [`Layout::from_size_align`]. #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn align_to(&self, align: usize) -> Result { + pub fn align_to(&self, align: usize) -> Result { Layout::from_size_align(self.size(), cmp::max(self.align(), align)) } @@ -274,16 +274,16 @@ pub fn pad_to_align(&self) -> Layout { /// layout of the array and `offs` is the distance between the start /// of each element in the array. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { + pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> { // This cannot overflow. Quoting from the invariant of Layout: // > `size`, when rounded up to the nearest multiple of `align`, // > must not overflow (i.e., the rounded value must be less than // > `usize::MAX`) let padded_size = self.size() + self.padding_needed_for(self.align()); - let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?; + let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError { private: () })?; // SAFETY: self.align is already known to be valid and alloc_size has been // padded already. @@ -307,7 +307,7 @@ pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { /// start of the `next` embedded within the concatenated record /// (assuming that the record itself starts at offset 0). /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. /// /// # Examples /// @@ -315,8 +315,8 @@ pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { /// the fields from its fields' layouts: /// /// ```rust - /// # use std::alloc::{Layout, LayoutErr}; - /// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec), LayoutErr> { + /// # use std::alloc::{Layout, LayoutError}; + /// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec), LayoutError> { /// let mut offsets = Vec::new(); /// let mut layout = Layout::from_size_align(0, 1)?; /// for &field in fields { @@ -337,12 +337,12 @@ pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { /// ``` #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> { + pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> { let new_align = cmp::max(self.align(), next.align()); let pad = self.padding_needed_for(next.align()); - let offset = self.size().checked_add(pad).ok_or(LayoutErr { private: () })?; - let new_size = offset.checked_add(next.size()).ok_or(LayoutErr { private: () })?; + let offset = self.size().checked_add(pad).ok_or(LayoutError { private: () })?; + let new_size = offset.checked_add(next.size()).ok_or(LayoutError { private: () })?; let layout = Layout::from_size_align(new_size, new_align)?; Ok((layout, offset)) @@ -359,11 +359,11 @@ pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> { /// guaranteed that all elements in the array will be properly /// aligned. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn repeat_packed(&self, n: usize) -> Result { - let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?; + pub fn repeat_packed(&self, n: usize) -> Result { + let size = self.size().checked_mul(n).ok_or(LayoutError { private: () })?; Layout::from_size_align(size, self.align()) } @@ -372,38 +372,41 @@ pub fn repeat_packed(&self, n: usize) -> Result { /// padding is inserted, the alignment of `next` is irrelevant, /// and is not incorporated *at all* into the resulting layout. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn extend_packed(&self, next: Self) -> Result { - let new_size = self.size().checked_add(next.size()).ok_or(LayoutErr { private: () })?; + pub fn extend_packed(&self, next: Self) -> Result { + let new_size = self.size().checked_add(next.size()).ok_or(LayoutError { private: () })?; Layout::from_size_align(new_size, self.align()) } /// Creates a layout describing the record for a `[T; n]`. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn array(n: usize) -> Result { + pub fn array(n: usize) -> Result { let (layout, offset) = Layout::new::().repeat(n)?; debug_assert_eq!(offset, mem::size_of::()); Ok(layout.pad_to_align()) } } +#[stable(feature = "alloc_layout", since = "1.28.0")] +pub type LayoutErr = LayoutError; + /// The parameters given to `Layout::from_size_align` /// or some other `Layout` constructor /// do not satisfy its documented constraints. -#[stable(feature = "alloc_layout", since = "1.28.0")] +#[stable(feature = "alloc_layout_error", since = "1.49.0")] #[derive(Clone, PartialEq, Eq, Debug)] -pub struct LayoutErr { +pub struct LayoutError { private: (), } // (we need this for downstream impl of trait Error) #[stable(feature = "alloc_layout", since = "1.28.0")] -impl fmt::Display for LayoutErr { +impl fmt::Display for LayoutError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("invalid parameters to Layout::from_size_align") } diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 6d09b4f0263..6635e4229e0 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -10,6 +10,9 @@ #[stable(feature = "alloc_layout", since = "1.28.0")] pub use self::layout::{Layout, LayoutErr}; +#[stable(feature = "alloc_layout_error", since = "1.49.0")] +pub use self::layout::LayoutError; + use crate::fmt; use crate::ptr::{self, NonNull};