提交 f65b630d 编写于 作者: M Mazdak Farrokhzad

constify parts of libstd.

上级 061d345c
文件模式从 100755 更改为 100644
......@@ -36,7 +36,7 @@
impl DefaultResizePolicy {
#[inline]
fn new() -> DefaultResizePolicy {
const fn new() -> DefaultResizePolicy {
DefaultResizePolicy
}
......@@ -69,7 +69,7 @@ fn raw_capacity(&self, len: usize) -> usize {
/// The capacity of the given raw capacity.
#[inline]
fn capacity(&self, raw_cap: usize) -> usize {
const 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.
//
......@@ -3013,7 +3013,7 @@ impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ```
#[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K {
pub const fn key(&self) -> &K {
&self.key
}
......
......@@ -247,7 +247,7 @@ unsafe fn hash_pair(&self) -> (*mut HashUint, *mut (K, V)) {
// Buckets hold references to the table.
impl<K, V, M> FullBucket<K, V, M> {
/// Borrow a reference to the table.
pub fn table(&self) -> &M {
pub const fn table(&self) -> &M {
&self.table
}
/// Borrow a mutable reference to the table.
......@@ -259,18 +259,18 @@ pub fn into_table(self) -> M {
self.table
}
/// Get the raw index.
pub fn index(&self) -> usize {
pub const fn index(&self) -> usize {
self.raw.idx
}
/// Get the raw bucket.
pub fn raw(&self) -> RawBucket<K, V> {
pub const fn raw(&self) -> RawBucket<K, V> {
self.raw
}
}
impl<K, V, M> EmptyBucket<K, V, M> {
/// Borrow a reference to the table.
pub fn table(&self) -> &M {
pub const fn table(&self) -> &M {
&self.table
}
/// Borrow a mutable reference to the table.
......@@ -281,7 +281,7 @@ pub fn table_mut(&mut self) -> &mut M {
impl<K, V, M> Bucket<K, V, M> {
/// Get the raw index.
pub fn index(&self) -> usize {
pub const fn index(&self) -> usize {
self.raw.idx
}
/// get the table.
......@@ -772,7 +772,7 @@ pub fn capacity(&self) -> usize {
/// The number of elements ever `put` in the hashtable, minus the number
/// of elements ever `take`n.
pub fn size(&self) -> usize {
pub const fn size(&self) -> usize {
self.size
}
......
......@@ -265,12 +265,12 @@ enum FromBytesWithNulErrorKind {
}
impl FromBytesWithNulError {
fn interior_nul(pos: usize) -> FromBytesWithNulError {
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
FromBytesWithNulError {
kind: FromBytesWithNulErrorKind::InteriorNul(pos),
}
}
fn not_nul_terminated() -> FromBytesWithNulError {
const fn not_nul_terminated() -> FromBytesWithNulError {
FromBytesWithNulError {
kind: FromBytesWithNulErrorKind::NotNulTerminated,
}
......@@ -833,7 +833,7 @@ impl NulError {
/// assert_eq!(nul_error.nul_position(), 7);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn nul_position(&self) -> usize { self.0 }
pub const fn nul_position(&self) -> usize { self.0 }
/// Consumes this error, returning the underlying vector of bytes which
/// generated the error in the first place.
......@@ -909,7 +909,7 @@ pub fn into_cstring(self) -> CString {
/// Access the underlying UTF-8 error that was the cause of this error.
#[stable(feature = "cstring_into", since = "1.7.0")]
pub fn utf8_error(&self) -> Utf8Error {
pub const fn utf8_error(&self) -> Utf8Error {
self.error
}
}
......@@ -1091,7 +1091,7 @@ pub fn from_bytes_with_nul(bytes: &[u8])
/// [`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()
}
......
......@@ -633,7 +633,7 @@ impl<W> IntoInnerError<W> {
/// };
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn error(&self) -> &Error { &self.1 }
pub const fn error(&self) -> &Error { &self.1 }
/// Returns the buffered writer instance which generated the error.
///
......
......@@ -104,7 +104,7 @@ impl<T> Cursor<T> {
/// # force_inference(&buff);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: T) -> Cursor<T> {
pub const fn new(inner: T) -> Cursor<T> {
Cursor { pos: 0, inner: inner }
}
......@@ -138,7 +138,7 @@ pub fn into_inner(self) -> T { self.inner }
/// let reference = buff.get_ref();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &T { &self.inner }
pub const fn get_ref(&self) -> &T { &self.inner }
/// Gets a mutable reference to the underlying value in this cursor.
///
......@@ -179,7 +179,7 @@ pub fn get_mut(&mut self) -> &mut T { &mut self.inner }
/// assert_eq!(buff.position(), 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn position(&self) -> u64 { self.pos }
pub const fn position(&self) -> u64 { self.pos }
/// Sets the position of this cursor.
///
......
......@@ -885,7 +885,7 @@ impl Initializer {
/// Returns a new `Initializer` which will zero out buffers.
#[unstable(feature = "read_initializer", issue = "42788")]
#[inline]
pub fn zeroing() -> Initializer {
pub const fn zeroing() -> Initializer {
Initializer(true)
}
......@@ -906,7 +906,7 @@ pub unsafe fn nop() -> Initializer {
/// Indicates if a buffer should be initialized.
#[unstable(feature = "read_initializer", issue = "42788")]
#[inline]
pub fn should_initialize(&self) -> bool {
pub const fn should_initialize(&self) -> bool {
self.0
}
......@@ -1653,7 +1653,7 @@ pub fn into_inner(self) -> (T, U) {
/// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_ref(&self) -> (&T, &U) {
pub const fn get_ref(&self) -> (&T, &U) {
(&self.first, &self.second)
}
......@@ -1780,7 +1780,7 @@ impl<T> Take<T> {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn limit(&self) -> u64 { self.limit }
pub const fn limit(&self) -> u64 { self.limit }
/// Sets the number of bytes that can be read before this instance will
/// return EOF. This is the same as constructing a new `Take` instance, so
......@@ -1856,7 +1856,7 @@ pub fn into_inner(self) -> T {
/// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_ref(&self) -> &T {
pub const fn get_ref(&self) -> &T {
&self.inner
}
......
......@@ -99,7 +99,7 @@ pub struct Empty { _priv: () }
/// assert!(buffer.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn empty() -> Empty { Empty { _priv: () } }
pub const fn empty() -> Empty { Empty { _priv: () } }
#[stable(feature = "rust1", since = "1.0.0")]
impl Read for Empty {
......@@ -199,7 +199,7 @@ pub struct Sink { _priv: () }
/// assert_eq!(num_bytes, 5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn sink() -> Sink { Sink { _priv: () } }
pub const fn sink() -> Sink { Sink { _priv: () } }
#[stable(feature = "rust1", since = "1.0.0")]
impl Write for Sink {
......
......@@ -475,7 +475,7 @@ pub fn set_port(&mut self, new_port: u16) {
/// assert_eq!(socket.flowinfo(), 10);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn flowinfo(&self) -> u32 {
pub const fn flowinfo(&self) -> u32 {
self.inner.sin6_flowinfo
}
......@@ -515,7 +515,7 @@ pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
/// assert_eq!(socket.scope_id(), 78);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn scope_id(&self) -> u32 {
pub const fn scope_id(&self) -> u32 {
self.inner.sin6_scope_id
}
......
......@@ -402,7 +402,7 @@ pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
/// assert_eq!(addr.octets(), [127, 0, 0, 1]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn octets(&self) -> [u8; 4] {
pub fn octets(&self) -> [u8; 4] {
let bits = u32::from_be(self.inner.s_addr);
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
}
......@@ -424,7 +424,7 @@ pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_unspecified(&self) -> bool {
pub const fn is_unspecified(&self) -> bool {
self.inner.s_addr == 0
}
......@@ -862,7 +862,6 @@ impl Ipv6Addr {
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_ip")]
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16,
g: u16, h: u16) -> Ipv6Addr {
Ipv6Addr {
......@@ -1224,7 +1223,7 @@ pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
/// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
/// ```
#[stable(feature = "ipv6_to_octets", since = "1.12.0")]
pub fn octets(&self) -> [u8; 16] {
pub const fn octets(&self) -> [u8; 16] {
self.inner.s6_addr
}
}
......
......@@ -439,7 +439,7 @@ impl<'a> PrefixComponent<'a> {
///
/// [`Prefix`]: enum.Prefix.html
#[stable(feature = "rust1", since = "1.0.0")]
pub fn kind(&self) -> Prefix<'a> {
pub const fn kind(&self) -> Prefix<'a> {
self.parsed
}
......@@ -447,7 +447,7 @@ pub fn kind(&self) -> Prefix<'a> {
///
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_os_str(&self) -> &'a OsStr {
pub const fn as_os_str(&self) -> &'a OsStr {
self.raw
}
}
......@@ -1918,7 +1918,7 @@ pub fn parent(&self) -> Option<&Path> {
/// [`None`]: ../../std/option/enum.Option.html#variant.None
/// [`parent`]: struct.Path.html#method.parent
#[stable(feature = "path_ancestors", since = "1.28.0")]
pub fn ancestors(&self) -> Ancestors {
pub const fn ancestors(&self) -> Ancestors {
Ancestors {
next: Some(&self),
}
......@@ -2267,7 +2267,7 @@ pub fn iter(&self) -> Iter {
/// println!("{}", path.display());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn display(&self) -> Display {
pub const fn display(&self) -> Display {
Display { path: self }
}
......
......@@ -926,7 +926,7 @@ impl Stdio {
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH\n");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
pub const fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
/// The child inherits from the corresponding parent descriptor.
///
......@@ -961,7 +961,7 @@ pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
/// println!("You piped in the reverse of: {}", String::from_utf8_lossy(&output.stdout));
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
pub const fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
/// This stream will be ignored. This is the equivalent of attaching the
/// stream to `/dev/null`
......@@ -998,7 +998,7 @@ pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
/// // Ignores any piped-in input
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn null() -> Stdio { Stdio(imp::Stdio::Null) }
pub const fn null() -> Stdio { Stdio(imp::Stdio::Null) }
}
impl FromInner<imp::Stdio> for Stdio {
......
......@@ -72,7 +72,7 @@ impl WaitTimeoutResult {
/// }
/// ```
#[stable(feature = "wait_timeout", since = "1.5.0")]
pub fn timed_out(&self) -> bool {
pub const fn timed_out(&self) -> bool {
self.0
}
}
......
......@@ -785,7 +785,7 @@ pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>) {
////////////////////////////////////////////////////////////////////////////////
impl<T> Sender<T> {
fn new(inner: Flavor<T>) -> Sender<T> {
const fn new(inner: Flavor<T>) -> Sender<T> {
Sender {
inner: UnsafeCell::new(inner),
}
......@@ -1469,7 +1469,7 @@ pub fn recv_deadline(&self, deadline: Instant) -> Result<T, RecvTimeoutError> {
/// assert_eq!(iter.next(), None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<T> {
pub const fn iter(&self) -> Iter<T> {
Iter { rx: self }
}
......@@ -1512,7 +1512,7 @@ pub fn iter(&self) -> Iter<T> {
/// assert_eq!(iter.next(), None);
/// ```
#[stable(feature = "receiver_try_iter", since = "1.15.0")]
pub fn try_iter(&self) -> TryIter<T> {
pub const fn try_iter(&self) -> TryIter<T> {
TryIter { rx: self }
}
......
......@@ -89,7 +89,7 @@ enum MyUpgrade<T> {
}
impl<T> Packet<T> {
pub fn new() -> Packet<T> {
pub const fn new() -> Packet<T> {
Packet {
data: UnsafeCell::new(None),
upgrade: UnsafeCell::new(NothingSent),
......
......@@ -517,7 +517,7 @@ impl OnceState {
/// assert!(!state.poisoned());
/// });
#[unstable(feature = "once_poison", issue = "33577")]
pub fn poisoned(&self) -> bool {
pub const fn poisoned(&self) -> bool {
self.poisoned
}
}
......
......@@ -80,7 +80,7 @@ pub unsafe fn destroy(&self) { self.0.destroy() }
}
// not meant to be exported to the outside world, just the containing module
pub fn raw(mutex: &Mutex) -> &imp::Mutex { &mutex.0 }
pub const fn raw(mutex: &Mutex) -> &imp::Mutex { &mutex.0 }
#[must_use]
/// A simple RAII utility for the above Mutex without the poisoning semantics.
......
......@@ -199,7 +199,7 @@ pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpSt
Ok(TcpStream { inner: sock })
}
pub fn socket(&self) -> &Socket { &self.inner }
pub const fn socket(&self) -> &Socket { &self.inner }
pub fn into_socket(self) -> Socket { self.inner }
......@@ -339,7 +339,7 @@ pub fn bind(addr: &SocketAddr) -> io::Result<TcpListener> {
Ok(TcpListener { inner: sock })
}
pub fn socket(&self) -> &Socket { &self.inner }
pub const fn socket(&self) -> &Socket { &self.inner }
pub fn into_socket(self) -> Socket { self.inner }
......@@ -427,7 +427,7 @@ pub fn bind(addr: &SocketAddr) -> io::Result<UdpSocket> {
Ok(UdpSocket { inner: sock })
}
pub fn socket(&self) -> &Socket { &self.inner }
pub const fn socket(&self) -> &Socket { &self.inner }
pub fn into_socket(self) -> Socket { self.inner }
......
......@@ -85,13 +85,13 @@ pub fn from_u32(value: u32) -> Option<CodePoint> {
///
/// Since all Unicode scalar values are code points, this always succeeds.
#[inline]
pub fn from_char(value: char) -> CodePoint {
pub const fn from_char(value: char) -> CodePoint {
CodePoint { value: value as u32 }
}
/// Returns the numeric value of the code point.
#[inline]
pub fn to_u32(&self) -> u32 {
pub const fn to_u32(&self) -> u32 {
self.value
}
......
......@@ -286,7 +286,7 @@ impl Builder {
/// handler.join().unwrap();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> Builder {
pub const fn new() -> Builder {
Builder {
name: None,
stack_size: None,
......@@ -1391,7 +1391,7 @@ impl<T> JoinHandle<T> {
/// println!("thread id: {:?}", thread.id());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn thread(&self) -> &Thread {
pub const fn thread(&self) -> &Thread {
&self.0.thread
}
......
......@@ -449,7 +449,7 @@ impl SystemTimeError {
/// }
/// ```
#[stable(feature = "time2", since = "1.8.0")]
pub fn duration(&self) -> Duration {
pub const fn duration(&self) -> Duration {
self.0
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册