@@ -9,7 +9,9 @@ use crate::{error, fmt};
9
9
#[ must_use]
10
10
#[ unstable( feature = "oneshot_channel" , issue = "143674" ) ]
11
11
pub fn channel < T > ( ) -> ( Sender < T > , Receiver < T > ) {
12
- let ( tx, rx) = mpmc:: channel ( ) ;
12
+ // Using a `sync_channel` with capacity 1 means that the internal implementation will use the
13
+ // `Array`-flavored channel implementtion.
14
+ let ( tx, rx) = mpmc:: sync_channel ( 1 ) ;
13
15
( Sender { inner : tx } , Receiver { inner : rx } )
14
16
}
15
17
@@ -18,15 +20,35 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
18
20
////////////////////////////////////////////////////////////////////////////////////////////////////
19
21
20
22
/// The sending half of a oneshot channel.
23
+ ///
24
+ /// # Examples
25
+ ///
26
+ /// (more examples to come)
27
+ ///
28
+ /// ```compile_fail
29
+ /// # #![feature(oneshot_channel)]
30
+ /// # use std::sync::oneshot;
31
+ /// #
32
+ /// let (sender, receiver) = oneshot::channel();
33
+ ///
34
+ /// struct NotSend(*mut ());
35
+ /// std::thread::spawn(move || {
36
+ /// sender.send(NotSend(std::ptr::null_mut()));
37
+ /// });
38
+ ///
39
+ /// let reply = receiver.try_recv().unwrap();
40
+ /// ```
21
41
#[ unstable( feature = "oneshot_channel" , issue = "143674" ) ]
22
42
pub struct Sender < T > {
23
43
/// The `oneshot` channel is simply a wrapper around a `mpmc` channel.
24
44
inner : mpmc:: Sender < T > ,
25
45
}
26
46
27
- /// SAFETY: Since the only methods in which synchronization must occur take full ownership of the
28
- /// [`Sender`], it is perfectly safe to share a &[`Sender`] between threads (as it is effectively
29
- /// useless without full ownership).
47
+ /// # Safety
48
+ ///
49
+ /// Since the only methods in which synchronization must occur take full ownership of the
50
+ /// [`Sender`], it is perfectly safe to share a `&Sender` between threads (as it is effectively
51
+ /// useless without ownership).
30
52
#[ unstable( feature = "oneshot_channel" , issue = "143674" ) ]
31
53
unsafe impl < T > Sync for Sender < T > { }
32
54
@@ -53,15 +75,35 @@ impl<T> fmt::Debug for Sender<T> {
53
75
////////////////////////////////////////////////////////////////////////////////////////////////////
54
76
55
77
/// The receiving half of a oneshot channel.
78
+ ///
79
+ /// # Examples
80
+ ///
81
+ /// (more examples to come)
82
+ ///
83
+ /// ```compile_fail
84
+ /// # #![feature(oneshot_channel)]
85
+ /// # use std::sync::oneshot;
86
+ /// #
87
+ /// let (sender, receiver) = oneshot::channel();
88
+ ///
89
+ /// struct NotSend(*mut ());
90
+ /// sender.send(NotSend(std::ptr::null_mut()));
91
+ ///
92
+ /// std::thread::spawn(move || {
93
+ /// let reply = receiver.try_recv().unwrap();
94
+ /// });
95
+ /// ```
56
96
#[ unstable( feature = "oneshot_channel" , issue = "143674" ) ]
57
97
pub struct Receiver < T > {
58
98
/// The `oneshot` channel is simply a wrapper around a `mpmc` channel.
59
99
inner : mpmc:: Receiver < T > ,
60
100
}
61
101
62
- /// SAFETY: Since the only methods in which synchronization must occur take full ownership of the
63
- /// [`Receiver`], it is perfectly safe to share a &[`Receiver`] between threads (as it is unable to
64
- /// receive any values without full ownership).
102
+ /// # Safety
103
+ ///
104
+ /// Since the only methods in which synchronization must occur take full ownership of the
105
+ /// [`Receiver`], it is perfectly safe to share a `&Receiver` between threads (as it is unable to
106
+ /// receive any values without ownership).
65
107
#[ unstable( feature = "oneshot_channel" , issue = "143674" ) ]
66
108
unsafe impl < T > Sync for Receiver < T > { }
67
109
0 commit comments