Skip to content

test: demo the implementation is not general enough issue #22

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

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 5 additions & 6 deletions fastimer-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ version.workspace = true
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 }
fastimer = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
tokio = { workspace = true, features = ["full"] }

[lints]
workspace = true
6 changes: 1 addition & 5 deletions fastimer-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

//! [`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;
Expand All @@ -31,7 +29,7 @@ mod delay {
#[derive(Clone, Copy, Debug, Default)]
pub struct MakeTokioDelay;

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

fn delay_util(&self, at: Instant) -> Self::Delay {
Expand All @@ -44,10 +42,8 @@ mod delay {
}
}

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

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

Expand Down
73 changes: 73 additions & 0 deletions fastimer-tokio/tests/lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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.

use std::time::Duration;
use std::time::Instant;

use fastimer_tokio::MakeTokioDelay;

fn timer() -> &'static MakeTokioDelay {
static TIMER: MakeTokioDelay = MakeTokioDelay;
&TIMER
}

struct TimerWrapper {
timer: &'static MakeTokioDelay,
}

fn trick_timer() -> TimerWrapper {
TimerWrapper { timer: timer() }
}

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

fn delay_util(&self, at: Instant) -> Self::Delay {
self.timer.delay_util(at)
}

fn delay(&self, duration: Duration) -> Self::Delay {
self.timer.delay(duration)
}
}

#[test]
fn test_lifetime_ok() {
let rt1 = tokio::runtime::Runtime::new().unwrap();
let rt2 = tokio::runtime::Runtime::new().unwrap();

let spawn = rt1.spawn(async move {
let mut interval = fastimer::interval(Duration::from_secs(1), trick_timer());
for _ in 0..3 {
interval.tick().await;
}
});

rt2.block_on(spawn).unwrap();
}

// #[test]
// fn test_lifetime_nok() {
// let rt1 = tokio::runtime::Runtime::new().unwrap();
// let rt2 = tokio::runtime::Runtime::new().unwrap();
//
// let spawn = rt1.spawn(async move {
// let mut interval = fastimer::interval(Duration::from_secs(1), timer());
// for _ in 0..3 {
// interval.tick().await;
// }
// });
//
// rt2.block_on(spawn).unwrap();
// }
Comment on lines +60 to +73
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to compile:

error: implementation of `MakeDelay` is not general enough
  --> fastimer-tokio/tests/lifetime.rs:49:17
   |
49 |       let spawn = rt1.spawn(async move {
   |  _________________^
50 | |         let mut interval = fastimer::interval(Duration::from_secs(1), timer());
51 | |         for _ in 0..3 {
52 | |             interval.tick().await;
53 | |         }
54 | |     });
   | |______^ implementation of `MakeDelay` is not general enough
   |
   = note: `&'0 MakeTokioDelay` must implement `MakeDelay`, for any lifetime `'0`...
   = note: ...but `MakeDelay` is actually implemented for the type `&'static MakeTokioDelay`