@@ -96,27 +96,87 @@ impl Timer {
96
96
}
97
97
98
98
/// 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.
101
102
///
102
103
/// Note that this invalidates any previous receiver which has been created
103
104
/// 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
+ /// ```
105
132
pub fn oneshot ( & mut self , msecs : u64 ) -> Receiver < ( ) > {
106
133
let ( tx, rx) = channel ( ) ;
107
134
self . obj . oneshot ( msecs, box TimerCallback { tx : tx } ) ;
108
135
return rx
109
136
}
110
137
111
138
/// 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.
116
144
///
117
145
/// Note that this invalidates any previous receiver which has been created
118
146
/// 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
+ /// ```
120
180
pub fn periodic ( & mut self , msecs : u64 ) -> Receiver < ( ) > {
121
181
let ( tx, rx) = channel ( ) ;
122
182
self . obj . period ( msecs, box TimerCallback { tx : tx } ) ;
0 commit comments