Skip to content
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

Implement io::Seek for io::Empty #78044

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 16 additions & 1 deletion library/std/src/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod tests;

use crate::fmt;
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Write};
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};

/// A reader which is always at EOF.
///
Expand Down Expand Up @@ -58,6 +58,21 @@ impl BufRead for Empty {
fn consume(&mut self, _n: usize) {}
}
oberien marked this conversation as resolved.
Show resolved Hide resolved

#[stable(feature = "empty_seek", since = "1.51.0")]
impl Seek for Empty {
fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
Ok(0)
}

fn stream_len(&mut self) -> io::Result<u64> {
Ok(0)
}

fn stream_position(&mut self) -> io::Result<u64> {
Ok(0)
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
impl fmt::Debug for Empty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
22 changes: 21 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, Empty, Repeat, Sink};
use crate::io::{copy, empty, repeat, sink, Empty, Repeat, SeekFrom, Sink};

#[test]
fn copy_copies() {
Expand Down Expand Up @@ -29,6 +29,26 @@ fn empty_reads() {
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
}

#[test]
fn empty_seeks() {
let mut e = empty();
assert!(matches!(e.seek(SeekFrom::Start(0)), Ok(0)));
oberien marked this conversation as resolved.
Show resolved Hide resolved
assert!(matches!(e.seek(SeekFrom::Start(1)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::Start(u64::MAX)), Ok(0)));

assert!(matches!(e.seek(SeekFrom::End(i64::MIN)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::End(-1)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::End(0)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::End(1)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::End(i64::MAX)), Ok(0)));

assert!(matches!(e.seek(SeekFrom::Current(i64::MIN)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::Current(-1)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::Current(0)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::Current(1)), Ok(0)));
assert!(matches!(e.seek(SeekFrom::Current(i64::MAX)), Ok(0)));
}

#[test]
fn repeat_repeats() {
let mut r = repeat(4);
Expand Down