Skip to content

Commit 17ba0cf

Browse files
committed
auto merge of #14745 : huonw/rust/timer-doc, r=alexcrichton
std::io: expand the oneshot/periodic docs. Examples! Fixes #14714.
2 parents 9239bb4 + e8d180d commit 17ba0cf

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

src/libstd/io/timer.rs

+68-8
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,87 @@ impl Timer {
9696
}
9797

9898
/// Creates a oneshot receiver which will have a notification sent when
99-
/// `msecs` milliseconds has elapsed. This does *not* block the current
100-
/// task, but instead returns immediately.
99+
/// `msecs` milliseconds has elapsed.
100+
///
101+
/// This does *not* block the current task, but instead returns immediately.
101102
///
102103
/// Note that this invalidates any previous receiver which has been created
103104
/// by this timer, and that the returned receiver will be invalidated once
104-
/// the timer is destroyed (when it falls out of scope).
105+
/// the timer is destroyed (when it falls out of scope). In particular, if
106+
/// this is called in method-chaining style, the receiver will be
107+
/// invalidated at the end of that statement, and all `recv` calls will
108+
/// fail.
109+
///
110+
/// # Example
111+
///
112+
/// ```rust
113+
/// use std::io::timer::Timer;
114+
///
115+
/// let mut timer = Timer::new().unwrap();
116+
/// let ten_milliseconds = timer.oneshot(10);
117+
///
118+
/// for _ in range(0, 100) { /* do work */ }
119+
///
120+
/// // blocks until 10 ms after the `oneshot` call
121+
/// ten_milliseconds.recv();
122+
/// ```
123+
///
124+
/// ```rust
125+
/// use std::io::timer::Timer;
126+
///
127+
/// // Incorrect, method chaining-style:
128+
/// let mut five_ms = Timer::new().unwrap().oneshot(5);
129+
/// // The timer object was destroyed, so this will always fail:
130+
/// // five_ms.recv()
131+
/// ```
105132
pub fn oneshot(&mut self, msecs: u64) -> Receiver<()> {
106133
let (tx, rx) = channel();
107134
self.obj.oneshot(msecs, box TimerCallback { tx: tx });
108135
return rx
109136
}
110137

111138
/// Creates a receiver which will have a continuous stream of notifications
112-
/// being sent every `msecs` milliseconds. This does *not* block the
113-
/// current task, but instead returns immediately. The first notification
114-
/// will not be received immediately, but rather after `msec` milliseconds
115-
/// have passed.
139+
/// being sent every `msecs` milliseconds.
140+
///
141+
/// This does *not* block the current task, but instead returns
142+
/// immediately. The first notification will not be received immediately,
143+
/// but rather after `msec` milliseconds have passed.
116144
///
117145
/// Note that this invalidates any previous receiver which has been created
118146
/// by this timer, and that the returned receiver will be invalidated once
119-
/// the timer is destroyed (when it falls out of scope).
147+
/// the timer is destroyed (when it falls out of scope). In particular, if
148+
/// this is called in method-chaining style, the receiver will be
149+
/// invalidated at the end of that statement, and all `recv` calls will
150+
/// fail.
151+
///
152+
/// # Example
153+
///
154+
/// ```rust
155+
/// use std::io::timer::Timer;
156+
///
157+
/// let mut timer = Timer::new().unwrap();
158+
/// let ten_milliseconds = timer.periodic(10);
159+
///
160+
/// for _ in range(0, 100) { /* do work */ }
161+
///
162+
/// // blocks until 10 ms after the `periodic` call
163+
/// ten_milliseconds.recv();
164+
///
165+
/// for _ in range(0, 100) { /* do work */ }
166+
///
167+
/// // blocks until 20 ms after the `periodic` call (*not* 10ms after the
168+
/// // previous `recv`)
169+
/// ten_milliseconds.recv();
170+
/// ```
171+
///
172+
/// ```rust
173+
/// use std::io::timer::Timer;
174+
///
175+
/// // Incorrect, method chaining-style.
176+
/// let mut five_ms = Timer::new().unwrap().periodic(5);
177+
/// // The timer object was destroyed, so this will always fail:
178+
/// // five_ms.recv()
179+
/// ```
120180
pub fn periodic(&mut self, msecs: u64) -> Receiver<()> {
121181
let (tx, rx) = channel();
122182
self.obj.period(msecs, box TimerCallback { tx: tx });

0 commit comments

Comments
 (0)