Skip to content

Commit f1cd179

Browse files
committed
impl Seek for Empty
Fix #78029
1 parent bf469eb commit f1cd179

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

library/std/src/io/util.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mod tests;
55

66
use crate::fmt;
7-
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Write};
7+
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
88

99
/// A reader which is always at EOF.
1010
///
@@ -58,6 +58,21 @@ impl BufRead for Empty {
5858
fn consume(&mut self, _n: usize) {}
5959
}
6060

61+
#[stable(feature = "empty_seek", since = "1.51.0")]
62+
impl Seek for Empty {
63+
fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
64+
Ok(0)
65+
}
66+
67+
fn stream_len(&mut self) -> io::Result<u64> {
68+
Ok(0)
69+
}
70+
71+
fn stream_position(&mut self) -> io::Result<u64> {
72+
Ok(0)
73+
}
74+
}
75+
6176
#[stable(feature = "std_debug", since = "1.16.0")]
6277
impl fmt::Debug for Empty {
6378
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

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

+21-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, Empty, Repeat, Sink};
2+
use crate::io::{copy, empty, repeat, sink, Empty, Repeat, SeekFrom, Sink};
33

44
#[test]
55
fn copy_copies() {
@@ -29,6 +29,26 @@ fn empty_reads() {
2929
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
3030
}
3131

32+
#[test]
33+
fn empty_seeks() {
34+
let mut e = empty();
35+
assert!(matches!(e.seek(SeekFrom::Start(0)), Ok(0)));
36+
assert!(matches!(e.seek(SeekFrom::Start(1)), Ok(0)));
37+
assert!(matches!(e.seek(SeekFrom::Start(u64::MAX)), Ok(0)));
38+
39+
assert!(matches!(e.seek(SeekFrom::End(i64::MIN)), Ok(0)));
40+
assert!(matches!(e.seek(SeekFrom::End(-1)), Ok(0)));
41+
assert!(matches!(e.seek(SeekFrom::End(0)), Ok(0)));
42+
assert!(matches!(e.seek(SeekFrom::End(1)), Ok(0)));
43+
assert!(matches!(e.seek(SeekFrom::End(i64::MAX)), Ok(0)));
44+
45+
assert!(matches!(e.seek(SeekFrom::Current(i64::MIN)), Ok(0)));
46+
assert!(matches!(e.seek(SeekFrom::Current(-1)), Ok(0)));
47+
assert!(matches!(e.seek(SeekFrom::Current(0)), Ok(0)));
48+
assert!(matches!(e.seek(SeekFrom::Current(1)), Ok(0)));
49+
assert!(matches!(e.seek(SeekFrom::Current(i64::MAX)), Ok(0)));
50+
}
51+
3252
#[test]
3353
fn repeat_repeats() {
3454
let mut r = repeat(4);

0 commit comments

Comments
 (0)