Skip to content

Commit 55e4827

Browse files
taiki-ecramertj
authored andcommitted
Add check for object safety
1 parent 621d491 commit 55e4827

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

futures/tests/object_safety.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
fn assert_is_object_safe<T>() {}
2+
3+
#[test]
4+
fn future() {
5+
// `FutureExt`, `TryFutureExt` and `UnsafeFutureObj` are not object safe.
6+
use futures::future::{FusedFuture, Future, TryFuture};
7+
8+
assert_is_object_safe::<&dyn Future<Output = ()>>();
9+
assert_is_object_safe::<&dyn TryFuture<Ok = (), Error = ()>>();
10+
assert_is_object_safe::<&dyn FusedFuture>();
11+
}
12+
13+
#[test]
14+
fn stream() {
15+
// `StreamExt` and `StreamExt` are not object safe.
16+
use futures::stream::{FusedStream, Stream, TryStream};
17+
18+
assert_is_object_safe::<&dyn Stream<Item = ()>>();
19+
assert_is_object_safe::<&dyn TryStream<Ok = (), Error = ()>>();
20+
assert_is_object_safe::<&dyn FusedStream>();
21+
}
22+
23+
#[test]
24+
fn sink() {
25+
// `SinkExt` is not object safe.
26+
use futures::sink::Sink;
27+
28+
assert_is_object_safe::<&dyn Sink<(), Error = ()>>();
29+
}
30+
31+
#[test]
32+
fn io() {
33+
// `AsyncReadExt`, `AsyncWriteExt`, `AsyncSeekExt` and `AsyncBufReadExt` are not object safe.
34+
use futures::io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite};
35+
36+
assert_is_object_safe::<&dyn AsyncRead>();
37+
assert_is_object_safe::<&dyn AsyncWrite>();
38+
assert_is_object_safe::<&dyn AsyncSeek>();
39+
assert_is_object_safe::<&dyn AsyncBufRead>();
40+
}
41+
42+
#[test]
43+
fn task() {
44+
// `ArcWake`, `SpawnExt` and `LocalSpawnExt` are not object safe.
45+
use futures::task::{LocalSpawn, Spawn};
46+
47+
assert_is_object_safe::<&dyn Spawn>();
48+
assert_is_object_safe::<&dyn LocalSpawn>();
49+
}

0 commit comments

Comments
 (0)