From fa5143f942e1ea9b487fc9cc336077d16964f64b Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 22 Feb 2023 17:44:50 +0100 Subject: [PATCH 1/2] Use the inner stream_len impl instead of using the Seek default This is useful if stream_len is overridden by the Seek implementation of the inner member. --- library/std/src/io/buffered/bufreader.rs | 4 ++++ library/std/src/io/buffered/bufwriter.rs | 4 ++++ library/std/src/io/impls.rs | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index 4f339a18a480e..0b64d4ab4434b 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -489,6 +489,10 @@ impl Seek for BufReader { ) }) } + + fn stream_len(&mut self) -> io::Result { + self.inner.stream_len() + } } impl SizeHint for BufReader { diff --git a/library/std/src/io/buffered/bufwriter.rs b/library/std/src/io/buffered/bufwriter.rs index 6acb937e78479..d7697db878c9b 100644 --- a/library/std/src/io/buffered/bufwriter.rs +++ b/library/std/src/io/buffered/bufwriter.rs @@ -661,6 +661,10 @@ impl Seek for BufWriter { self.flush_buf()?; self.get_mut().seek(pos) } + + fn stream_len(&mut self) -> io::Result { + self.inner.stream_len() + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index e5048dcc8acd9..bcbf978432b3b 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -93,6 +93,11 @@ impl Seek for &mut S { fn stream_position(&mut self) -> io::Result { (**self).stream_position() } + + #[inline] + fn stream_len(&mut self) -> io::Result { + (**self).stream_len() + } } #[stable(feature = "rust1", since = "1.0.0")] impl BufRead for &mut B { @@ -197,6 +202,11 @@ impl Seek for Box { fn stream_position(&mut self) -> io::Result { (**self).stream_position() } + + #[inline] + fn stream_len(&mut self) -> io::Result { + (**self).stream_len() + } } #[stable(feature = "rust1", since = "1.0.0")] impl BufRead for Box { From 811d5b47827776a3b8966ba88ed733524f1288f8 Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 22 Feb 2023 17:45:53 +0100 Subject: [PATCH 2/2] Make stream_len atomic for File --- library/std/src/fs.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index c550378e7d6b7..1c63280fb0ca6 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -795,6 +795,11 @@ impl Seek for File { fn seek(&mut self, pos: SeekFrom) -> io::Result { self.inner.seek(pos) } + + fn stream_len(&mut self) -> io::Result { + let file_attr = self.inner.file_attr()?; + Ok(file_attr.size()) + } } #[stable(feature = "rust1", since = "1.0.0")] impl Read for &File { @@ -851,6 +856,10 @@ impl Seek for &File { fn seek(&mut self, pos: SeekFrom) -> io::Result { self.inner.seek(pos) } + fn stream_len(&mut self) -> io::Result { + let file_attr = self.inner.file_attr()?; + Ok(file_attr.size()) + } } impl OpenOptions {