Skip to content

Commit

Permalink
Rollup merge of #78811 - a1phyr:const_io_structs, r=dtolnay
Browse files Browse the repository at this point in the history
Make some std::io functions `const`

Tracking issue: #78812

Make the following functions `const`:
- `io::Cursor::new`
- `io::Cursor::get_ref`
- `io::Cursor::position`
- `io::empty`
- `io::repeat`
- `io::sink`

r? `````@dtolnay`````
  • Loading branch information
m-ou-se authored Nov 8, 2020
2 parents 3541280 + 001dd7e commit 77f333b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
9 changes: 6 additions & 3 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ impl<T> Cursor<T> {
/// # force_inference(&buff);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: T) -> Cursor<T> {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn new(inner: T) -> Cursor<T> {
Cursor { pos: 0, inner }
}

Expand Down Expand Up @@ -130,7 +131,8 @@ impl<T> Cursor<T> {
/// let reference = buff.get_ref();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &T {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn get_ref(&self) -> &T {
&self.inner
}

Expand Down Expand Up @@ -175,7 +177,8 @@ impl<T> Cursor<T> {
/// assert_eq!(buff.position(), 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn position(&self) -> u64 {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn position(&self) -> u64 {
self.pos
}

Expand Down
7 changes: 7 additions & 0 deletions library/std/src/io/cursor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,10 @@ fn test_eq() {

let _: AssertEq<Cursor<Vec<u8>>> = AssertEq(Cursor::new(Vec::new()));
}

#[allow(dead_code)]
fn const_cursor() {
const CURSOR: Cursor<&[u8]> = Cursor::new(&[0]);
const _: &&[u8] = CURSOR.get_ref();
const _: u64 = CURSOR.position();
}
9 changes: 6 additions & 3 deletions library/std/src/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pub struct Empty {
/// assert!(buffer.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn empty() -> Empty {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn empty() -> Empty {
Empty { _priv: () }
}

Expand Down Expand Up @@ -159,7 +160,8 @@ pub struct Repeat {
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn repeat(byte: u8) -> Repeat {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn repeat(byte: u8) -> Repeat {
Repeat { byte }
}

Expand Down Expand Up @@ -226,7 +228,8 @@ pub struct Sink {
/// assert_eq!(num_bytes, 5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn sink() -> Sink {
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
pub const fn sink() -> Sink {
Sink { _priv: () }
}

Expand Down
9 changes: 8 additions & 1 deletion library/std/src/io/util/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io::prelude::*;
use crate::io::{copy, empty, repeat, sink};
use crate::io::{copy, empty, repeat, sink, Empty, Repeat, Sink};

#[test]
fn copy_copies() {
Expand Down Expand Up @@ -43,3 +43,10 @@ fn take_some_bytes() {
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
}

#[allow(dead_code)]
fn const_utils() {
const _: Empty = empty();
const _: Repeat = repeat(b'c');
const _: Sink = sink();
}
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
#![feature(const_fn_transmute)]
#![feature(const_fn)]
#![feature(const_fn_fn_ptr_basics)]
#![feature(const_io_structs)]
#![feature(const_ip)]
#![feature(const_ipv6)]
#![feature(const_raw_ptr_deref)]
Expand Down

0 comments on commit 77f333b

Please sign in to comment.