1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3 
4 use std::cell::Cell;
5 use std::io::Cursor;
6 use std::net::SocketAddr;
7 use std::rc::Rc;
8 use tokio::net::TcpStream;
9 use tokio::time::{Duration, Instant};
10 
11 #[allow(dead_code)]
12 type BoxFutureSync<T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + Sync>>;
13 #[allow(dead_code)]
14 type BoxFutureSend<T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send>>;
15 #[allow(dead_code)]
16 type BoxFuture<T> = std::pin::Pin<Box<dyn std::future::Future<Output = T>>>;
17 
18 #[allow(dead_code)]
require_send<T: Send>(_t: &T)19 fn require_send<T: Send>(_t: &T) {}
20 #[allow(dead_code)]
require_sync<T: Sync>(_t: &T)21 fn require_sync<T: Sync>(_t: &T) {}
22 
23 #[allow(dead_code)]
24 struct Invalid;
25 
26 trait AmbiguousIfSend<A> {
some_item(&self)27     fn some_item(&self) {}
28 }
29 impl<T: ?Sized> AmbiguousIfSend<()> for T {}
30 impl<T: ?Sized + Send> AmbiguousIfSend<Invalid> for T {}
31 
32 trait AmbiguousIfSync<A> {
some_item(&self)33     fn some_item(&self) {}
34 }
35 impl<T: ?Sized> AmbiguousIfSync<()> for T {}
36 impl<T: ?Sized + Sync> AmbiguousIfSync<Invalid> for T {}
37 
38 macro_rules! into_todo {
39     ($typ:ty) => {{
40         let x: $typ = todo!();
41         x
42     }};
43 }
44 macro_rules! assert_value {
45     ($type:ty: Send & Sync) => {
46         #[allow(unreachable_code)]
47         #[allow(unused_variables)]
48         const _: fn() = || {
49             let f: $type = todo!();
50             require_send(&f);
51             require_sync(&f);
52         };
53     };
54     ($type:ty: !Send & Sync) => {
55         #[allow(unreachable_code)]
56         #[allow(unused_variables)]
57         const _: fn() = || {
58             let f: $type = todo!();
59             AmbiguousIfSend::some_item(&f);
60             require_sync(&f);
61         };
62     };
63     ($type:ty: Send & !Sync) => {
64         #[allow(unreachable_code)]
65         #[allow(unused_variables)]
66         const _: fn() = || {
67             let f: $type = todo!();
68             require_send(&f);
69             AmbiguousIfSync::some_item(&f);
70         };
71     };
72     ($type:ty: !Send & !Sync) => {
73         #[allow(unreachable_code)]
74         #[allow(unused_variables)]
75         const _: fn() = || {
76             let f: $type = todo!();
77             AmbiguousIfSend::some_item(&f);
78             AmbiguousIfSync::some_item(&f);
79         };
80     };
81 }
82 macro_rules! async_assert_fn {
83     ($($f:ident $(< $($generic:ty),* > )? )::+($($arg:ty),*): Send & Sync) => {
84         #[allow(unreachable_code)]
85         #[allow(unused_variables)]
86         const _: fn() = || {
87             let f = $($f $(::<$($generic),*>)? )::+( $( into_todo!($arg) ),* );
88             require_send(&f);
89             require_sync(&f);
90         };
91     };
92     ($($f:ident $(< $($generic:ty),* > )? )::+($($arg:ty),*): Send & !Sync) => {
93         #[allow(unreachable_code)]
94         #[allow(unused_variables)]
95         const _: fn() = || {
96             let f = $($f $(::<$($generic),*>)? )::+( $( into_todo!($arg) ),* );
97             require_send(&f);
98             AmbiguousIfSync::some_item(&f);
99         };
100     };
101     ($($f:ident $(< $($generic:ty),* > )? )::+($($arg:ty),*): !Send & Sync) => {
102         #[allow(unreachable_code)]
103         #[allow(unused_variables)]
104         const _: fn() = || {
105             let f = $($f $(::<$($generic),*>)? )::+( $( into_todo!($arg) ),* );
106             AmbiguousIfSend::some_item(&f);
107             require_sync(&f);
108         };
109     };
110     ($($f:ident $(< $($generic:ty),* > )? )::+($($arg:ty),*): !Send & !Sync) => {
111         #[allow(unreachable_code)]
112         #[allow(unused_variables)]
113         const _: fn() = || {
114             let f = $($f $(::<$($generic),*>)? )::+( $( into_todo!($arg) ),* );
115             AmbiguousIfSend::some_item(&f);
116             AmbiguousIfSync::some_item(&f);
117         };
118     };
119 }
120 
121 async_assert_fn!(tokio::io::copy(&mut TcpStream, &mut TcpStream): Send & Sync);
122 async_assert_fn!(tokio::io::empty(): Send & Sync);
123 async_assert_fn!(tokio::io::repeat(u8): Send & Sync);
124 async_assert_fn!(tokio::io::sink(): Send & Sync);
125 async_assert_fn!(tokio::io::split(TcpStream): Send & Sync);
126 async_assert_fn!(tokio::io::stderr(): Send & Sync);
127 async_assert_fn!(tokio::io::stdin(): Send & Sync);
128 async_assert_fn!(tokio::io::stdout(): Send & Sync);
129 async_assert_fn!(tokio::io::Split<Cursor<Vec<u8>>>::next_segment(_): Send & Sync);
130 
131 async_assert_fn!(tokio::fs::canonicalize(&str): Send & Sync);
132 async_assert_fn!(tokio::fs::copy(&str, &str): Send & Sync);
133 async_assert_fn!(tokio::fs::create_dir(&str): Send & Sync);
134 async_assert_fn!(tokio::fs::create_dir_all(&str): Send & Sync);
135 async_assert_fn!(tokio::fs::hard_link(&str, &str): Send & Sync);
136 async_assert_fn!(tokio::fs::metadata(&str): Send & Sync);
137 async_assert_fn!(tokio::fs::read(&str): Send & Sync);
138 async_assert_fn!(tokio::fs::read_dir(&str): Send & Sync);
139 async_assert_fn!(tokio::fs::read_link(&str): Send & Sync);
140 async_assert_fn!(tokio::fs::read_to_string(&str): Send & Sync);
141 async_assert_fn!(tokio::fs::remove_dir(&str): Send & Sync);
142 async_assert_fn!(tokio::fs::remove_dir_all(&str): Send & Sync);
143 async_assert_fn!(tokio::fs::remove_file(&str): Send & Sync);
144 async_assert_fn!(tokio::fs::rename(&str, &str): Send & Sync);
145 async_assert_fn!(tokio::fs::set_permissions(&str, std::fs::Permissions): Send & Sync);
146 async_assert_fn!(tokio::fs::symlink_metadata(&str): Send & Sync);
147 async_assert_fn!(tokio::fs::write(&str, Vec<u8>): Send & Sync);
148 async_assert_fn!(tokio::fs::ReadDir::next_entry(_): Send & Sync);
149 async_assert_fn!(tokio::fs::OpenOptions::open(_, &str): Send & Sync);
150 async_assert_fn!(tokio::fs::DirEntry::metadata(_): Send & Sync);
151 async_assert_fn!(tokio::fs::DirEntry::file_type(_): Send & Sync);
152 
153 async_assert_fn!(tokio::fs::File::open(&str): Send & Sync);
154 async_assert_fn!(tokio::fs::File::create(&str): Send & Sync);
155 async_assert_fn!(tokio::fs::File::seek(_, std::io::SeekFrom): Send & Sync);
156 async_assert_fn!(tokio::fs::File::sync_all(_): Send & Sync);
157 async_assert_fn!(tokio::fs::File::sync_data(_): Send & Sync);
158 async_assert_fn!(tokio::fs::File::set_len(_, u64): Send & Sync);
159 async_assert_fn!(tokio::fs::File::metadata(_): Send & Sync);
160 async_assert_fn!(tokio::fs::File::try_clone(_): Send & Sync);
161 async_assert_fn!(tokio::fs::File::into_std(_): Send & Sync);
162 async_assert_fn!(tokio::fs::File::set_permissions(_, std::fs::Permissions): Send & Sync);
163 
164 async_assert_fn!(tokio::net::lookup_host(SocketAddr): Send & Sync);
165 async_assert_fn!(tokio::net::TcpListener::bind(SocketAddr): Send & Sync);
166 async_assert_fn!(tokio::net::TcpListener::accept(_): Send & Sync);
167 async_assert_fn!(tokio::net::TcpStream::connect(SocketAddr): Send & Sync);
168 async_assert_fn!(tokio::net::TcpStream::peek(_, &mut [u8]): Send & Sync);
169 async_assert_fn!(tokio::net::tcp::ReadHalf::peek(_, &mut [u8]): Send & Sync);
170 async_assert_fn!(tokio::net::UdpSocket::bind(SocketAddr): Send & Sync);
171 async_assert_fn!(tokio::net::UdpSocket::connect(_, SocketAddr): Send & Sync);
172 async_assert_fn!(tokio::net::UdpSocket::send(_, &[u8]): Send & Sync);
173 async_assert_fn!(tokio::net::UdpSocket::recv(_, &mut [u8]): Send & Sync);
174 async_assert_fn!(tokio::net::UdpSocket::send_to(_, &[u8], SocketAddr): Send & Sync);
175 async_assert_fn!(tokio::net::UdpSocket::recv_from(_, &mut [u8]): Send & Sync);
176 async_assert_fn!(tokio::net::udp::RecvHalf::recv(_, &mut [u8]): Send & Sync);
177 async_assert_fn!(tokio::net::udp::RecvHalf::recv_from(_, &mut [u8]): Send & Sync);
178 async_assert_fn!(tokio::net::udp::SendHalf::send(_, &[u8]): Send & Sync);
179 async_assert_fn!(tokio::net::udp::SendHalf::send_to(_, &[u8], &SocketAddr): Send & Sync);
180 
181 #[cfg(unix)]
182 mod unix_datagram {
183     use super::*;
184     async_assert_fn!(tokio::net::UnixListener::bind(&str): Send & Sync);
185     async_assert_fn!(tokio::net::UnixListener::accept(_): Send & Sync);
186     async_assert_fn!(tokio::net::UnixDatagram::send(_, &[u8]): Send & Sync);
187     async_assert_fn!(tokio::net::UnixDatagram::recv(_, &mut [u8]): Send & Sync);
188     async_assert_fn!(tokio::net::UnixDatagram::send_to(_, &[u8], &str): Send & Sync);
189     async_assert_fn!(tokio::net::UnixDatagram::recv_from(_, &mut [u8]): Send & Sync);
190     async_assert_fn!(tokio::net::UnixStream::connect(&str): Send & Sync);
191 }
192 
193 async_assert_fn!(tokio::process::Child::wait_with_output(_): Send & Sync);
194 async_assert_fn!(tokio::signal::ctrl_c(): Send & Sync);
195 #[cfg(unix)]
196 async_assert_fn!(tokio::signal::unix::Signal::recv(_): Send & Sync);
197 
198 async_assert_fn!(tokio::stream::empty<Rc<u8>>(): Send & Sync);
199 async_assert_fn!(tokio::stream::pending<Rc<u8>>(): Send & Sync);
200 async_assert_fn!(tokio::stream::iter(std::vec::IntoIter<u8>): Send & Sync);
201 
202 async_assert_fn!(tokio::sync::Barrier::wait(_): Send & Sync);
203 async_assert_fn!(tokio::sync::Mutex<u8>::lock(_): Send & Sync);
204 async_assert_fn!(tokio::sync::Mutex<Cell<u8>>::lock(_): Send & Sync);
205 async_assert_fn!(tokio::sync::Mutex<Rc<u8>>::lock(_): !Send & !Sync);
206 async_assert_fn!(tokio::sync::Mutex<u8>::lock_owned(_): Send & Sync);
207 async_assert_fn!(tokio::sync::Mutex<Cell<u8>>::lock_owned(_): Send & Sync);
208 async_assert_fn!(tokio::sync::Mutex<Rc<u8>>::lock_owned(_): !Send & !Sync);
209 async_assert_fn!(tokio::sync::Notify::notified(_): Send & !Sync);
210 async_assert_fn!(tokio::sync::RwLock<u8>::read(_): Send & Sync);
211 async_assert_fn!(tokio::sync::RwLock<u8>::write(_): Send & Sync);
212 async_assert_fn!(tokio::sync::RwLock<Cell<u8>>::read(_): !Send & !Sync);
213 async_assert_fn!(tokio::sync::RwLock<Cell<u8>>::write(_): !Send & !Sync);
214 async_assert_fn!(tokio::sync::RwLock<Rc<u8>>::read(_): !Send & !Sync);
215 async_assert_fn!(tokio::sync::RwLock<Rc<u8>>::write(_): !Send & !Sync);
216 async_assert_fn!(tokio::sync::Semaphore::acquire(_): Send & Sync);
217 
218 async_assert_fn!(tokio::sync::broadcast::Receiver<u8>::recv(_): Send & Sync);
219 async_assert_fn!(tokio::sync::broadcast::Receiver<Cell<u8>>::recv(_): Send & Sync);
220 async_assert_fn!(tokio::sync::broadcast::Receiver<Rc<u8>>::recv(_): !Send & !Sync);
221 
222 async_assert_fn!(tokio::sync::mpsc::Receiver<u8>::recv(_): Send & Sync);
223 async_assert_fn!(tokio::sync::mpsc::Receiver<Cell<u8>>::recv(_): Send & Sync);
224 async_assert_fn!(tokio::sync::mpsc::Receiver<Rc<u8>>::recv(_): !Send & !Sync);
225 async_assert_fn!(tokio::sync::mpsc::Sender<u8>::send(_, u8): Send & Sync);
226 async_assert_fn!(tokio::sync::mpsc::Sender<Cell<u8>>::send(_, Cell<u8>): Send & !Sync);
227 async_assert_fn!(tokio::sync::mpsc::Sender<Rc<u8>>::send(_, Rc<u8>): !Send & !Sync);
228 
229 async_assert_fn!(tokio::sync::mpsc::UnboundedReceiver<u8>::recv(_): Send & Sync);
230 async_assert_fn!(tokio::sync::mpsc::UnboundedReceiver<Cell<u8>>::recv(_): Send & Sync);
231 async_assert_fn!(tokio::sync::mpsc::UnboundedReceiver<Rc<u8>>::recv(_): !Send & !Sync);
232 
233 async_assert_fn!(tokio::sync::watch::Receiver<u8>::recv(_): Send & Sync);
234 async_assert_fn!(tokio::sync::watch::Receiver<Cell<u8>>::recv(_): !Send & !Sync);
235 async_assert_fn!(tokio::sync::watch::Receiver<Rc<u8>>::recv(_): !Send & !Sync);
236 async_assert_fn!(tokio::sync::watch::Sender<u8>::closed(_): Send & Sync);
237 async_assert_fn!(tokio::sync::watch::Sender<Cell<u8>>::closed(_): !Send & !Sync);
238 async_assert_fn!(tokio::sync::watch::Sender<Rc<u8>>::closed(_): !Send & !Sync);
239 
240 async_assert_fn!(tokio::task::LocalKey<u32>::scope(_, u32, BoxFutureSync<()>): Send & Sync);
241 async_assert_fn!(tokio::task::LocalKey<u32>::scope(_, u32, BoxFutureSend<()>): Send & !Sync);
242 async_assert_fn!(tokio::task::LocalKey<u32>::scope(_, u32, BoxFuture<()>): !Send & !Sync);
243 async_assert_fn!(tokio::task::LocalKey<Cell<u32>>::scope(_, Cell<u32>, BoxFutureSync<()>): Send & !Sync);
244 async_assert_fn!(tokio::task::LocalKey<Cell<u32>>::scope(_, Cell<u32>, BoxFutureSend<()>): Send & !Sync);
245 async_assert_fn!(tokio::task::LocalKey<Cell<u32>>::scope(_, Cell<u32>, BoxFuture<()>): !Send & !Sync);
246 async_assert_fn!(tokio::task::LocalKey<Rc<u32>>::scope(_, Rc<u32>, BoxFutureSync<()>): !Send & !Sync);
247 async_assert_fn!(tokio::task::LocalKey<Rc<u32>>::scope(_, Rc<u32>, BoxFutureSend<()>): !Send & !Sync);
248 async_assert_fn!(tokio::task::LocalKey<Rc<u32>>::scope(_, Rc<u32>, BoxFuture<()>): !Send & !Sync);
249 async_assert_fn!(tokio::task::LocalSet::run_until(_, BoxFutureSync<()>): !Send & !Sync);
250 assert_value!(tokio::task::LocalSet: !Send & !Sync);
251 
252 async_assert_fn!(tokio::time::advance(Duration): Send & Sync);
253 async_assert_fn!(tokio::time::delay_for(Duration): Send & Sync);
254 async_assert_fn!(tokio::time::delay_until(Instant): Send & Sync);
255 async_assert_fn!(tokio::time::timeout(Duration, BoxFutureSync<()>): Send & Sync);
256 async_assert_fn!(tokio::time::timeout(Duration, BoxFutureSend<()>): Send & !Sync);
257 async_assert_fn!(tokio::time::timeout(Duration, BoxFuture<()>): !Send & !Sync);
258 async_assert_fn!(tokio::time::timeout_at(Instant, BoxFutureSync<()>): Send & Sync);
259 async_assert_fn!(tokio::time::timeout_at(Instant, BoxFutureSend<()>): Send & !Sync);
260 async_assert_fn!(tokio::time::timeout_at(Instant, BoxFuture<()>): !Send & !Sync);
261 async_assert_fn!(tokio::time::Interval::tick(_): Send & Sync);
262 
263 #[cfg(tokio_unstable)]
264 assert_value!(tokio::sync::CancellationToken: Send & Sync);
265