Skip to content

Commit

Permalink
Rollup merge of #120900 - marcospb19:std-use-seek-stream-position, r=…
Browse files Browse the repository at this point in the history
…joshtriplett

std: use `stream_position` where applicable

by replacing `seek(SeekFrom::Current(0))` calls
  • Loading branch information
GuillaumeGomez authored Apr 14, 2024
2 parents 29b1207 + af6840b commit fa483a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
5 changes: 2 additions & 3 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ impl OpenOptions {
/// If a file is opened with both read and append access, beware that after
/// opening, and after every write, the position for reading may be set at the
/// end of the file. So, before writing, save the current position (using
/// <code>[seek]\([SeekFrom]::[Current]\(0))</code>), and restore it before the next read.
/// <code>[Seek]::[stream_position]</code>), and restore it before the next read.
///
/// ## Note
///
Expand All @@ -1002,8 +1002,7 @@ impl OpenOptions {
///
/// [`write()`]: Write::write "io::Write::write"
/// [`flush()`]: Write::flush "io::Write::flush"
/// [seek]: Seek::seek "io::Seek::seek"
/// [Current]: SeekFrom::Current "io::SeekFrom::Current"
/// [stream_position]: Seek::stream_position "io::Seek::stream_position"
///
/// # Examples
///
Expand Down
48 changes: 24 additions & 24 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ fn file_test_io_seek_and_tell_smoke_test() {
{
let mut read_stream = check!(File::open(filename));
check!(read_stream.seek(SeekFrom::Start(set_cursor)));
tell_pos_pre_read = check!(read_stream.seek(SeekFrom::Current(0)));
tell_pos_pre_read = check!(read_stream.stream_position());
check!(read_stream.read(&mut read_mem));
tell_pos_post_read = check!(read_stream.seek(SeekFrom::Current(0)));
tell_pos_post_read = check!(read_stream.stream_position());
}
check!(fs::remove_file(filename));
let read_str = str::from_utf8(&read_mem).unwrap();
Expand Down Expand Up @@ -284,42 +284,42 @@ fn file_test_io_read_write_at() {
let oo = OpenOptions::new().create_new(true).write(true).read(true).clone();
let mut rw = check!(oo.open(&filename));
assert_eq!(check!(rw.write_at(write1.as_bytes(), 5)), write1.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
assert_eq!(check!(rw.stream_position()), 0);
assert_eq!(check!(rw.read_at(&mut buf, 5)), write1.len());
assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
assert_eq!(check!(rw.stream_position()), 0);
assert_eq!(check!(rw.read_at(&mut buf[..write2.len()], 0)), write2.len());
assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok("\0\0\0\0\0"));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
assert_eq!(check!(rw.stream_position()), 0);
assert_eq!(check!(rw.write(write2.as_bytes())), write2.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
assert_eq!(check!(rw.stream_position()), 5);
assert_eq!(check!(rw.read(&mut buf)), write1.len());
assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
assert_eq!(check!(rw.read_at(&mut buf[..write2.len()], 0)), write2.len());
assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok(write2));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
assert_eq!(check!(rw.write_at(write3.as_bytes(), 9)), write3.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
}
{
let mut read = check!(File::open(&filename));
assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 0);
assert_eq!(check!(read.stream_position()), 0);
assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(read.stream_position()), 9);
assert_eq!(check!(read.read(&mut buf)), write3.len());
assert_eq!(str::from_utf8(&buf[..write3.len()]), Ok(write3));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.read_at(&mut buf, 14)), 0);
assert_eq!(check!(read.read_at(&mut buf, 15)), 0);
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
}
check!(fs::remove_file(&filename));
}
Expand Down Expand Up @@ -362,38 +362,38 @@ fn file_test_io_seek_read_write() {
let oo = OpenOptions::new().create_new(true).write(true).read(true).clone();
let mut rw = check!(oo.open(&filename));
assert_eq!(check!(rw.seek_write(write1.as_bytes(), 5)), write1.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
assert_eq!(check!(rw.seek_read(&mut buf, 5)), write1.len());
assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
assert_eq!(check!(rw.seek(SeekFrom::Start(0))), 0);
assert_eq!(check!(rw.write(write2.as_bytes())), write2.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
assert_eq!(check!(rw.stream_position()), 5);
assert_eq!(check!(rw.read(&mut buf)), write1.len());
assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
assert_eq!(check!(rw.stream_position()), 9);
assert_eq!(check!(rw.seek_read(&mut buf[..write2.len()], 0)), write2.len());
assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok(write2));
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
assert_eq!(check!(rw.stream_position()), 5);
assert_eq!(check!(rw.seek_write(write3.as_bytes(), 9)), write3.len());
assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(rw.stream_position()), 14);
}
{
let mut read = check!(File::open(&filename));
assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
assert_eq!(check!(read.read(&mut buf)), write3.len());
assert_eq!(str::from_utf8(&buf[..write3.len()]), Ok(write3));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
assert_eq!(check!(read.stream_position()), 14);
assert_eq!(check!(read.seek_read(&mut buf, 14)), 0);
assert_eq!(check!(read.seek_read(&mut buf, 15)), 0);
}
Expand Down
7 changes: 5 additions & 2 deletions library/std/src/io/buffered/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn test_buffered_reader_seek() {

assert_eq!(reader.seek(SeekFrom::Start(3)).ok(), Some(3));
assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
assert_eq!(reader.seek(SeekFrom::Current(0)).ok(), Some(3));
assert_eq!(reader.stream_position().ok(), Some(3));
assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
assert_eq!(reader.seek(SeekFrom::Current(1)).ok(), Some(4));
assert_eq!(reader.fill_buf().ok(), Some(&[1, 2][..]));
Expand Down Expand Up @@ -230,6 +230,9 @@ fn test_buffered_reader_seek_underflow() {
Ok(len)
}
}
// note: this implementation of `Seek` is "broken" due to position
// wrapping, so calling `reader.seek(Current(0))` is semantically different
// than `reader.stream_position()`
impl Seek for PositionReader {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match pos {
Expand Down Expand Up @@ -374,7 +377,7 @@ fn test_buffered_writer_seek() {
let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new()));
w.write_all(&[0, 1, 2, 3, 4, 5]).unwrap();
w.write_all(&[6, 7]).unwrap();
assert_eq!(w.seek(SeekFrom::Current(0)).ok(), Some(8));
assert_eq!(w.stream_position().ok(), Some(8));
assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]);
assert_eq!(w.seek(SeekFrom::Start(2)).ok(), Some(2));
w.write_all(&[8, 9]).unwrap();
Expand Down

0 comments on commit fa483a4

Please sign in to comment.