|
2 | 2 |
|
3 | 3 | use crate::fmt;
|
4 | 4 | use crate::io::{self, Read, Initializer, Write, ErrorKind, BufRead, IoSlice, IoSliceMut};
|
5 |
| -use crate::mem; |
| 5 | +use crate::mem::MaybeUninit; |
6 | 6 |
|
7 | 7 | /// Copies the entire contents of a reader into a writer.
|
8 | 8 | ///
|
@@ -43,27 +43,23 @@ use crate::mem;
|
43 | 43 | pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
|
44 | 44 | where R: Read, W: Write
|
45 | 45 | {
|
46 |
| - let mut buf = unsafe { |
47 |
| - // This is still technically undefined behavior due to creating a reference |
48 |
| - // to uninitialized data, but within libstd we can rely on more guarantees |
49 |
| - // than if this code were in an external lib |
50 |
| - |
51 |
| - // FIXME: This should probably be changed to an array of `MaybeUninit<u8>` |
52 |
| - // once the `mem::MaybeUninit` slice APIs stabilize |
53 |
| - let mut buf: mem::MaybeUninit<[u8; super::DEFAULT_BUF_SIZE]> = mem::MaybeUninit::uninit(); |
54 |
| - reader.initializer().initialize(&mut *buf.as_mut_ptr()); |
55 |
| - buf.assume_init() |
56 |
| - }; |
| 46 | + let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit(); |
| 47 | + // FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized |
| 48 | + // `MaybeUninit`. Revisit this once we decided whether that is valid or not. |
| 49 | + // This is still technically undefined behavior due to creating a reference |
| 50 | + // to uninitialized data, but within libstd we can rely on more guarantees |
| 51 | + // than if this code were in an external lib. |
| 52 | + unsafe { reader.initializer().initialize(buf.get_mut()); } |
57 | 53 |
|
58 | 54 | let mut written = 0;
|
59 | 55 | loop {
|
60 |
| - let len = match reader.read(&mut buf) { |
| 56 | + let len = match reader.read(unsafe { buf.get_mut() }) { |
61 | 57 | Ok(0) => return Ok(written),
|
62 | 58 | Ok(len) => len,
|
63 | 59 | Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
64 | 60 | Err(e) => return Err(e),
|
65 | 61 | };
|
66 |
| - writer.write_all(&buf[..len])?; |
| 62 | + writer.write_all(unsafe { &buf.get_ref()[..len] })?; |
67 | 63 | written += len as u64;
|
68 | 64 | }
|
69 | 65 | }
|
|
0 commit comments