]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #55278 - Centril:constification-1, r=alexcrichton
authorbors <bors@rust-lang.org>
Mon, 12 Nov 2018 18:54:11 +0000 (18:54 +0000)
committerbors <bors@rust-lang.org>
Mon, 12 Nov 2018 18:54:11 +0000 (18:54 +0000)
Minor standard library constification

This PR makes some bits of the standard library into `const fn`s.
I've tried to be as aggressive as I possibly could in the constification.
The list is rather small due to how restrictive `const fn` is at the moment.

r? @oli-obk cc @rust-lang/libs

Stable public APIs affected:
+ [x] `Cell::as_ptr`
+ [x] `UnsafeCell::get`
+ [x] `char::is_ascii`
+ [x] `iter::empty`
+ [x] `ManuallyDrop::{new, into_inner}`
+ [x] `RangeInclusive::{start, end}`
+ [x] `NonNull::as_ptr`
+ [x] `{[T], str}::as_ptr`
+ [x] `Duration::{as_secs, subsec_millis, subsec_micros, subsec_nanos}`
+ [x] `CStr::as_ptr`
+ [x] `Ipv4Addr::is_unspecified`
+ [x] `Ipv6Addr::new`
+ [x] `Ipv6Addr::octets`

Unstable public APIs affected:
+ [x] `Duration::{as_millis, as_micros, as_nanos, as_float_secs}`
+ [x] `Wrapping::{count_ones, count_zeros, trailing_zeros, rotate_left, rotate_right, swap_bytes, reverse_bits, from_be, from_le, to_be, to_le, leading_zeros, is_positive, is_negative, leading_zeros}`
+ [x] `core::convert::identity`

--------------------------

## Removed from list in first pass:

Stable public APIs affected:
+ [ ] `BTree{Map, Set}::{len, is_empty}`
+ [ ] `VecDeque::is_empty`
+ [ ] `String::{is_empty, len}`
+ [ ] `FromUtf8Error::utf8_error`
+ [ ] `Vec<T>::{is_empty, len}`
+ [ ] `Layout::size`
+ [ ] `DecodeUtf16Error::unpaired_surrogate`
+ [ ] `core::fmt::{fill, width, precision, sign_plus, sign_minus, alternate, sign_aware_zero_pad}`
+ [ ] `panic::Location::{file, line, column}`
+ [ ] `{ChunksExact, RChunksExact}::remainder`
+ [ ] `Utf8Error::valid_up_to`
+ [ ] `VacantEntry::key`
+ [ ] `NulError::nul_position`
+ [ ] `IntoStringError::utf8_error`
+ [ ] `IntoInnerError::error`
+ [ ] `io::Chain::get_ref`
+ [ ] `io::Take::{limit, get_ref}`
+ [ ] `SocketAddrV6::{flowinfo, scope_id}`
+ [ ] `PrefixComponent::{kind, as_os_str}`
+ [ ] `Path::{ancestors, display}`
+ [ ] `WaitTimeoutResult::timed_out`
+ [ ] `Receiver::{iter, try_iter}`
+ [ ] `thread::JoinHandle::thread`
+ [ ] `SystemTimeError::duration`

Unstable public APIs affected:
+ [ ] `core::fmt::Arguments::new_v1`
+ [ ] `core::fmt::Arguments::new_v1_formatted`
+ [ ] `Pin::{get_ref, into_ref}`
+ [ ] `Utf8Lossy::chunks`
+ [ ] `LocalWaker::as_waker`
+ [ ] `panic::PanicInfo::{internal_constructor, message, location}`
+ [ ] `panic::Location::{internal_constructor }`

## Removed from list in 2nd pass:

Stable public APIs affected:
+ [ ] `LinkedList::{new, iter, is_empty, len}`
+ [ ] `mem::forget`
+ [ ] `Cursor::{new, get_ref, position}`
+ [ ] `io::{empty, repeat, sink}`
+ [ ] `PoisonError::new`
+ [ ] `thread::Builder::new`
+ [ ] `process::Stdio::{piped, inherit, null}`

Unstable public APIs affected:
+ [ ] `io::Initializer::{zeroing, should_initialize}`

1  2 
src/libcore/lib.rs
src/libcore/mem.rs
src/libcore/ptr.rs
src/libstd/ffi/c_str.rs

diff --combined src/libcore/lib.rs
index c69d4441121ce9d9f6ad72e6403bec7e2929be4a,2445d93692748beea584d1730821740fda11bf48..6cf6417974a0dc367267621d7ddf1372dd12a035
@@@ -82,7 -82,6 +82,6 @@@
  #![feature(const_fn)]
  #![feature(const_int_ops)]
  #![feature(const_fn_union)]
- #![feature(const_manually_drop_new)]
  #![feature(custom_attribute)]
  #![feature(doc_cfg)]
  #![feature(doc_spotlight)]
@@@ -228,7 -227,7 +227,7 @@@ mod nonzero
  mod tuple;
  mod unit;
  
 -// Pull in the the `coresimd` crate directly into libcore. This is where all the
 +// Pull in the `coresimd` crate directly into libcore. This is where all the
  // architecture-specific (and vendor-specific) intrinsics are defined. AKA
  // things like SIMD and such. Note that the actual source for all this lies in a
  // different repository, rust-lang-nursery/stdsimd. That's why the setup here is
diff --combined src/libcore/mem.rs
index 8c4ff02aa140f65afec7d23c05edfee71287d46f,b89f37fa5ce25eddaad9c5578f3f0e06cd82cba3..d4b7094d6fb645bb48166d0d45f12d22b0a6eeb8
@@@ -202,7 -202,7 +202,7 @@@ pub fn forget<T>(t: T) 
  ///
  /// ## Size of Enums
  ///
 -/// Enums that carry no data other than the descriminant have the same size as C enums
 +/// Enums that carry no data other than the discriminant have the same size as C enums
  /// on the platform they are compiled for.
  ///
  /// ## Size of Unions
@@@ -942,7 -942,6 +942,6 @@@ impl<T> ManuallyDrop<T> 
      /// ManuallyDrop::new(Box::new(()));
      /// ```
      #[stable(feature = "manually_drop", since = "1.20.0")]
-     #[rustc_const_unstable(feature = "const_manually_drop_new")]
      #[inline]
      pub const fn new(value: T) -> ManuallyDrop<T> {
          ManuallyDrop { value }
      /// ```
      #[stable(feature = "manually_drop", since = "1.20.0")]
      #[inline]
-     pub fn into_inner(slot: ManuallyDrop<T>) -> T {
+     pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
          slot.value
      }
  
@@@ -1081,7 -1080,7 +1080,7 @@@ impl<T> MaybeUninit<T> 
      ///
      /// # Unsafety
      ///
 -    /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
 +    /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
      /// state, otherwise this will immediately cause undefined behavior.
      #[unstable(feature = "maybe_uninit", issue = "53491")]
      pub unsafe fn into_inner(self) -> T {
      ///
      /// # Unsafety
      ///
 -    /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
 +    /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
      /// state, otherwise this will immediately cause undefined behavior.
      #[unstable(feature = "maybe_uninit", issue = "53491")]
      pub unsafe fn get_ref(&self) -> &T {
      ///
      /// # Unsafety
      ///
 -    /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
 +    /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
      /// state, otherwise this will immediately cause undefined behavior.
      #[unstable(feature = "maybe_uninit", issue = "53491")]
      pub unsafe fn get_mut(&mut self) -> &mut T {
diff --combined src/libcore/ptr.rs
index 827e297c84d1f427918508b885a71395a50884f9,b795cd72150084b5693d644bad467705156c1129..a7bfc3f51244720b5990f3bde99239b695250e38
@@@ -120,7 -120,7 +120,7 @@@ pub use intrinsics::write_bytes
  ///
  /// Additionally, if `T` is not [`Copy`], using the pointed-to value after
  /// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
 -/// foo` counts as a use because it will cause the the value to be dropped
 +/// foo` counts as a use because it will cause the value to be dropped
  /// again. [`write`] can be used to overwrite data without causing it to be
  /// dropped.
  ///
@@@ -371,7 -371,7 +371,7 @@@ pub(crate) unsafe fn swap_nonoverlappin
  #[inline]
  unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
      // The approach here is to utilize simd to swap x & y efficiently. Testing reveals
 -    // that swapping either 32 bytes or 64 bytes at a time is most efficient for intel
 +    // that swapping either 32 bytes or 64 bytes at a time is most efficient for Intel
      // Haswell E processors. LLVM is more able to optimize if we give a struct a
      // #[repr(simd)], even if we don't actually use this struct directly.
      //
@@@ -1005,7 -1005,7 +1005,7 @@@ impl<T: ?Sized> *const T 
      /// # Null-unchecked version
      ///
      /// If you are sure the pointer can never be null and are looking for some kind of
 -    /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>, know that you can
 +    /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
      /// dereference the pointer directly.
      ///
      /// ```
@@@ -1625,7 -1625,7 +1625,7 @@@ impl<T: ?Sized> *mut T 
      /// # Null-unchecked version
      ///
      /// If you are sure the pointer can never be null and are looking for some kind of
 -    /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>, know that you can
 +    /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
      /// dereference the pointer directly.
      ///
      /// ```
@@@ -2905,7 -2905,7 +2905,7 @@@ impl<T: ?Sized> NonNull<T> 
      /// Acquires the underlying `*mut` pointer.
      #[stable(feature = "nonnull", since = "1.25.0")]
      #[inline]
-     pub fn as_ptr(self) -> *mut T {
+     pub const fn as_ptr(self) -> *mut T {
          self.pointer.0 as *mut T
      }
  
diff --combined src/libstd/ffi/c_str.rs
index 87ffe0f15e45461032f31da6de9159016b7795c6,cb91453998540be040414e19a84a0f18f72c6abc..66718b95408ca160f90bb3fd6f40506addc8a1be
@@@ -1091,7 -1091,7 +1091,7 @@@ impl CStr 
      /// [`CString`]: struct.CString.html
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
-     pub fn as_ptr(&self) -> *const c_char {
+     pub const fn as_ptr(&self) -> *const c_char {
          self.inner.as_ptr()
      }
  
      ///
      /// If the contents of the `CStr` are valid UTF-8 data, this
      /// function will return a [`Cow`]`::`[`Borrowed`]`(`[`&str`]`)`
 -    /// with the the corresponding [`&str`] slice. Otherwise, it will
 +    /// with the corresponding [`&str`] slice. Otherwise, it will
      /// replace any invalid UTF-8 sequences with
      /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
      /// [`Cow`]`::`[`Owned`]`(`[`String`]`)` with the result.