Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fastimer-tokio crate for out-of-the-box integration #18

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

[workspace]
members = ["fastimer", "xtask"]
members = ["fastimer", "fastimer-tokio", "xtask"]
resolver = "2"

[workspace.package]
Expand All @@ -23,6 +23,11 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/fast/fastimer"
rust-version = "1.83.0"
version = "0.5.0"

[workspace.dependencies]
fastimer = { version = "0.5.0", path = "fastimer" }
tokio = { version = "1.43.0" }

[workspace.lints.rust]
unknown_lints = "deny"
Expand Down
41 changes: 41 additions & 0 deletions fastimer-tokio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2024 FastLabs Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[package]
name = "fastimer-tokio"

description = "This crates provides tokio runtime integration for fastimer."

edition.workspace = true
homepage.workspace = true
license.workspace = true
readme.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
spawn = ["dep:fastimer", "dep:tokio", "tokio/rt"]
time = ["dep:fastimer", "dep:tokio", "tokio/time"]

[dependencies]
fastimer = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }

[lints]
workspace = true
81 changes: 81 additions & 0 deletions fastimer-tokio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(missing_docs)]

//! [`tokio`] runtime support for [`fastimer`]'s traits.

#[cfg(feature = "time")]
pub use delay::*;

#[cfg(feature = "time")]
mod delay {
use std::time::Duration;
use std::time::Instant;

use fastimer::MakeDelay;

/// A delay implementation that uses Tokio's timer.
#[derive(Clone, Copy, Debug, Default)]
pub struct MakeTokioDelay;

impl MakeDelay for MakeTokioDelay {
type Delay = tokio::time::Sleep;

fn delay_util(&self, at: Instant) -> Self::Delay {
tokio::time::sleep_until(tokio::time::Instant::from_std(at))
}

fn delay(&self, duration: Duration) -> Self::Delay {
tokio::time::sleep(duration)
}
}
}

#[cfg(feature = "spawn")]
pub use spawn::*;

#[cfg(feature = "spawn")]
mod spawn {
use std::future::Future;

use fastimer::Spawn;

/// A spawn implementation that uses Tokio's runtime.
#[derive(Clone, Debug, Default)]
pub struct TokioSpawn(Option<tokio::runtime::Handle>);

impl TokioSpawn {
/// Create a new [`TokioSpawn`] with the given [`tokio::runtime::Handle`].
pub fn with_handle(mut self, handle: tokio::runtime::Handle) -> Self {
self.0 = Some(handle);
self
}

/// Create a new [`TokioSpawn`] with the [`tokio::runtime::Handle`] in current context.
pub fn current() -> Self {
Self::default().with_handle(tokio::runtime::Handle::current())
}
}

impl Spawn for TokioSpawn {
fn spawn<F: Future<Output = ()> + Send + 'static>(&self, future: F) {
match &self.0 {
None => tokio::spawn(future),
Some(handle) => handle.spawn(future),
};
}
}
}
2 changes: 1 addition & 1 deletion fastimer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

[package]
name = "fastimer"
version = "0.5.0"

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

Expand All @@ -24,6 +23,7 @@ license.workspace = true
readme.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[package.metadata.docs.rs]
all-features = true
Expand Down