Skip to content

Commit 308786c

Browse files
authored
Merge pull request #499 from rkarp/trait-asserts
Make trait assertions nicer
2 parents b56f325 + 24fadda commit 308786c

File tree

6 files changed

+13
-35
lines changed

6 files changed

+13
-35
lines changed

futures-cpupool/src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ struct MySender<F, T> {
8989
keep_running_flag: Arc<AtomicBool>,
9090
}
9191

92-
fn _assert() {
93-
fn _assert_send<T: Send>() {}
94-
fn _assert_sync<T: Sync>() {}
95-
_assert_send::<CpuPool>();
96-
_assert_sync::<CpuPool>();
97-
}
92+
trait AssertSendSync: Send + Sync {}
93+
impl AssertSendSync for CpuPool {}
9894

9995
struct Inner {
10096
tx: Mutex<mpsc::Sender<Message>>,

src/sync/mpsc/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,8 @@ pub struct Sender<T> {
106106
#[derive(Debug)]
107107
pub struct UnboundedSender<T>(Sender<T>);
108108

109-
fn _assert_kinds() {
110-
fn _assert_send<T: Send>() {}
111-
fn _assert_sync<T: Sync>() {}
112-
fn _assert_clone<T: Clone>() {}
113-
_assert_send::<UnboundedSender<u32>>();
114-
_assert_sync::<UnboundedSender<u32>>();
115-
_assert_clone::<UnboundedSender<u32>>();
116-
}
109+
trait AssertKinds: Send + Sync + Clone {}
110+
impl AssertKinds for UnboundedSender<u32> {}
117111

118112

119113
/// The receiving end of a channel which implements the `Stream` trait.

src/task_impl/atomic_task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl AtomicTask {
4343
/// Create an `AtomicTask` initialized with the given `Task`
4444
pub fn new() -> AtomicTask {
4545
// Make sure that task is Sync
46-
fn is_sync<T: Sync>() {}
47-
is_sync::<Task>();
46+
trait AssertSync: Sync {}
47+
impl AssertSync for Task {}
4848

4949
AtomicTask {
5050
state: AtomicUsize::new(WAITING),

src/task_impl/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ pub struct Task {
6161
events: UnparkEvents,
6262
}
6363

64-
fn _assert_kinds() {
65-
fn _assert_send<T: Send>() {}
66-
_assert_send::<Task>();
67-
}
64+
trait AssertSend: Send {}
65+
impl AssertSend for Task {}
6866

6967
/// Returns a handle to the current task to call `notify` at a later date.
7068
///

tests/mpsc.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ use std::thread;
1111
use std::sync::{Arc, Mutex};
1212
use std::sync::atomic::{AtomicUsize, Ordering};
1313

14-
fn is_send<T: Send>() {}
15-
16-
#[test]
17-
fn bounds() {
18-
is_send::<mpsc::Sender<i32>>();
19-
is_send::<mpsc::Receiver<i32>>();
20-
}
14+
trait AssertSend: Send {}
15+
impl AssertSend for mpsc::Sender<i32> {}
16+
impl AssertSend for mpsc::Receiver<i32> {}
2117

2218
#[test]
2319
fn send_recv() {

tests/ready_queue.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ use futures::future;
88
use futures::stream::FuturesUnordered;
99
use futures::sync::oneshot;
1010

11-
#[test]
12-
fn bounds() {
13-
fn is_send<T: Send>() {}
14-
fn is_sync<T: Sync>() {}
15-
16-
is_send::<FuturesUnordered<()>>();
17-
is_sync::<FuturesUnordered<()>>();
18-
}
11+
trait AssertSendSync: Send + Sync {}
12+
impl AssertSendSync for FuturesUnordered<()> {}
1913

2014
#[test]
2115
fn basic_usage() {

0 commit comments

Comments
 (0)