Relax implicit W: Sized bound on LineWriter<W>

This commit is contained in:
Maybe Waffle 2023-05-01 18:01:04 +00:00
parent a49753373b
commit 29302a204c
2 changed files with 29 additions and 27 deletions

View File

@ -64,7 +64,7 @@
/// } /// }
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct LineWriter<W: Write> { pub struct LineWriter<W: ?Sized + Write> {
inner: BufWriter<W>, inner: BufWriter<W>,
} }
@ -109,27 +109,6 @@ pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
LineWriter { inner: BufWriter::with_capacity(capacity, inner) } LineWriter { inner: BufWriter::with_capacity(capacity, inner) }
} }
/// Gets a reference to the underlying writer.
///
/// # Examples
///
/// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
/// fn main() -> std::io::Result<()> {
/// let file = File::create("poem.txt")?;
/// let file = LineWriter::new(file);
///
/// let reference = file.get_ref();
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &W {
self.inner.get_ref()
}
/// Gets a mutable reference to the underlying writer. /// Gets a mutable reference to the underlying writer.
/// ///
/// Caution must be taken when calling methods on the mutable reference /// Caution must be taken when calling methods on the mutable reference
@ -184,8 +163,31 @@ pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
} }
} }
impl<W: ?Sized + Write> LineWriter<W> {
/// Gets a reference to the underlying writer.
///
/// # Examples
///
/// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
/// fn main() -> std::io::Result<()> {
/// let file = File::create("poem.txt")?;
/// let file = LineWriter::new(file);
///
/// let reference = file.get_ref();
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &W {
self.inner.get_ref()
}
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> Write for LineWriter<W> { impl<W: ?Sized + Write> Write for LineWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
LineWriterShim::new(&mut self.inner).write(buf) LineWriterShim::new(&mut self.inner).write(buf)
} }
@ -216,7 +218,7 @@ fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> fmt::Debug for LineWriter<W> impl<W: ?Sized + Write> fmt::Debug for LineWriter<W>
where where
W: fmt::Debug, W: fmt::Debug,
{ {

View File

@ -11,11 +11,11 @@
/// `BufWriters` to be temporarily given line-buffering logic; this is what /// `BufWriters` to be temporarily given line-buffering logic; this is what
/// enables Stdout to be alternately in line-buffered or block-buffered mode. /// enables Stdout to be alternately in line-buffered or block-buffered mode.
#[derive(Debug)] #[derive(Debug)]
pub struct LineWriterShim<'a, W: Write> { pub struct LineWriterShim<'a, W: ?Sized + Write> {
buffer: &'a mut BufWriter<W>, buffer: &'a mut BufWriter<W>,
} }
impl<'a, W: Write> LineWriterShim<'a, W> { impl<'a, W: ?Sized + Write> LineWriterShim<'a, W> {
pub fn new(buffer: &'a mut BufWriter<W>) -> Self { pub fn new(buffer: &'a mut BufWriter<W>) -> Self {
Self { buffer } Self { buffer }
} }
@ -49,7 +49,7 @@ fn flush_if_completed_line(&mut self) -> io::Result<()> {
} }
} }
impl<'a, W: Write> Write for LineWriterShim<'a, W> { impl<'a, W: ?Sized + Write> Write for LineWriterShim<'a, W> {
/// Write some data into this BufReader with line buffering. This means /// Write some data into this BufReader with line buffering. This means
/// that, if any newlines are present in the data, the data up to the last /// that, if any newlines are present in the data, the data up to the last
/// newline is sent directly to the underlying writer, and data after it /// newline is sent directly to the underlying writer, and data after it