Skip to content

Commit 8e9d851

Browse files
authored
chore: move timeout code to separate file (#25)
Signed-off-by: tison <[email protected]>
1 parent 45d8adf commit 8e9d851

File tree

2 files changed

+117
-95
lines changed

2 files changed

+117
-95
lines changed

fastimer/src/lib.rs

+3-95
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
//! [`NotifyAction`]: schedule::NotifyAction
4141
4242
use std::future::Future;
43-
use std::future::IntoFuture;
44-
use std::task::Context;
45-
use std::task::Poll;
4643
use std::time::Duration;
4744
use std::time::Instant;
4845

@@ -51,6 +48,9 @@ mod macros;
5148
mod interval;
5249
pub use interval::*;
5350

51+
mod timeout;
52+
pub use timeout::*;
53+
5454
pub mod schedule;
5555

5656
/// Create a far future instant.
@@ -126,95 +126,3 @@ pub trait MakeDelayExt: MakeDelay {
126126
}
127127

128128
impl<T: MakeDelay> MakeDelayExt for T {}
129-
130-
/// Errors returned by [`Timeout`].
131-
///
132-
/// This error is returned when a timeout expires before the function was able
133-
/// to finish.
134-
#[derive(Debug, PartialEq, Eq)]
135-
pub struct Elapsed(());
136-
137-
/// A future that completes when the inner future completes or when the timeout expires.
138-
///
139-
/// Created by [`timeout`] or [`timeout_at`].
140-
#[must_use = "futures do nothing unless you `.await` or poll them"]
141-
#[derive(Debug)]
142-
#[pin_project::pin_project]
143-
pub struct Timeout<T, D> {
144-
#[pin]
145-
value: T,
146-
#[pin]
147-
delay: D,
148-
}
149-
150-
impl<T, D> Timeout<T, D> {
151-
/// Gets a reference to the underlying value in this timeout.
152-
pub fn get(&self) -> &T {
153-
&self.value
154-
}
155-
156-
/// Gets a mutable reference to the underlying value in this timeout.
157-
pub fn get_mut(&mut self) -> &mut T {
158-
&mut self.value
159-
}
160-
161-
/// Consumes this timeout, returning the underlying value.
162-
pub fn into_inner(self) -> T {
163-
self.value
164-
}
165-
}
166-
167-
impl<T, D> Future for Timeout<T, D>
168-
where
169-
T: Future,
170-
D: Future<Output = ()>,
171-
{
172-
type Output = Result<T::Output, Elapsed>;
173-
174-
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
175-
let this = self.project();
176-
177-
if let Poll::Ready(v) = this.value.poll(cx) {
178-
return Poll::Ready(Ok(v));
179-
}
180-
181-
match this.delay.poll(cx) {
182-
Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))),
183-
Poll::Pending => Poll::Pending,
184-
}
185-
}
186-
}
187-
188-
/// Requires a `Future` to complete before the specified duration has elapsed.
189-
pub fn timeout<F, D>(
190-
duration: Duration,
191-
future: F,
192-
make_delay: &D,
193-
) -> Timeout<F::IntoFuture, D::Delay>
194-
where
195-
F: IntoFuture,
196-
D: MakeDelay + ?Sized,
197-
{
198-
let delay = make_delay.delay(duration);
199-
Timeout {
200-
value: future.into_future(),
201-
delay,
202-
}
203-
}
204-
205-
/// Requires a `Future` to complete before the specified instant in time.
206-
pub fn timeout_at<F, D>(
207-
deadline: Instant,
208-
future: F,
209-
make_delay: &D,
210-
) -> Timeout<F::IntoFuture, D::Delay>
211-
where
212-
F: IntoFuture,
213-
D: MakeDelay + ?Sized,
214-
{
215-
let delay = make_delay.delay_util(deadline);
216-
Timeout {
217-
value: future.into_future(),
218-
delay,
219-
}
220-
}

fastimer/src/timeout.rs

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2024 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::future::Future;
16+
use std::future::IntoFuture;
17+
use std::task::Context;
18+
use std::task::Poll;
19+
use std::time::Duration;
20+
use std::time::Instant;
21+
22+
use crate::MakeDelay;
23+
24+
/// Errors returned by [`Timeout`].
25+
///
26+
/// This error is returned when a timeout expires before the function was able
27+
/// to finish.
28+
#[derive(Debug, PartialEq, Eq)]
29+
pub struct Elapsed(());
30+
31+
/// A future that completes when the inner future completes or when the timeout expires.
32+
///
33+
/// Created by [`timeout`] or [`timeout_at`].
34+
#[must_use = "futures do nothing unless you `.await` or poll them"]
35+
#[derive(Debug)]
36+
#[pin_project::pin_project]
37+
pub struct Timeout<T, D> {
38+
#[pin]
39+
value: T,
40+
#[pin]
41+
delay: D,
42+
}
43+
44+
impl<T, D> Timeout<T, D> {
45+
/// Gets a reference to the underlying value in this timeout.
46+
pub fn get(&self) -> &T {
47+
&self.value
48+
}
49+
50+
/// Gets a mutable reference to the underlying value in this timeout.
51+
pub fn get_mut(&mut self) -> &mut T {
52+
&mut self.value
53+
}
54+
55+
/// Consumes this timeout, returning the underlying value.
56+
pub fn into_inner(self) -> T {
57+
self.value
58+
}
59+
}
60+
61+
impl<T, D> Future for Timeout<T, D>
62+
where
63+
T: Future,
64+
D: Future<Output = ()>,
65+
{
66+
type Output = Result<T::Output, Elapsed>;
67+
68+
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
69+
let this = self.project();
70+
71+
if let Poll::Ready(v) = this.value.poll(cx) {
72+
return Poll::Ready(Ok(v));
73+
}
74+
75+
match this.delay.poll(cx) {
76+
Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))),
77+
Poll::Pending => Poll::Pending,
78+
}
79+
}
80+
}
81+
82+
/// Requires a `Future` to complete before the specified duration has elapsed.
83+
pub fn timeout<F, D>(
84+
duration: Duration,
85+
future: F,
86+
make_delay: &D,
87+
) -> Timeout<F::IntoFuture, D::Delay>
88+
where
89+
F: IntoFuture,
90+
D: MakeDelay + ?Sized,
91+
{
92+
let delay = make_delay.delay(duration);
93+
Timeout {
94+
value: future.into_future(),
95+
delay,
96+
}
97+
}
98+
99+
/// Requires a `Future` to complete before the specified instant in time.
100+
pub fn timeout_at<F, D>(
101+
deadline: Instant,
102+
future: F,
103+
make_delay: &D,
104+
) -> Timeout<F::IntoFuture, D::Delay>
105+
where
106+
F: IntoFuture,
107+
D: MakeDelay + ?Sized,
108+
{
109+
let delay = make_delay.delay_util(deadline);
110+
Timeout {
111+
value: future.into_future(),
112+
delay,
113+
}
114+
}

0 commit comments

Comments
 (0)