Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6723e9

Browse files
add compile_fail examples
1 parent 7f1c015 commit e6723e9

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

‎library/std/src/sync/oneshot.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::{error, fmt};
99
#[must_use]
1010
#[unstable(feature = "oneshot_channel", issue = "143674")]
1111
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);
1315
(Sender { inner: tx }, Receiver { inner: rx })
1416
}
1517

@@ -18,15 +20,35 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
1820
////////////////////////////////////////////////////////////////////////////////////////////////////
1921

2022
/// 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+
/// ```
2141
#[unstable(feature = "oneshot_channel", issue = "143674")]
2242
pub struct Sender<T> {
2343
/// The `oneshot` channel is simply a wrapper around a `mpmc` channel.
2444
inner: mpmc::Sender<T>,
2545
}
2646

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).
3052
#[unstable(feature = "oneshot_channel", issue = "143674")]
3153
unsafe impl<T> Sync for Sender<T> {}
3254

@@ -53,15 +75,35 @@ impl<T> fmt::Debug for Sender<T> {
5375
////////////////////////////////////////////////////////////////////////////////////////////////////
5476

5577
/// 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+
/// ```
5696
#[unstable(feature = "oneshot_channel", issue = "143674")]
5797
pub struct Receiver<T> {
5898
/// The `oneshot` channel is simply a wrapper around a `mpmc` channel.
5999
inner: mpmc::Receiver<T>,
60100
}
61101

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).
65107
#[unstable(feature = "oneshot_channel", issue = "143674")]
66108
unsafe impl<T> Sync for Receiver<T> {}
67109

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /