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

Remove Option<W> in BufWriter #72925

Closed
Lucretiel opened this issue Jun 2, 2020 · 2 comments · Fixed by #87171
Closed

Remove Option<W> in BufWriter #72925

Lucretiel opened this issue Jun 2, 2020 · 2 comments · Fixed by #87171
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@Lucretiel
Copy link
Contributor

Lucretiel commented Jun 2, 2020

inner: Option<W>,

BufWriter doesn't really need to hold its inner writer as an Option; it seems like the only reason it does so is to make it easier to call into_inner, here:

pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
match self.flush_buf() {
Err(e) => Err(IntoInnerError(self, e)),
Ok(()) => Ok(self.inner.take().unwrap()),
}
}

While it's unfortunate to add unsafe, there's already some in buffered.rs, and it seems like the better choice here would be to use ptr::read and mem::forget, especially because this method is probably infrequently used, and making this change would remove the numerous unwraps that litter the BufWriter implementation.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 2, 2020
@Lucretiel
Copy link
Contributor Author

Just tried to implement this; it turns out to be nontrivial, because you need to mem::forget(self) in a way that doesn't leak self.buf.

@Amanieu
Copy link
Member

Amanieu commented Jul 11, 2020

You can use ptr::drop_in_place on self.buf before forget(self).

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jul 23, 2021
@bors bors closed this as completed in 8837bf1 Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants