Skip to content

Commit 8ae283d

Browse files
authored
Merge pull request #36 from async-rs/minimal
Minimal
2 parents d5aa531 + d23f8ee commit 8ae283d

File tree

9 files changed

+371
-997
lines changed

9 files changed

+371
-997
lines changed

README.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,6 @@
77
A library for working with timers, timeouts, and intervals with the `futures`
88
crate.
99

10-
```toml
11-
# Cargo.toml
12-
[dependencies]
13-
futures-timer = "0.3"
14-
```
15-
16-
An example of using a `Delay` is:
17-
18-
```rust
19-
use std::time::Duration;
20-
21-
use futures::prelude::*;
22-
use futures_timer::Delay;
23-
24-
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
25-
Delay::new(Duration::from_secs(3))
26-
.map(|()| println!("printed after three seconds"))
27-
.await?;
28-
}
29-
```
30-
31-
And using an `Interval`:
32-
33-
```rust
34-
use std::time::Duration;
35-
36-
use futures::prelude::*;
37-
use futures_timer::Interval;
38-
39-
#[runtime::main]
40-
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
41-
Interval::new(Duration::from_secs(4))
42-
.take(4)
43-
.for_each(|()| Ok(println!("printed after three seconds")))
44-
.await?;
45-
}
46-
```
47-
48-
Or timing out a future
49-
50-
```rust
51-
use std::time::Duration;
52-
53-
use futures_timer::FutureExt;
54-
55-
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
56-
// create a future that will take at most 3 seconds to resolve
57-
let future = long_running_future()
58-
.timeout(Duration::from_secs(3));
59-
}
60-
```
61-
6210
# License
6311

6412
This project is licensed under either of

src/delay.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,20 @@ impl Delay {
3838
/// The default timer will be spun up in a helper thread on first use.
3939
#[inline]
4040
pub fn new(dur: Duration) -> Delay {
41-
Delay::new_at(Instant::now() + dur)
41+
Delay::new_handle(Instant::now() + dur, Default::default())
4242
}
4343

44-
/// Creates a new future which will fire at the time specified by `at`.
45-
///
46-
/// The returned object will be bound to the default timer for this thread.
47-
/// The default timer will be spun up in a helper thread on first use.
44+
/// Return the `Instant` when this delay will fire.
4845
#[inline]
49-
pub fn new_at(at: Instant) -> Delay {
50-
Delay::new_handle(at, Default::default())
46+
pub fn when(&self, timeout: &Delay) -> Instant {
47+
timeout.when
5148
}
5249

5350
/// Creates a new future which will fire at the time specified by `at`.
5451
///
5552
/// The returned instance of `Delay` will be bound to the timer specified by
5653
/// the `handle` argument.
54+
#[doc(hidden)]
5755
pub fn new_handle(at: Instant, handle: TimerHandle) -> Delay {
5856
let inner = match handle.inner.upgrade() {
5957
Some(i) => i,
@@ -89,15 +87,6 @@ impl Delay {
8987
}
9088
}
9189

92-
/// Resets this timeout to an new timeout which will fire at the time
93-
/// specified by `dur`.
94-
///
95-
/// This is equivalent to calling `reset_at` with `Instant::now() + dur`
96-
#[inline]
97-
pub fn reset(&mut self, dur: Duration) {
98-
self.reset_at(Instant::now() + dur)
99-
}
100-
10190
/// Resets this timeout to an new timeout which will fire at the time
10291
/// specified by `at`.
10392
///
@@ -112,9 +101,9 @@ impl Delay {
112101
/// will be dropped. It is required to call `poll` again after this method
113102
/// has been called to ensure tha ta task is blocked on this future.
114103
#[inline]
115-
pub fn reset_at(&mut self, at: Instant) {
116-
self.when = at;
117-
if self._reset(at).is_err() {
104+
pub fn reset(&mut self, dur: Duration) {
105+
self.when = Instant::now() + dur;
106+
if self._reset(self.when).is_err() {
118107
self.state = None
119108
}
120109
}
@@ -148,11 +137,6 @@ impl Delay {
148137
}
149138
}
150139

151-
#[inline]
152-
pub fn fires_at(timeout: &Delay) -> Instant {
153-
timeout.when
154-
}
155-
156140
impl Future for Delay {
157141
type Output = io::Result<()>;
158142

src/ext.rs

Lines changed: 0 additions & 200 deletions
This file was deleted.

src/heap_timer.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::cmp::Ordering;
2+
use std::sync::Arc;
3+
use std::time::Instant;
4+
5+
use crate::{Node, ScheduledTimer};
6+
7+
/// Entries in the timer heap, sorted by the instant they're firing at and then
8+
/// also containing some payload data.
9+
pub(crate) struct HeapTimer {
10+
pub(crate) at: Instant,
11+
pub(crate) gen: usize,
12+
pub(crate) node: Arc<Node<ScheduledTimer>>,
13+
}
14+
15+
impl PartialEq for HeapTimer {
16+
fn eq(&self, other: &HeapTimer) -> bool {
17+
self.at == other.at
18+
}
19+
}
20+
21+
impl Eq for HeapTimer {}
22+
23+
impl PartialOrd for HeapTimer {
24+
fn partial_cmp(&self, other: &HeapTimer) -> Option<Ordering> {
25+
Some(self.cmp(other))
26+
}
27+
}
28+
29+
impl Ord for HeapTimer {
30+
fn cmp(&self, other: &HeapTimer) -> Ordering {
31+
self.at.cmp(&other.at)
32+
}
33+
}

0 commit comments

Comments
 (0)