Skip to content

Minor standard library constification #55278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert making internal APIs const fn.
  • Loading branch information
Centril committed Nov 10, 2018
commit e15c62d61fa02fac93992db9297aa4a8a56cef93
2 changes: 1 addition & 1 deletion src/liballoc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ impl<'a, T> Hole<'a, T> {
}

#[inline]
const fn pos(&self) -> usize {
fn pos(&self) -> usize {
self.pos
}

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {

/// Returns the height of this node in the whole tree. Zero height denotes the
/// leaf level.
pub const fn height(&self) -> usize {
pub fn height(&self) -> usize {
self.height
}

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
}

impl<T> Node<T> {
const fn new(element: T) -> Self {
fn new(element: T) -> Self {
Node {
next: None,
prev: None,
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ impl<T> VecDeque<T> {
}

#[inline]
const fn is_contiguous(&self) -> bool {
fn is_contiguous(&self) -> bool {
self.tail <= self.head
}

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<T, A: Alloc> RawVec<T, A> {
/// Gets a raw pointer to the start of the allocation. Note that this is
/// Unique::empty() if `cap = 0` or T is zero-sized. In the former case, you must
/// be careful.
pub const fn ptr(&self) -> *mut T {
pub fn ptr(&self) -> *mut T {
self.ptr.as_ptr()
}

Expand All @@ -221,7 +221,7 @@ impl<T, A: Alloc> RawVec<T, A> {
}

/// Returns a shared reference to the allocator backing this RawVec.
pub const fn alloc(&self) -> &A {
pub fn alloc(&self) -> &A {
&self.a
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use num::NonZeroUsize;
#[derive(Debug)]
pub struct Excess(pub NonNull<u8>, pub usize);

const fn size_align<T>() -> (usize, usize) {
fn size_align<T>() -> (usize, usize) {
(mem::size_of::<T>(), mem::align_of::<T>())
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl TryFromSliceError {
issue = "0")]
#[inline]
#[doc(hidden)]
pub const fn __description(&self) -> &str {
pub fn __description(&self) -> &str {
"could not convert slice to array"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn bench_multiple_take(b: &mut Bencher) {
});
}

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

#[bench]
fn bench_max_by_key(b: &mut Bencher) {
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,12 @@ type BorrowFlag = isize;
const UNUSED: BorrowFlag = 0;

#[inline(always)]
const fn is_writing(x: BorrowFlag) -> bool {
fn is_writing(x: BorrowFlag) -> bool {
x < UNUSED
}

#[inline(always)]
const fn is_reading(x: BorrowFlag) -> bool {
fn is_reading(x: BorrowFlag) -> bool {
x > UNUSED
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1703,11 +1703,11 @@ impl<'a> Formatter<'a> {

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2658,7 +2658,7 @@ impl<I, U> FusedIterator for Flatten<I>
I::Item: IntoIterator<IntoIter = U, Item = U::Item> {}

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

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ impl fmt::Display for ParseFloatError {
}
}

const fn pfe_empty() -> ParseFloatError {
fn pfe_empty() -> ParseFloatError {
ParseFloatError { kind: FloatErrorKind::Empty }
}

const fn pfe_invalid() -> ParseFloatError {
fn pfe_invalid() -> ParseFloatError {
ParseFloatError { kind: FloatErrorKind::Invalid }
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/num/dec2flt/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Decimal<'a> {
}

impl<'a> Decimal<'a> {
pub const fn new(integral: &'a [u8], fractional: &'a [u8], exp: i64) -> Decimal<'a> {
pub fn new(integral: &'a [u8], fractional: &'a [u8], exp: i64) -> Decimal<'a> {
Decimal { integral, fractional, exp }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/num/dec2flt/rawfp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Unpacked {
}

impl Unpacked {
pub const fn new(sig: u64, k: i16) -> Self {
pub fn new(sig: u64, k: i16) -> Self {
Unpacked { sig, k }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/num/flt2dec/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// This is used to approximate `k = ceil(log_10 (mant * 2^exp))`;
/// the true `k` is either `k_0` or `k_0+1`.
#[doc(hidden)]
pub const fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
// 2^(nbits-1) < mant <= 2^nbits if mant > 0
let nbits = 64 - (mant - 1).leading_zeros() as i64;
// 1292913986 = floor(2^32 * log_10 2)
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,7 @@ impl<T: ?Sized> Unique<T> {
}

/// Acquires the underlying `*mut` pointer.
pub const fn as_ptr(self) -> *mut T {
pub fn as_ptr(self) -> *mut T {
self.pointer.0 as *mut T
}

Expand Down
6 changes: 3 additions & 3 deletions src/libcore/slice/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ const HI_USIZE: usize = HI_U64 as usize;
/// bytes where the borrow propagated all the way to the most significant
/// bit."
#[inline]
const fn contains_zero_byte(x: usize) -> bool {
fn contains_zero_byte(x: usize) -> bool {
x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
}

#[cfg(target_pointer_width = "16")]
#[inline]
const fn repeat_byte(b: u8) -> usize {
fn repeat_byte(b: u8) -> usize {
(b as usize) << 8 | b as usize
}

#[cfg(not(target_pointer_width = "16"))]
#[inline]
const fn repeat_byte(b: u8) -> usize {
fn repeat_byte(b: u8) -> usize {
(b as usize) * (::usize::MAX / 255)
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,7 @@ impl<'a, T> IntoIterator for &'a mut [T] {

// Macro helper functions
#[inline(always)]
const fn size_from_ptr<T>(_: *const T) -> usize {
fn size_from_ptr<T>(_: *const T) -> usize {
mem::size_of::<T>()
}

Expand Down
8 changes: 4 additions & 4 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,16 @@ pub struct Chars<'a> {
/// The first byte is special, only want bottom 5 bits for width 2, 4 bits
/// for width 3, and 3 bits for width 4.
#[inline]
const fn utf8_first_byte(byte: u8, width: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
fn utf8_first_byte(byte: u8, width: u32) -> u32 { (byte & (0x7F >> width)) as u32 }

/// Returns the value of `ch` updated with continuation byte `byte`.
#[inline]
const fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }

/// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
/// bits `10`).
#[inline]
const fn utf8_is_cont_byte(byte: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
fn utf8_is_cont_byte(byte: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }

#[inline]
fn unwrap_or_0(opt: Option<&u8>) -> u8 {
Expand Down Expand Up @@ -1420,7 +1420,7 @@ const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize;

/// Returns `true` if any byte in the word `x` is nonascii (>= 128).
#[inline]
const fn contains_nonascii(x: usize) -> bool {
fn contains_nonascii(x: usize) -> bool {
(x & NONASCII_MASK) != 0
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/unicode/bool_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ impl SmallBoolTrie {
}
}

const fn trie_range_leaf(c: u32, bitmap_chunk: u64) -> bool {
fn trie_range_leaf(c: u32, bitmap_chunk: u64) -> bool {
((bitmap_chunk >> (c & 63)) & 1) != 0
}
4 changes: 2 additions & 2 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct DefaultResizePolicy;

impl DefaultResizePolicy {
#[inline]
const fn new() -> DefaultResizePolicy {
fn new() -> DefaultResizePolicy {
DefaultResizePolicy
}

Expand Down Expand Up @@ -69,7 +69,7 @@ impl DefaultResizePolicy {

/// The capacity of the given raw capacity.
#[inline]
const fn capacity(&self, raw_cap: usize) -> usize {
fn capacity(&self, raw_cap: usize) -> usize {
// This doesn't have to be checked for overflow since allocation size
// in bytes will overflow earlier than multiplication by 10.
//
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/collections/hash/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<K, V> RawBucket<K, V> {
// Buckets hold references to the table.
impl<K, V, M> FullBucket<K, V, M> {
/// Borrow a reference to the table.
pub const fn table(&self) -> &M {
pub fn table(&self) -> &M {
&self.table
}
/// Borrow a mutable reference to the table.
Expand All @@ -259,18 +259,18 @@ impl<K, V, M> FullBucket<K, V, M> {
self.table
}
/// Get the raw index.
pub const fn index(&self) -> usize {
pub fn index(&self) -> usize {
self.raw.idx
}
/// Get the raw bucket.
pub const fn raw(&self) -> RawBucket<K, V> {
pub fn raw(&self) -> RawBucket<K, V> {
self.raw
}
}

impl<K, V, M> EmptyBucket<K, V, M> {
/// Borrow a reference to the table.
pub const fn table(&self) -> &M {
pub fn table(&self) -> &M {
&self.table
}
/// Borrow a mutable reference to the table.
Expand All @@ -281,7 +281,7 @@ impl<K, V, M> EmptyBucket<K, V, M> {

impl<K, V, M> Bucket<K, V, M> {
/// Get the raw index.
pub const fn index(&self) -> usize {
pub fn index(&self) -> usize {
self.raw.idx
}
/// get the table.
Expand Down Expand Up @@ -772,7 +772,7 @@ impl<K, V> RawTable<K, V> {

/// The number of elements ever `put` in the hashtable, minus the number
/// of elements ever `take`n.
pub const fn size(&self) -> usize {
pub fn size(&self) -> usize {
self.size
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ enum FromBytesWithNulErrorKind {
}

impl FromBytesWithNulError {
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
fn interior_nul(pos: usize) -> FromBytesWithNulError {
FromBytesWithNulError {
kind: FromBytesWithNulErrorKind::InteriorNul(pos),
}
}
const fn not_nul_terminated() -> FromBytesWithNulError {
fn not_nul_terminated() -> FromBytesWithNulError {
FromBytesWithNulError {
kind: FromBytesWithNulErrorKind::NotNulTerminated,
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>) {
////////////////////////////////////////////////////////////////////////////////

impl<T> Sender<T> {
const fn new(inner: Flavor<T>) -> Sender<T> {
fn new(inner: Flavor<T>) -> Sender<T> {
Sender {
inner: UnsafeCell::new(inner),
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/mpsc/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ enum MyUpgrade<T> {
}

impl<T> Packet<T> {
pub const fn new() -> Packet<T> {
pub fn new() -> Packet<T> {
Packet {
data: UnsafeCell::new(None),
upgrade: UnsafeCell::new(NothingSent),
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys_common/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Mutex {
}

// not meant to be exported to the outside world, just the containing module
pub const fn raw(mutex: &Mutex) -> &imp::Mutex { &mutex.0 }
pub fn raw(mutex: &Mutex) -> &imp::Mutex { &mutex.0 }

#[must_use]
/// A simple RAII utility for the above Mutex without the poisoning semantics.
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl TcpStream {
Ok(TcpStream { inner: sock })
}

pub const fn socket(&self) -> &Socket { &self.inner }
pub fn socket(&self) -> &Socket { &self.inner }

pub fn into_socket(self) -> Socket { self.inner }

Expand Down Expand Up @@ -339,7 +339,7 @@ impl TcpListener {
Ok(TcpListener { inner: sock })
}

pub const fn socket(&self) -> &Socket { &self.inner }
pub fn socket(&self) -> &Socket { &self.inner }

pub fn into_socket(self) -> Socket { self.inner }

Expand Down Expand Up @@ -427,7 +427,7 @@ impl UdpSocket {
Ok(UdpSocket { inner: sock })
}

pub const fn socket(&self) -> &Socket { &self.inner }
pub fn socket(&self) -> &Socket { &self.inner }

pub fn into_socket(self) -> Socket { self.inner }

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ impl CodePoint {
///
/// Since all Unicode scalar values are code points, this always succeeds.
#[inline]
pub const fn from_char(value: char) -> CodePoint {
pub fn from_char(value: char) -> CodePoint {
CodePoint { value: value as u32 }
}

/// Returns the numeric value of the code point.
#[inline]
pub const fn to_u32(&self) -> u32 {
pub fn to_u32(&self) -> u32 {
self.value
}

Expand Down