mirror of https://github.com/rust-lang/rust.git
Rollup merge of #42347 - frewsxcv:frewsxcv/improve-receiver-recv-timeout-docs, r=QuietMisdreavus
Rewrite doc examples for `Receiver::recv_timeout`. None
This commit is contained in:
commit
9bd6dc73fc
|
@ -1252,14 +1252,43 @@ impl<T> Receiver<T> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
/// Successfully receiving value before encountering timeout:
|
||||||
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::sync::mpsc::{self, RecvTimeoutError};
|
/// use std::thread;
|
||||||
/// use std::time::Duration;
|
/// use std::time::Duration;
|
||||||
|
/// use std::sync::mpsc;
|
||||||
///
|
///
|
||||||
/// let (send, recv) = mpsc::channel::<()>();
|
/// let (send, recv) = mpsc::channel();
|
||||||
///
|
///
|
||||||
/// let timeout = Duration::from_millis(100);
|
/// thread::spawn(move || {
|
||||||
/// assert_eq!(Err(RecvTimeoutError::Timeout), recv.recv_timeout(timeout));
|
/// send.send('a').unwrap();
|
||||||
|
/// });
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// recv.recv_timeout(Duration::from_millis(400)),
|
||||||
|
/// Ok('a')
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Receiving an error upon reaching timeout:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use std::thread;
|
||||||
|
/// use std::time::Duration;
|
||||||
|
/// use std::sync::mpsc;
|
||||||
|
///
|
||||||
|
/// let (send, recv) = mpsc::channel();
|
||||||
|
///
|
||||||
|
/// thread::spawn(move || {
|
||||||
|
/// thread::sleep(Duration::from_millis(800));
|
||||||
|
/// send.send('a').unwrap();
|
||||||
|
/// });
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// recv.recv_timeout(Duration::from_millis(400)),
|
||||||
|
/// Err(mpsc::RecvTimeoutError::Timeout)
|
||||||
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
|
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
|
||||||
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError> {
|
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError> {
|
||||||
|
|
Loading…
Reference in New Issue