Skip to content

Commit 65204a9

Browse files
committed
Auto merge of rust-lang#55278 - Centril:constification-1, r=alexcrichton
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}`
2 parents 0195812 + ac1c6b0 commit 65204a9

28 files changed

+118
-85
lines changed

src/libcore/cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<T: ?Sized> Cell<T> {
474474
/// ```
475475
#[inline]
476476
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
477-
pub fn as_ptr(&self) -> *mut T {
477+
pub const fn as_ptr(&self) -> *mut T {
478478
self.value.get()
479479
}
480480

@@ -1508,7 +1508,7 @@ impl<T: ?Sized> UnsafeCell<T> {
15081508
/// ```
15091509
#[inline]
15101510
#[stable(feature = "rust1", since = "1.0.0")]
1511-
pub fn get(&self) -> *mut T {
1511+
pub const fn get(&self) -> *mut T {
15121512
&self.value as *const T as *mut T
15131513
}
15141514
}

src/libcore/char/methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ impl char {
903903
/// ```
904904
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
905905
#[inline]
906-
pub fn is_ascii(&self) -> bool {
906+
pub const fn is_ascii(&self) -> bool {
907907
*self as u32 <= 0x7F
908908
}
909909

src/libcore/convert.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
/// assert_eq!(vec![1, 3], filtered);
105105
/// ```
106106
#[unstable(feature = "convert_id", issue = "53500")]
107-
#[rustc_const_unstable(feature = "const_convert_id")]
108107
#[inline]
109108
pub const fn identity<T>(x: T) -> T { x }
110109

src/libcore/iter/sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<T> Default for Empty<T> {
283283
/// assert_eq!(None, nope.next());
284284
/// ```
285285
#[stable(feature = "iter_empty", since = "1.2.0")]
286-
pub fn empty<T>() -> Empty<T> {
286+
pub const fn empty<T>() -> Empty<T> {
287287
Empty(marker::PhantomData)
288288
}
289289

src/libcore/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
#![feature(const_fn)]
8383
#![feature(const_int_ops)]
8484
#![feature(const_fn_union)]
85-
#![feature(const_manually_drop_new)]
8685
#![feature(custom_attribute)]
8786
#![feature(doc_cfg)]
8887
#![feature(doc_spotlight)]

src/libcore/mem.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,6 @@ impl<T> ManuallyDrop<T> {
942942
/// ManuallyDrop::new(Box::new(()));
943943
/// ```
944944
#[stable(feature = "manually_drop", since = "1.20.0")]
945-
#[rustc_const_unstable(feature = "const_manually_drop_new")]
946945
#[inline]
947946
pub const fn new(value: T) -> ManuallyDrop<T> {
948947
ManuallyDrop { value }
@@ -961,7 +960,7 @@ impl<T> ManuallyDrop<T> {
961960
/// ```
962961
#[stable(feature = "manually_drop", since = "1.20.0")]
963962
#[inline]
964-
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
963+
pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
965964
slot.value
966965
}
967966

src/libcore/num/flt2dec/estimator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
2222
// therefore this always underestimates (or is exact), but not much.
2323
(((nbits + exp as i64) * 1292913986) >> 32) as i16
2424
}
25-

src/libcore/num/flt2dec/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,3 @@ pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
658658
}
659659
}
660660
}
661-

src/libcore/num/wrapping.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ assert_eq!(n.count_ones(), 3);
387387
```"),
388388
#[inline]
389389
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
390-
pub fn count_ones(self) -> u32 {
390+
pub const fn count_ones(self) -> u32 {
391391
self.0.count_ones()
392392
}
393393
}
@@ -407,7 +407,7 @@ assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);
407407
```"),
408408
#[inline]
409409
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
410-
pub fn count_zeros(self) -> u32 {
410+
pub const fn count_zeros(self) -> u32 {
411411
self.0.count_zeros()
412412
}
413413
}
@@ -430,7 +430,7 @@ assert_eq!(n.trailing_zeros(), 3);
430430
```"),
431431
#[inline]
432432
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
433-
pub fn trailing_zeros(self) -> u32 {
433+
pub const fn trailing_zeros(self) -> u32 {
434434
self.0.trailing_zeros()
435435
}
436436
}
@@ -456,7 +456,7 @@ assert_eq!(n.trailing_zeros(), 3);
456456
/// ```
457457
#[inline]
458458
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
459-
pub fn rotate_left(self, n: u32) -> Self {
459+
pub const fn rotate_left(self, n: u32) -> Self {
460460
Wrapping(self.0.rotate_left(n))
461461
}
462462

@@ -481,7 +481,7 @@ assert_eq!(n.trailing_zeros(), 3);
481481
/// ```
482482
#[inline]
483483
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
484-
pub fn rotate_right(self, n: u32) -> Self {
484+
pub const fn rotate_right(self, n: u32) -> Self {
485485
Wrapping(self.0.rotate_right(n))
486486
}
487487

@@ -505,7 +505,7 @@ assert_eq!(n.trailing_zeros(), 3);
505505
/// ```
506506
#[inline]
507507
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
508-
pub fn swap_bytes(self) -> Self {
508+
pub const fn swap_bytes(self) -> Self {
509509
Wrapping(self.0.swap_bytes())
510510
}
511511

@@ -532,7 +532,7 @@ assert_eq!(n.trailing_zeros(), 3);
532532
/// ```
533533
#[unstable(feature = "reverse_bits", issue = "48763")]
534534
#[inline]
535-
pub fn reverse_bits(self) -> Self {
535+
pub const fn reverse_bits(self) -> Self {
536536
Wrapping(self.0.reverse_bits())
537537
}
538538

@@ -560,7 +560,7 @@ if cfg!(target_endian = \"big\") {
560560
```"),
561561
#[inline]
562562
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
563-
pub fn from_be(x: Self) -> Self {
563+
pub const fn from_be(x: Self) -> Self {
564564
Wrapping(<$t>::from_be(x.0))
565565
}
566566
}
@@ -589,7 +589,7 @@ if cfg!(target_endian = \"little\") {
589589
```"),
590590
#[inline]
591591
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
592-
pub fn from_le(x: Self) -> Self {
592+
pub const fn from_le(x: Self) -> Self {
593593
Wrapping(<$t>::from_le(x.0))
594594
}
595595
}
@@ -618,7 +618,7 @@ if cfg!(target_endian = \"big\") {
618618
```"),
619619
#[inline]
620620
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
621-
pub fn to_be(self) -> Self {
621+
pub const fn to_be(self) -> Self {
622622
Wrapping(self.0.to_be())
623623
}
624624
}
@@ -647,7 +647,7 @@ if cfg!(target_endian = \"little\") {
647647
```"),
648648
#[inline]
649649
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
650-
pub fn to_le(self) -> Self {
650+
pub const fn to_le(self) -> Self {
651651
Wrapping(self.0.to_le())
652652
}
653653
}
@@ -707,7 +707,7 @@ assert_eq!(n.leading_zeros(), 3);
707707
```"),
708708
#[inline]
709709
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
710-
pub fn leading_zeros(self) -> u32 {
710+
pub const fn leading_zeros(self) -> u32 {
711711
self.0.leading_zeros()
712712
}
713713
}
@@ -784,7 +784,7 @@ assert!(!Wrapping(-10", stringify!($t), ").is_positive());
784784
```"),
785785
#[inline]
786786
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
787-
pub fn is_positive(self) -> bool {
787+
pub const fn is_positive(self) -> bool {
788788
self.0.is_positive()
789789
}
790790
}
@@ -806,7 +806,7 @@ assert!(!Wrapping(10", stringify!($t), ").is_negative());
806806
```"),
807807
#[inline]
808808
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
809-
pub fn is_negative(self) -> bool {
809+
pub const fn is_negative(self) -> bool {
810810
self.0.is_negative()
811811
}
812812
}
@@ -836,7 +836,7 @@ assert_eq!(n.leading_zeros(), 2);
836836
```"),
837837
#[inline]
838838
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
839-
pub fn leading_zeros(self) -> u32 {
839+
pub const fn leading_zeros(self) -> u32 {
840840
self.0.leading_zeros()
841841
}
842842
}

src/libcore/ops/range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<Idx> RangeInclusive<Idx> {
416416
/// ```
417417
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
418418
#[inline]
419-
pub fn start(&self) -> &Idx {
419+
pub const fn start(&self) -> &Idx {
420420
&self.start
421421
}
422422

@@ -440,7 +440,7 @@ impl<Idx> RangeInclusive<Idx> {
440440
/// ```
441441
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
442442
#[inline]
443-
pub fn end(&self) -> &Idx {
443+
pub const fn end(&self) -> &Idx {
444444
&self.end
445445
}
446446

0 commit comments

Comments
 (0)