Skip to content

Commit 8837bf1

Browse files
committed
Remove Option from BufWriter
Fixes #72925
1 parent 1158367 commit 8837bf1

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

library/std/src/io/buffered/bufwriter.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use crate::ptr;
6868
/// [`flush`]: BufWriter::flush
6969
#[stable(feature = "rust1", since = "1.0.0")]
7070
pub struct BufWriter<W: Write> {
71-
inner: Option<W>,
71+
inner: W,
7272
// The buffer. Avoid using this like a normal `Vec` in common code paths.
7373
// That is, don't use `buf.push`, `buf.extend_from_slice`, or any other
7474
// methods that require bounds checking or the like. This makes an enormous
@@ -112,7 +112,7 @@ impl<W: Write> BufWriter<W> {
112112
/// ```
113113
#[stable(feature = "rust1", since = "1.0.0")]
114114
pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
115-
BufWriter { inner: Some(inner), buf: Vec::with_capacity(capacity), panicked: false }
115+
BufWriter { inner, buf: Vec::with_capacity(capacity), panicked: false }
116116
}
117117

118118
/// Send data in our local buffer into the inner writer, looping as
@@ -161,10 +161,9 @@ impl<W: Write> BufWriter<W> {
161161
}
162162

163163
let mut guard = BufGuard::new(&mut self.buf);
164-
let inner = self.inner.as_mut().unwrap();
165164
while !guard.done() {
166165
self.panicked = true;
167-
let r = inner.write(guard.remaining());
166+
let r = self.inner.write(guard.remaining());
168167
self.panicked = false;
169168

170169
match r {
@@ -212,7 +211,7 @@ impl<W: Write> BufWriter<W> {
212211
/// ```
213212
#[stable(feature = "rust1", since = "1.0.0")]
214213
pub fn get_ref(&self) -> &W {
215-
self.inner.as_ref().unwrap()
214+
&self.inner
216215
}
217216

218217
/// Gets a mutable reference to the underlying writer.
@@ -232,7 +231,7 @@ impl<W: Write> BufWriter<W> {
232231
/// ```
233232
#[stable(feature = "rust1", since = "1.0.0")]
234233
pub fn get_mut(&mut self) -> &mut W {
235-
self.inner.as_mut().unwrap()
234+
&mut self.inner
236235
}
237236

238237
/// Returns a reference to the internally buffered data.
@@ -308,7 +307,7 @@ impl<W: Write> BufWriter<W> {
308307
pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
309308
match self.flush_buf() {
310309
Err(e) => Err(IntoInnerError::new(self, e)),
311-
Ok(()) => Ok(self.inner.take().unwrap()),
310+
Ok(()) => Ok(self.into_raw_parts().0),
312311
}
313312
}
314313

@@ -339,7 +338,12 @@ impl<W: Write> BufWriter<W> {
339338
pub fn into_raw_parts(mut self) -> (W, Result<Vec<u8>, WriterPanicked>) {
340339
let buf = mem::take(&mut self.buf);
341340
let buf = if !self.panicked { Ok(buf) } else { Err(WriterPanicked { buf }) };
342-
(self.inner.take().unwrap(), buf)
341+
342+
// SAFETY: forget(self) prevents double dropping inner
343+
let inner = unsafe { ptr::read(&mut self.inner) };
344+
mem::forget(self);
345+
346+
(inner, buf)
343347
}
344348

345349
// Ensure this function does not get inlined into `write`, so that it
@@ -643,7 +647,7 @@ where
643647
{
644648
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
645649
fmt.debug_struct("BufWriter")
646-
.field("writer", &self.inner.as_ref().unwrap())
650+
.field("writer", &self.inner)
647651
.field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
648652
.finish()
649653
}
@@ -663,7 +667,7 @@ impl<W: Write + Seek> Seek for BufWriter<W> {
663667
#[stable(feature = "rust1", since = "1.0.0")]
664668
impl<W: Write> Drop for BufWriter<W> {
665669
fn drop(&mut self) {
666-
if self.inner.is_some() && !self.panicked {
670+
if !self.panicked {
667671
// dtors should not panic, so we ignore a failed flush
668672
let _r = self.flush_buf();
669673
}

0 commit comments

Comments
 (0)