| 1 |
#[cfg(feature = "nightly")] |
| 2 |
use core::mem::MaybeUninit; |
| 3 |
|
| 4 |
#[cfg(feature = "nightly")] |
| 5 |
use crate::io::{ErrorKind, Read, Write}; |
| 6 |
|
| 7 |
#[cfg(feature = "nightly")] |
| 8 |
pub fn copy<R: ?Sized, W: ?Sized, const S: usize>( |
| 9 |
reader: &mut R, |
| 10 |
writer: &mut W, |
| 11 |
) -> crate::io::Result<u64> |
| 12 |
where |
| 13 |
R: Read, |
| 14 |
W: Write, |
| 15 |
{ |
| 16 |
let mut buf = MaybeUninit::<[u8; S]>::uninit(); |
| 17 |
// FIXME: #42788 |
| 18 |
// |
| 19 |
// - This creates a (mut) reference to a slice of |
| 20 |
// _uninitialized_ integers, which is **undefined behavior** |
| 21 |
// |
| 22 |
// - Only the standard library gets to soundly "ignore" this, |
| 23 |
// based on its privileged knowledge of unstable rustc |
| 24 |
// internals; |
| 25 |
unsafe { |
| 26 |
reader.initializer().initialize(buf.assume_init_mut()); |
| 27 |
} |
| 28 |
|
| 29 |
let mut written = 0; |
| 30 |
loop { |
| 31 |
let len = match reader.read(unsafe { buf.assume_init_mut() }) { |
| 32 |
Ok(0) => return Ok(written), |
| 33 |
Ok(len) => len, |
| 34 |
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, |
| 35 |
Err(e) => return Err(e), |
| 36 |
}; |
| 37 |
writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?; |
| 38 |
written += len as u64; |
| 39 |
} |
| 40 |
} |