uninit_read/
impls.rs

1//! This module provides `UninitRead` implementations for common `Read` and `AsyncRead` types.
2
3use crate::UninitRead;
4use std::ops::{Deref, DerefMut};
5
6unsafe impl<T: ?Sized + UninitRead + Unpin> UninitRead for Box<T> {}
7unsafe impl<T: ?Sized + UninitRead + Unpin> UninitRead for &mut T {}
8unsafe impl<T: UninitRead + DerefMut + Unpin> UninitRead for std::pin::Pin<T> where
9    <T as Deref>::Target: UninitRead
10{
11}
12
13pub mod std_io {
14    use crate::UninitRead;
15    use std::fs::File;
16    use std::io::{Cursor, Empty, Read, Stdin};
17    use std::net::TcpStream;
18    unsafe impl UninitRead for File {}
19    unsafe impl UninitRead for Stdin {}
20    unsafe impl UninitRead for TcpStream {}
21    unsafe impl UninitRead for Empty {}
22    unsafe impl<T> UninitRead for Cursor<T> where Cursor<T>: Read {}
23
24    //todo: more std impls
25
26    #[cfg(unix)]
27    pub mod unix {
28        use crate::UninitRead;
29        use std::os::unix::net::UnixStream;
30
31        unsafe impl UninitRead for UnixStream {}
32    }
33}
34
35#[cfg(feature = "async")]
36pub mod async_impls {
37    use crate::UninitRead;
38
39    unsafe impl UninitRead for &[u8] {}
40}
41
42/// Implementations for `futures-lite` I/O types.
43#[cfg(feature = "futures-lite")]
44pub mod futures_lite {
45    use crate::UninitRead;
46    use futures_io::AsyncRead;
47    use futures_lite::io::{BufReader, Bytes, Chain, Cursor, Empty, ReadHalf, Take};
48
49    // Blanket implementations for common futures-lite I/O adapters.
50    // Safety: These types delegate their `AsyncRead` implementation to an inner type,
51    // so if the inner type is `UninitRead`, the wrapper is also `UninitRead`.
52
53    unsafe impl<R> UninitRead for BufReader<R> where R: UninitRead {}
54
55    unsafe impl<R> UninitRead for Bytes<R> where R: UninitRead {}
56
57    unsafe impl<T> UninitRead for Cursor<T> where Cursor<T>: AsyncRead {}
58
59    unsafe impl<R1, R2> UninitRead for Chain<R1, R2>
60    where
61        R1: UninitRead,
62        R2: UninitRead,
63    {
64    }
65
66    unsafe impl UninitRead for Empty {}
67
68    unsafe impl<T: Unpin> UninitRead for ReadHalf<T> where T: UninitRead {}
69
70    unsafe impl<R> UninitRead for Take<R> where R: UninitRead {}
71}
72
73/// Implementations for `async-fs` I/O types.
74#[cfg(feature = "async-fs")]
75pub mod async_fs {
76    use crate::UninitRead;
77    use async_fs::File;
78
79    /// Safety: `async_fs::File` is known to not read from the buffer before writing.
80    unsafe impl UninitRead for File {}
81}
82
83/// Implementations for `async-net` I/O types.
84#[cfg(feature = "async-net")]
85pub mod async_net {
86    use crate::UninitRead;
87    use async_net::TcpStream;
88
89    /// Safety: `async_net::TcpStream` is known to not read from the buffer before writing.
90    unsafe impl UninitRead for TcpStream {}
91
92    #[cfg(unix)]
93    pub mod unix {
94        use crate::UninitRead;
95        use async_net::unix::UnixStream;
96
97        /// Safety: `async_net::unix::UnixStream` is known to not read from the buffer before writing.
98        unsafe impl UninitRead for UnixStream {}
99    }
100}
101
102/// Implementations for `tokio` compatibility types.
103#[cfg(feature = "tokio")]
104pub mod tokio {
105    use crate::UninitRead;
106    use tokio_util::compat::Compat;
107
108    /// Safety: `tokio_util::compat::Compat<T>` delegates to an inner `tokio::io::AsyncRead`
109    /// which is known to correctly handle uninitialized buffers via `ReadBuf`.
110    unsafe impl<T: tokio::io::AsyncRead> UninitRead for Compat<T> where Compat<T>: futures_io::AsyncRead {}
111}