Skip to content

Commit 51ae96b

Browse files
authored
feat: add fastimer-tokio crate for out-of-the-box integration (#18)
Signed-off-by: tison <[email protected]>
1 parent 0a74c8a commit 51ae96b

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
[workspace]
16-
members = ["fastimer", "xtask"]
16+
members = ["fastimer", "fastimer-tokio", "xtask"]
1717
resolver = "2"
1818

1919
[workspace.package]
@@ -23,6 +23,11 @@ license = "Apache-2.0"
2323
readme = "README.md"
2424
repository = "https://github.com/fast/fastimer"
2525
rust-version = "1.83.0"
26+
version = "0.5.0"
27+
28+
[workspace.dependencies]
29+
fastimer = { version = "0.5.0", path = "fastimer" }
30+
tokio = { version = "1.43.0" }
2631

2732
[workspace.lints.rust]
2833
unknown_lints = "deny"

fastimer-tokio/Cargo.toml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
[package]
16+
name = "fastimer-tokio"
17+
18+
description = "This crates provides tokio runtime integration for fastimer."
19+
20+
edition.workspace = true
21+
homepage.workspace = true
22+
license.workspace = true
23+
readme.workspace = true
24+
repository.workspace = true
25+
rust-version.workspace = true
26+
version.workspace = true
27+
28+
[package.metadata.docs.rs]
29+
all-features = true
30+
rustdoc-args = ["--cfg", "docsrs"]
31+
32+
[features]
33+
spawn = ["dep:fastimer", "dep:tokio", "tokio/rt"]
34+
time = ["dep:fastimer", "dep:tokio", "tokio/time"]
35+
36+
[dependencies]
37+
fastimer = { workspace = true, optional = true }
38+
tokio = { workspace = true, optional = true }
39+
40+
[lints]
41+
workspace = true

fastimer-tokio/src/lib.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
16+
#![deny(missing_docs)]
17+
18+
//! [`tokio`] runtime support for [`fastimer`]'s traits.
19+
20+
#[cfg(feature = "time")]
21+
pub use delay::*;
22+
23+
#[cfg(feature = "time")]
24+
mod delay {
25+
use std::time::Duration;
26+
use std::time::Instant;
27+
28+
use fastimer::MakeDelay;
29+
30+
/// A delay implementation that uses Tokio's timer.
31+
#[derive(Clone, Copy, Debug, Default)]
32+
pub struct MakeTokioDelay;
33+
34+
impl MakeDelay for MakeTokioDelay {
35+
type Delay = tokio::time::Sleep;
36+
37+
fn delay_util(&self, at: Instant) -> Self::Delay {
38+
tokio::time::sleep_until(tokio::time::Instant::from_std(at))
39+
}
40+
41+
fn delay(&self, duration: Duration) -> Self::Delay {
42+
tokio::time::sleep(duration)
43+
}
44+
}
45+
}
46+
47+
#[cfg(feature = "spawn")]
48+
pub use spawn::*;
49+
50+
#[cfg(feature = "spawn")]
51+
mod spawn {
52+
use std::future::Future;
53+
54+
use fastimer::Spawn;
55+
56+
/// A spawn implementation that uses Tokio's runtime.
57+
#[derive(Clone, Debug, Default)]
58+
pub struct TokioSpawn(Option<tokio::runtime::Handle>);
59+
60+
impl TokioSpawn {
61+
/// Create a new [`TokioSpawn`] with the given [`tokio::runtime::Handle`].
62+
pub fn with_handle(mut self, handle: tokio::runtime::Handle) -> Self {
63+
self.0 = Some(handle);
64+
self
65+
}
66+
67+
/// Create a new [`TokioSpawn`] with the [`tokio::runtime::Handle`] in current context.
68+
pub fn current() -> Self {
69+
Self::default().with_handle(tokio::runtime::Handle::current())
70+
}
71+
}
72+
73+
impl Spawn for TokioSpawn {
74+
fn spawn<F: Future<Output = ()> + Send + 'static>(&self, future: F) {
75+
match &self.0 {
76+
None => tokio::spawn(future),
77+
Some(handle) => handle.spawn(future),
78+
};
79+
}
80+
}
81+
}

fastimer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
[package]
1616
name = "fastimer"
17-
version = "0.5.0"
1817

1918
description = "This crates implements runtime-agnostic timer traits and utilities."
2019

@@ -24,6 +23,7 @@ license.workspace = true
2423
readme.workspace = true
2524
repository.workspace = true
2625
rust-version.workspace = true
26+
version.workspace = true
2727

2828
[package.metadata.docs.rs]
2929
all-features = true

0 commit comments

Comments
 (0)