Repository: tokio-rs/tokio
File: tokio/src/io/stdout.rs (Rust)

Code before:
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let messages = vec!["hello", " world\n"];
    ///
    ///     // When you use `stdio` in a loop, it is recommended to create
    ///     // a single `stdio` instance outside the loop and call a write
    ///     // operation against that instance on each loop.
    ///     //
    ///     // Repeatedly creating `stdout` instances inside the loop and
    ///     // writing to that handle could result in mangled output since
    ///     // each write operation is handled by a different blocking thread.
    ///     let mut stdout = io::stdout();
    ///
    ///     for message in &messages {
    ///         stdout.write_all(message.as_bytes()).await.unwrap();
    ///         stdout.flush().await.unwrap();
    ///     }
    /// }
    /// ```
    pub fn stdout() -> Stdout {
        let std = io::stdout();
        // SAFETY: The `Read` implementation of `std` does not read from the
        // buffer it is borrowing and correctly reports the length of the data
        // written into the buffer.
        let blocking = unsafe {Blocking::new(std)};
        Stdout {
            std: SplitByUtf8BoundaryIfWindows::new(blocking),
        }
    }
}

#[cfg(unix)]
mod sys {
    use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};

    use super::Stdout;

    impl AsRawFd for Stdout {
        fn as_raw_fd(&self) -> RawFd {
            std::io::stdout().as_raw_fd()
        }
    }

    impl AsFd for Stdout {
        fn as_fd(&self) -> BorrowedFd<'_> {
            unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
        }
    }
}


Review comment (suggestion):
```suggestion
        let blocking = unsafe { Blocking::new(std) };
```

Code after:
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let messages = vec!["hello", " world\n"];
    ///
    ///     // When you use `stdio` in a loop, it is recommended to create
    ///     // a single `stdio` instance outside the loop and call a write
    ///     // operation against that instance on each loop.
    ///     //
    ///     // Repeatedly creating `stdout` instances inside the loop and
    ///     // writing to that handle could result in mangled output since
    ///     // each write operation is handled by a different blocking thread.
    ///     let mut stdout = io::stdout();
    ///
    ///     for message in &messages {
    ///         stdout.write_all(message.as_bytes()).await.unwrap();
    ///         stdout.flush().await.unwrap();
    ///     }
    /// }
    /// ```
    pub fn stdout() -> Stdout {
        let std = io::stdout();
        // SAFETY: The `Read` implementation of `std` does not read from the
        // buffer it is borrowing and correctly reports the length of the data
        // written into the buffer.
        let blocking = unsafe { Blocking::new(std) };
        Stdout {
            std: SplitByUtf8BoundaryIfWindows::new(blocking),
        }
    }
}

#[cfg(unix)]
mod sys {
    use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};

    use super::Stdout;

    impl AsRawFd for Stdout {
        fn as_raw_fd(&self) -> RawFd {
            std::io::stdout().as_raw_fd()
        }
    }

    impl AsFd for Stdout {
        fn as_fd(&self) -> BorrowedFd<'_> {
            unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
        }
    }
}