Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3033120

Browse files
Rollup merge ofrust-lang#127659 - saethlin:manually-drop-bufwriter, r=joboet
Use ManuallyDrop in BufWriter::into_partsThe fact that `mem::forget` takes by value means that it interacts very poorly with Stacked Borrows; generally users think of calling it as a no-op, but in Stacked Borrows, the field retagging tends to cause surprise tag invalidation.
2 parents6b67c66 +c8b79dd commit3033120

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::fmt;
33
usecrate::io::{
44
self,ErrorKind,IntoInnerError,IoSlice,Seek,SeekFrom,Write,DEFAULT_BUF_SIZE,
55
};
6-
usecrate::mem;
6+
usecrate::mem::{self,ManuallyDrop};
77
usecrate::ptr;
88

99
/// Wraps a writer and buffers its output.
@@ -164,13 +164,13 @@ impl<W: Write> BufWriter<W> {
164164
/// assert_eq!(&buffered_data.unwrap(), b"ata");
165165
/// ```
166166
#[stable(feature ="bufwriter_into_parts", since ="1.56.0")]
167-
pubfninto_parts(mutself) ->(W,Result<Vec<u8>,WriterPanicked>){
168-
let buf = mem::take(&mutself.buf);
169-
let buf =if !self.panicked{Ok(buf)}else{Err(WriterPanicked{ buf})};
167+
pubfninto_parts(self) ->(W,Result<Vec<u8>,WriterPanicked>){
168+
letmut this =ManuallyDrop::new(self);
169+
let buf = mem::take(&mut this.buf);
170+
let buf =if !this.panicked{Ok(buf)}else{Err(WriterPanicked{ buf})};
170171

171-
// SAFETY: forget(self) prevents double dropping inner
172-
let inner =unsafe{ ptr::read(&self.inner)};
173-
mem::forget(self);
172+
// SAFETY: double-drops are prevented by putting `this` in a ManuallyDrop that is never dropped
173+
let inner =unsafe{ ptr::read(&this.inner)};
174174

175175
(inner, buf)
176176
}

‎std/src/io/buffered/tests.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,3 +1067,13 @@ fn bufreader_full_initialize() {
10671067
// But we initialized the whole buffer!
10681068
assert_eq!(reader.initialized(), reader.capacity());
10691069
}
1070+
1071+
/// This is a regression test for https://github.com/rust-lang/rust/issues/127584.
1072+
#[test]
1073+
fnbufwriter_aliasing(){
1074+
usecrate::io::{BufWriter,Cursor};
1075+
letmut v =vec![0;1024];
1076+
let c =Cursor::new(&mut v);
1077+
let w =BufWriter::new(Box::new(c));
1078+
let _ = w.into_parts();
1079+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp