pub unsafe trait IoSafe { }Expand description
Types whose I/O trait implementations do not move or drop the underlying I/O object
The I/O object inside Async cannot be closed before the Async is dropped, because
[Async] deregisters the I/O object from the reactor on drop. Closing the I/O before
deregistering leads to the reactor holding a dangling I/O handle, which violates I/O safety.
As such, functions that grant mutable access to the inner I/O object are unsafe, because they
may move or drop the underlying I/O. Unfortunately, Async needs to call I/O traits such as
Read and Write to implement the async version of those traits.
To signal that those traits are safe to implement for an I/O type, it must implement
IoSafe, which acts as a promise that the I/O type doesn’t move or drop itself in its I/O
trait implementations.
This trait is implemented for std I/O types.
§Safety
Implementors of IoSafe must not drop or move its underlying I/O source in its
implementations of Read, Write, and BufRead. Specifically, the “underlying I/O
source” is defined as the I/O primitive corresponding to the type’s AsFd/AsSocket
implementation.
Implementations on Foreign Types§
impl IoSafe for File
impl IoSafe for Stderr
impl IoSafe for StderrLock<'_>
impl IoSafe for Stdin
impl IoSafe for StdinLock<'_>
impl IoSafe for Stdout
impl IoSafe for StdoutLock<'_>
impl IoSafe for TcpStream
impl IoSafe for UdpSocket
impl IoSafe for UnixStream
impl IoSafe for ChildStderr
impl IoSafe for ChildStdin
impl IoSafe for ChildStdout
impl<T: IoSafe + Write> IoSafe for BufWriter<T>
impl<T: IoSafe + Write> IoSafe for LineWriter<T>
impl<T: IoSafe + ?Sized> IoSafe for &T
IoSafe cannot be unconditionally implemented for references, because non-mutable references
can still drop or move internal fields via interior mutability.