Skip to content

Commit bea4862

Browse files
committed
fix up all the code examples and mark them all no_run
1 parent 5f8cabb commit bea4862

File tree

5 files changed

+144
-142
lines changed

5 files changed

+144
-142
lines changed

src/future/future_ext.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ pub trait FutureExt: Future {
1919
///
2020
/// # Example
2121
///
22-
/// ```
23-
/// use futures_time::prelude::*;
24-
/// use futures_time::time::{Instant, Duration};
22+
/// ```no_run
23+
/// use wstd::prelude::*;
24+
/// use wstd::time::{Instant, Duration};
2525
/// use std::io;
2626
///
27-
/// fn main() {
28-
/// async_io::block_on(async {
29-
/// let res = async { "meow" }
30-
/// .delay(Duration::from_millis(100)) // longer delay
31-
/// .timeout(Duration::from_millis(50)) // shorter timeout
32-
/// .await;
33-
/// assert_eq!(res.unwrap_err().kind(), io::ErrorKind::TimedOut); // error
27+
/// #[wstd::main]
28+
/// async fn main() {
29+
/// let res = async { "meow" }
30+
/// .delay(Duration::from_millis(100)) // longer delay
31+
/// .timeout(Duration::from_millis(50)) // shorter timeout
32+
/// .await;
33+
/// assert_eq!(res.unwrap_err().kind(), io::ErrorKind::TimedOut); // error
3434
///
35-
/// let res = async { "meow" }
36-
/// .delay(Duration::from_millis(50)) // shorter delay
37-
/// .timeout(Duration::from_millis(100)) // longer timeout
38-
/// .await;
39-
/// assert_eq!(res.unwrap(), "meow"); // success
40-
/// });
35+
/// let res = async { "meow" }
36+
/// .delay(Duration::from_millis(50)) // shorter delay
37+
/// .timeout(Duration::from_millis(100)) // longer timeout
38+
/// .await;
39+
/// assert_eq!(res.unwrap(), "meow"); // success
4140
/// }
4241
/// ```
4342
fn timeout<D>(self, deadline: D) -> Timeout<Self, D::IntoFuture>
@@ -57,17 +56,16 @@ pub trait FutureExt: Future {
5756
///
5857
/// # Example
5958
///
60-
/// ```
61-
/// use futures_time::prelude::*;
62-
/// use futures_time::time::{Instant, Duration};
59+
/// ```no_run
60+
/// use wstd::prelude::*;
61+
/// use wstd::time::{Instant, Duration};
6362
///
64-
/// fn main() {
65-
/// async_io::block_on(async {
66-
/// let now = Instant::now();
67-
/// let delay = Duration::from_millis(100);
68-
/// let _ = async { "meow" }.delay(delay).await;
69-
/// assert!(now.elapsed() >= *delay);
70-
/// });
63+
/// #[wstd::main]
64+
/// async fn main() {
65+
/// let now = Instant::now();
66+
/// let delay = Duration::from_millis(100);
67+
/// let _ = async { "meow" }.delay(delay).await;
68+
/// assert!(now.elapsed() >= delay);
7169
/// }
7270
/// ```
7371
fn delay<D>(self, deadline: D) -> Delay<Self, D::IntoFuture>

src/future/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,22 @@
1919
//! [`Stream::timeout`]: crate::stream::StreamExt::timeout
2020
//!
2121
//!
22-
//! ```
22+
//! ```no_run
2323
//! use futures_lite::prelude::*;
24-
//! use futures_time::prelude::*;
25-
//! use futures_time::channel;
26-
//! use futures_time::time::Duration;
27-
//!
28-
//! fn main() {
29-
//! async_io::block_on(async {
30-
//! let (send, mut recv) = channel::bounded::<()>(1); // create a new send/receive pair
31-
//! let mut counter = 0;
32-
//! let value = async { "meow" }
33-
//! .delay(Duration::from_millis(100))
34-
//! .timeout(recv.next()) // time-out if the sender is dropped.
35-
//! .await;
36-
//!
37-
//! assert_eq!(value.unwrap(), "meow");
38-
//! })
24+
//! use wstd::prelude::*;
25+
//! use wstd::channel;
26+
//! use wstd::time::Duration;
27+
//!
28+
//! #[wstd::main]
29+
//! async fn main() {
30+
//! let (send, mut recv) = channel::bounded::<()>(1); // create a new send/receive pair
31+
//! let mut counter = 0;
32+
//! let value = async { "meow" }
33+
//! .delay(Duration::from_millis(100))
34+
//! .timeout(recv.next()) // time-out if the sender is dropped.
35+
//! .await;
36+
//!
37+
//! assert_eq!(value.unwrap(), "meow");
3938
//! }
4039
//! ```
4140

src/stream/stream_ext.rs

Lines changed: 90 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,22 @@ pub trait StreamExt: Stream {
3232
///
3333
/// # Example
3434
///
35-
/// ```
35+
/// ```no_run
3636
/// use futures_lite::prelude::*;
37-
/// use futures_time::prelude::*;
38-
/// use futures_time::time::{Instant, Duration};
39-
/// use futures_time::stream;
40-
///
41-
/// fn main() {
42-
/// async_io::block_on(async {
43-
/// let mut counter = 0;
44-
/// stream::interval(Duration::from_millis(100))
45-
/// .take(4)
46-
/// .sample(Duration::from_millis(200))
47-
/// .for_each(|_| counter += 1)
48-
/// .await;
49-
///
50-
/// assert_eq!(counter, 2);
51-
/// })
37+
/// use wstd::prelude::*;
38+
/// use wstd::time::{Instant, Duration};
39+
/// use wstd::stream;
40+
///
41+
/// #[wstd::main]
42+
/// async fn main() {
43+
/// let mut counter = 0;
44+
/// stream::interval(Duration::from_millis(100))
45+
/// .take(4)
46+
/// .sample(Duration::from_millis(200))
47+
/// .for_each(|_| counter += 1)
48+
/// .await;
49+
///
50+
/// assert_eq!(counter, 2);
5251
/// }
5352
/// ```
5453
fn sample<I>(self, interval: I) -> Sample<Self, I::IntoStream>
@@ -73,23 +72,22 @@ pub trait StreamExt: Stream {
7372
///
7473
/// # Example
7574
///
76-
/// ```
75+
/// ```no_run
7776
/// use futures_lite::prelude::*;
78-
/// use futures_time::prelude::*;
79-
/// use futures_time::time::{Instant, Duration};
80-
/// use futures_time::stream;
81-
///
82-
/// fn main() {
83-
/// async_io::block_on(async {
84-
/// let mut counter = 0;
85-
/// stream::interval(Duration::from_millis(5))
86-
/// .take(10)
87-
/// .buffer(Duration::from_millis(20))
88-
/// .for_each(|buf| counter += buf.len())
89-
/// .await;
90-
///
91-
/// assert_eq!(counter, 10);
92-
/// })
77+
/// use wstd::prelude::*;
78+
/// use wstd::time::{Instant, Duration};
79+
/// use wstd::stream;
80+
///
81+
/// #[wstd::main]
82+
/// async fn main() {
83+
/// let mut counter = 0;
84+
/// stream::interval(Duration::from_millis(5))
85+
/// .take(10)
86+
/// .buffer(Duration::from_millis(20))
87+
/// .for_each(|buf| counter += buf.len())
88+
/// .await;
89+
///
90+
/// assert_eq!(counter, 10);
9391
/// }
9492
/// ```
9593
fn buffer<I>(self, interval: I) -> Buffer<Self, I::IntoStream>
@@ -119,23 +117,22 @@ pub trait StreamExt: Stream {
119117
///
120118
/// # Example
121119
///
122-
/// ```
120+
/// ```no_run
123121
/// use futures_lite::prelude::*;
124-
/// use futures_time::prelude::*;
125-
/// use futures_time::time::{Instant, Duration};
126-
/// use futures_time::stream;
127-
///
128-
/// fn main() {
129-
/// async_io::block_on(async {
130-
/// let mut counter = 0;
131-
/// stream::interval(Duration::from_millis(10))
132-
/// .take(10)
133-
/// .debounce(Duration::from_millis(20)) // the window is greater than the interval
134-
/// .for_each(|_| counter += 1)
135-
/// .await;
136-
///
137-
/// assert_eq!(counter, 1); // so only the last item is received
138-
/// })
122+
/// use wstd::prelude::*;
123+
/// use wstd::time::{Instant, Duration};
124+
/// use wstd::stream;
125+
///
126+
/// #[wstd::main]
127+
/// async fn main() {
128+
/// let mut counter = 0;
129+
/// stream::interval(Duration::from_millis(10))
130+
/// .take(10)
131+
/// .debounce(Duration::from_millis(20)) // the window is greater than the interval
132+
/// .for_each(|_| counter += 1)
133+
/// .await;
134+
///
135+
/// assert_eq!(counter, 1); // so only the last item is received
139136
/// }
140137
/// ```
141138
fn debounce<D>(self, window: D) -> Debounce<Self, D::IntoFuture>
@@ -156,19 +153,18 @@ pub trait StreamExt: Stream {
156153
///
157154
/// # Example
158155
///
159-
/// ```
156+
/// ```no_run
160157
/// use futures_lite::prelude::*;
161-
/// use futures_time::prelude::*;
162-
/// use futures_time::time::{Instant, Duration};
163-
/// use futures_lite::stream;
164-
///
165-
/// fn main() {
166-
/// async_io::block_on(async {
167-
/// let now = Instant::now();
168-
/// let delay = Duration::from_millis(100);
169-
/// let _ = stream::once("meow").delay(delay).next().await;
170-
/// assert!(now.elapsed() >= *delay);
171-
/// });
158+
/// use wstd::prelude::*;
159+
/// use wstd::time::{Instant, Duration};
160+
/// use wstd::stream;
161+
///
162+
/// #[wstd::main]
163+
/// async fn main() {
164+
/// let now = Instant::now();
165+
/// let delay = Duration::from_millis(100);
166+
/// let _ = futures_lite::stream::once("meow").delay(delay).next().await;
167+
/// assert!(now.elapsed() >= delay);
172168
/// }
173169
/// ```
174170
fn delay<D>(self, deadline: D) -> Delay<Self, D::IntoFuture>
@@ -214,23 +210,22 @@ pub trait StreamExt: Stream {
214210
///
215211
/// # Examples
216212
///
217-
/// ```
213+
/// ```no_run
218214
/// use futures_lite::prelude::*;
219-
/// use futures_time::prelude::*;
220-
/// use futures_time::time::Duration;
221-
/// use futures_time::stream;
222-
///
223-
/// fn main() {
224-
/// async_io::block_on(async {
225-
/// let mut counter = 0;
226-
/// stream::interval(Duration::from_millis(100)) // Yield an item every 100ms
227-
/// .take(4) // Stop after 4 items
228-
/// .throttle(Duration::from_millis(300)) // Only let an item through every 300ms
229-
/// .for_each(|_| counter += 1) // Increment a counter for each item
230-
/// .await;
231-
///
232-
/// assert_eq!(counter, 2);
233-
/// })
215+
/// use wstd::prelude::*;
216+
/// use wstd::time::Duration;
217+
/// use wstd::stream;
218+
///
219+
/// #[wstd::main]
220+
/// async fn main() {
221+
/// let mut counter = 0;
222+
/// stream::interval(Duration::from_millis(100)) // Yield an item every 100ms
223+
/// .take(4) // Stop after 4 items
224+
/// .throttle(Duration::from_millis(300)) // Only let an item through every 300ms
225+
/// .for_each(|_| counter += 1) // Increment a counter for each item
226+
/// .await;
227+
///
228+
/// assert_eq!(counter, 2);
234229
/// }
235230
/// ```
236231
fn throttle<I>(self, interval: I) -> Throttle<Self, I::IntoStream>
@@ -254,29 +249,28 @@ pub trait StreamExt: Stream {
254249
///
255250
/// # Example
256251
///
257-
/// ```
252+
/// ```no_run
258253
/// use futures_lite::prelude::*;
259-
/// use futures_time::prelude::*;
260-
/// use futures_time::time::{Instant, Duration};
261-
/// use futures_lite::stream;
254+
/// use wstd::prelude::*;
255+
/// use wstd::time::{Instant, Duration};
256+
/// use wstd::stream;
262257
/// use std::io;
263258
///
264-
/// fn main() {
265-
/// async_io::block_on(async {
266-
/// let res = stream::once("meow")
267-
/// .delay(Duration::from_millis(100)) // longer delay
268-
/// .timeout(Duration::from_millis(50)) // shorter timeout
269-
/// .next()
270-
/// .await;
271-
/// assert_eq!(res.unwrap().unwrap_err().kind(), io::ErrorKind::TimedOut); // error
272-
///
273-
/// let res = stream::once("meow")
274-
/// .delay(Duration::from_millis(50)) // shorter delay
275-
/// .timeout(Duration::from_millis(100)) // longer timeout
276-
/// .next()
277-
/// .await;
278-
/// assert_eq!(res.unwrap().unwrap(), "meow"); // success
279-
/// });
259+
/// #[wstd::main]
260+
/// async fn main() {
261+
/// let res = futures_lite::stream::once("meow")
262+
/// .delay(Duration::from_millis(100)) // longer delay
263+
/// .timeout(Duration::from_millis(50)) // shorter timeout
264+
/// .next()
265+
/// .await;
266+
/// assert_eq!(res.unwrap().unwrap_err().kind(), io::ErrorKind::TimedOut); // error
267+
///
268+
/// let res = futures_lite::stream::once("meow")
269+
/// .delay(Duration::from_millis(50)) // shorter delay
270+
/// .timeout(Duration::from_millis(100)) // longer timeout
271+
/// .next()
272+
/// .await;
273+
/// assert_eq!(res.unwrap().unwrap(), "meow"); // success
280274
/// }
281275
/// ```
282276
fn timeout<D>(self, deadline: D) -> Timeout<Self, D::IntoFuture>

src/time/duration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl Duration {
5050
/// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
5151
///
5252
/// # Examples
53-
/// ```
54-
/// use futures_time::time::Duration;
53+
/// ```no_run
54+
/// use wstd::time::Duration;
5555
///
5656
/// let dur = Duration::from_secs_f64(2.7);
5757
/// assert_eq!(dur, Duration::new(2, 700_000_000));

src/time/instant.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,26 @@ impl Instant {
1818
///
1919
/// # Examples
2020
///
21-
/// ```
22-
/// use futures_time::time::Instant;
21+
/// ```no_run
22+
/// use wstd::time::Instant;
2323
///
2424
/// let now = Instant::now();
2525
/// ```
2626
#[must_use]
2727
pub fn now() -> Self {
2828
Instant(wasi::clocks::monotonic_clock::now())
2929
}
30+
31+
/// Returns the amount of time elapsed from another instant to this one, or zero duration if
32+
/// that instant is later than this one.
33+
pub fn duration_since(&self, earlier: Instant) -> Duration {
34+
Duration::from_micros(self.0.checked_sub(earlier.0).unwrap_or_default())
35+
}
36+
37+
/// Returns the amount of time elapsed since this instant.
38+
pub fn elapsed(&self) -> Duration {
39+
Instant::now().duration_since(*self)
40+
}
3041
}
3142

3243
impl Add<Duration> for Instant {

0 commit comments

Comments
 (0)