Skip to content

Commit e78fd66

Browse files
authored
Rollup merge of #78811 - a1phyr:const_io_structs, r=dtolnay
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`
2 parents 31c3252 + 001dd7e commit e78fd66

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

library/std/src/io/cursor.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl<T> Cursor<T> {
9494
/// # force_inference(&buff);
9595
/// ```
9696
#[stable(feature = "rust1", since = "1.0.0")]
97-
pub fn new(inner: T) -> Cursor<T> {
97+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
98+
pub const fn new(inner: T) -> Cursor<T> {
9899
Cursor { pos: 0, inner }
99100
}
100101

@@ -130,7 +131,8 @@ impl<T> Cursor<T> {
130131
/// let reference = buff.get_ref();
131132
/// ```
132133
#[stable(feature = "rust1", since = "1.0.0")]
133-
pub fn get_ref(&self) -> &T {
134+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
135+
pub const fn get_ref(&self) -> &T {
134136
&self.inner
135137
}
136138

@@ -175,7 +177,8 @@ impl<T> Cursor<T> {
175177
/// assert_eq!(buff.position(), 1);
176178
/// ```
177179
#[stable(feature = "rust1", since = "1.0.0")]
178-
pub fn position(&self) -> u64 {
180+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
181+
pub const fn position(&self) -> u64 {
179182
self.pos
180183
}
181184

library/std/src/io/cursor/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,10 @@ fn test_eq() {
514514

515515
let _: AssertEq<Cursor<Vec<u8>>> = AssertEq(Cursor::new(Vec::new()));
516516
}
517+
518+
#[allow(dead_code)]
519+
fn const_cursor() {
520+
const CURSOR: Cursor<&[u8]> = Cursor::new(&[0]);
521+
const _: &&[u8] = CURSOR.get_ref();
522+
const _: u64 = CURSOR.position();
523+
}

library/std/src/io/util.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ pub struct Empty {
102102
/// assert!(buffer.is_empty());
103103
/// ```
104104
#[stable(feature = "rust1", since = "1.0.0")]
105-
pub fn empty() -> Empty {
105+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
106+
pub const fn empty() -> Empty {
106107
Empty { _priv: () }
107108
}
108109

@@ -159,7 +160,8 @@ pub struct Repeat {
159160
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
160161
/// ```
161162
#[stable(feature = "rust1", since = "1.0.0")]
162-
pub fn repeat(byte: u8) -> Repeat {
163+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
164+
pub const fn repeat(byte: u8) -> Repeat {
163165
Repeat { byte }
164166
}
165167

@@ -226,7 +228,8 @@ pub struct Sink {
226228
/// assert_eq!(num_bytes, 5);
227229
/// ```
228230
#[stable(feature = "rust1", since = "1.0.0")]
229-
pub fn sink() -> Sink {
231+
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
232+
pub const fn sink() -> Sink {
230233
Sink { _priv: () }
231234
}
232235

library/std/src/io/util/tests.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::io::prelude::*;
2-
use crate::io::{copy, empty, repeat, sink};
2+
use crate::io::{copy, empty, repeat, sink, Empty, Repeat, Sink};
33

44
#[test]
55
fn copy_copies() {
@@ -43,3 +43,10 @@ fn take_some_bytes() {
4343
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
4444
assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
4545
}
46+
47+
#[allow(dead_code)]
48+
fn const_utils() {
49+
const _: Empty = empty();
50+
const _: Repeat = repeat(b'c');
51+
const _: Sink = sink();
52+
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
#![feature(const_fn_transmute)]
243243
#![feature(const_fn)]
244244
#![feature(const_fn_fn_ptr_basics)]
245+
#![feature(const_io_structs)]
245246
#![feature(const_ip)]
246247
#![feature(const_ipv6)]
247248
#![feature(const_raw_ptr_deref)]

0 commit comments

Comments
 (0)