Skip to content

Commit 5b89877

Browse files
committed
constify parts of libcore.
1 parent b4c046b commit 5b89877

33 files changed

+84
-112
lines changed

src/libcore/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use num::NonZeroUsize;
2525
#[derive(Debug)]
2626
pub struct Excess(pub NonNull<u8>, pub usize);
2727

28-
fn size_align<T>() -> (usize, usize) {
28+
const fn size_align<T>() -> (usize, usize) {
2929
(mem::size_of::<T>(), mem::align_of::<T>())
3030
}
3131

@@ -116,7 +116,7 @@ impl Layout {
116116
/// The minimum size in bytes for a memory block of this layout.
117117
#[stable(feature = "alloc_layout", since = "1.28.0")]
118118
#[inline]
119-
pub fn size(&self) -> usize { self.size_ }
119+
pub const fn size(&self) -> usize { self.size_ }
120120

121121
/// The minimum byte alignment for a memory block of this layout.
122122
#[stable(feature = "alloc_layout", since = "1.28.0")]

src/libcore/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl TryFromSliceError {
7777
issue = "0")]
7878
#[inline]
7979
#[doc(hidden)]
80-
pub fn __description(&self) -> &str {
80+
pub const fn __description(&self) -> &str {
8181
"could not convert slice to array"
8282
}
8383
}

src/libcore/benches/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn bench_multiple_take(b: &mut Bencher) {
3939
});
4040
}
4141

42-
fn scatter(x: i32) -> i32 { (x * 31) % 127 }
42+
const fn scatter(x: i32) -> i32 { (x * 31) % 127 }
4343

4444
#[bench]
4545
fn bench_max_by_key(b: &mut Bencher) {

src/libcore/cell.rs

Lines changed: 4 additions & 4 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

@@ -636,12 +636,12 @@ type BorrowFlag = isize;
636636
const UNUSED: BorrowFlag = 0;
637637

638638
#[inline(always)]
639-
fn is_writing(x: BorrowFlag) -> bool {
639+
const fn is_writing(x: BorrowFlag) -> bool {
640640
x < UNUSED
641641
}
642642

643643
#[inline(always)]
644-
fn is_reading(x: BorrowFlag) -> bool {
644+
const fn is_reading(x: BorrowFlag) -> bool {
645645
x > UNUSED
646646
}
647647

@@ -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/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
130130
impl DecodeUtf16Error {
131131
/// Returns the unpaired surrogate which caused this error.
132132
#[stable(feature = "decode_utf16", since = "1.9.0")]
133-
pub fn unpaired_surrogate(&self) -> u16 {
133+
pub const fn unpaired_surrogate(&self) -> u16 {
134134
self.code
135135
}
136136
}

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/fmt/mod.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'a> Arguments<'a> {
341341
#[doc(hidden)] #[inline]
342342
#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
343343
issue = "0")]
344-
pub fn new_v1(pieces: &'a [&'a str],
344+
pub const fn new_v1(pieces: &'a [&'a str],
345345
args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
346346
Arguments {
347347
pieces,
@@ -359,7 +359,7 @@ impl<'a> Arguments<'a> {
359359
#[doc(hidden)] #[inline]
360360
#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
361361
issue = "0")]
362-
pub fn new_v1_formatted(pieces: &'a [&'a str],
362+
pub const fn new_v1_formatted(pieces: &'a [&'a str],
363363
args: &'a [ArgumentV1<'a>],
364364
fmt: &'a [rt::v1::Argument]) -> Arguments<'a> {
365365
Arguments {
@@ -1492,7 +1492,7 @@ impl<'a> Formatter<'a> {
14921492
/// assert_eq!(&format!("{:t>6}", Foo), "tttttt");
14931493
/// ```
14941494
#[stable(feature = "fmt_flags", since = "1.5.0")]
1495-
pub fn fill(&self) -> char { self.fill }
1495+
pub const fn fill(&self) -> char { self.fill }
14961496

14971497
/// Flag indicating what form of alignment was requested.
14981498
///
@@ -1562,7 +1562,7 @@ impl<'a> Formatter<'a> {
15621562
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
15631563
/// ```
15641564
#[stable(feature = "fmt_flags", since = "1.5.0")]
1565-
pub fn width(&self) -> Option<usize> { self.width }
1565+
pub const fn width(&self) -> Option<usize> { self.width }
15661566

15671567
/// Optionally specified precision for numeric types.
15681568
///
@@ -1589,7 +1589,7 @@ impl<'a> Formatter<'a> {
15891589
/// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)");
15901590
/// ```
15911591
#[stable(feature = "fmt_flags", since = "1.5.0")]
1592-
pub fn precision(&self) -> Option<usize> { self.precision }
1592+
pub const fn precision(&self) -> Option<usize> { self.precision }
15931593

15941594
/// Determines if the `+` flag was specified.
15951595
///
@@ -1617,7 +1617,9 @@ impl<'a> Formatter<'a> {
16171617
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
16181618
/// ```
16191619
#[stable(feature = "fmt_flags", since = "1.5.0")]
1620-
pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 }
1620+
pub const fn sign_plus(&self) -> bool {
1621+
self.flags & (1 << FlagV1::SignPlus as u32) != 0
1622+
}
16211623

16221624
/// Determines if the `-` flag was specified.
16231625
///
@@ -1643,7 +1645,9 @@ impl<'a> Formatter<'a> {
16431645
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
16441646
/// ```
16451647
#[stable(feature = "fmt_flags", since = "1.5.0")]
1646-
pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 }
1648+
pub const fn sign_minus(&self) -> bool {
1649+
self.flags & (1 << FlagV1::SignMinus as u32) != 0
1650+
}
16471651

16481652
/// Determines if the `#` flag was specified.
16491653
///
@@ -1668,7 +1672,9 @@ impl<'a> Formatter<'a> {
16681672
/// assert_eq!(&format!("{}", Foo(23)), "23");
16691673
/// ```
16701674
#[stable(feature = "fmt_flags", since = "1.5.0")]
1671-
pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 }
1675+
pub const fn alternate(&self) -> bool {
1676+
self.flags & (1 << FlagV1::Alternate as u32) != 0
1677+
}
16721678

16731679
/// Determines if the `0` flag was specified.
16741680
///
@@ -1691,15 +1697,19 @@ impl<'a> Formatter<'a> {
16911697
/// assert_eq!(&format!("{:04}", Foo(23)), "23");
16921698
/// ```
16931699
#[stable(feature = "fmt_flags", since = "1.5.0")]
1694-
pub fn sign_aware_zero_pad(&self) -> bool {
1700+
pub const fn sign_aware_zero_pad(&self) -> bool {
16951701
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
16961702
}
16971703

16981704
// FIXME: Decide what public API we want for these two flags.
16991705
// https://github.com/rust-lang/rust/issues/48584
1700-
fn debug_lower_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugLowerHex as u32) != 0 }
1706+
const fn debug_lower_hex(&self) -> bool {
1707+
self.flags & (1 << FlagV1::DebugLowerHex as u32) != 0
1708+
}
17011709

1702-
fn debug_upper_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugUpperHex as u32) != 0 }
1710+
const fn debug_upper_hex(&self) -> bool {
1711+
self.flags & (1 << FlagV1::DebugUpperHex as u32) != 0
1712+
}
17031713

17041714
/// Creates a [`DebugStruct`] builder designed to assist with creation of
17051715
/// [`fmt::Debug`] implementations for structs.

src/libcore/iter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2658,7 +2658,7 @@ impl<I, U> FusedIterator for Flatten<I>
26582658
I::Item: IntoIterator<IntoIter = U, Item = U::Item> {}
26592659

26602660
/// Adapts an iterator by flattening it, for use in `flatten()` and `flat_map()`.
2661-
fn flatten_compat<I, U>(iter: I) -> FlattenCompat<I, U> {
2661+
const fn flatten_compat<I, U>(iter: I) -> FlattenCompat<I, U> {
26622662
FlattenCompat { iter, frontiter: None, backiter: None }
26632663
}
26642664

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

0 commit comments

Comments
 (0)