-
Notifications
You must be signed in to change notification settings - Fork 91
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
modules: Expand time module with Duration, Interval and Instant support #815
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How could we implement Timeout<T>
in Rune @udoprog? I initially tried, but due to the complexities with T
, I decided to pause for now... I'll try again later...
#[rune::function]
async fn timeout(duration: Duration, future: F) -> Timeout<F>
{
Timeout {
inner: tokio::time::timeout(duration.inner, future).await,
}
}
#[derive(Debug, Any)]
#[rune(item = ::time)]
pub struct Timeout<T>
{
inner: tokio::time::Timeout<T>,
}
Another situation is the async module
.function("tick", Interval::tick)
.build_associated::<Interval>()?;
pub async fn tick(&mut self) {
self.inner.tick().await;
} the trait bound
for<'a> fn(&'a mut time::Interval) -> impl std::future::Future<Output = ()> {
time::Interval::tick
}: rune::function::Function<_, _>
is not satisfied the trait `rune::function::Function<_, _>` is not implemented for fn item
for<'a> fn(&'a mut time::Interval) -> impl std::future::Future<Output = ()> {
time::Interval::tick
} |
Thank you! I'm not sure about For |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're likely to have some clippy issues that needs to be resolved, but looks good. Thank you!
crates/rune-modules/src/time.rs
Outdated
docstring, | ||
runtime::{Mut, VmError, VmResult}, | ||
vm_panic, vm_try, Any, ContextError, Module, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imports are broken up per-module in the rest of the project. It leads to cleaner diffs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current project style would be:
use rune:{docstring, vm_panic, Any, ContextError, Module};
use rune::runtime::{Mut, VmResult};
pub async fn tick(mut internal: Mut<Interval>) { | ||
internal.inner.tick().await; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc for tick will be applied? It doesn't seem right to me
Added methods to convert between the use time::Duration;
let duration = Duration::from_secs(5);
let std_duration = duration.into_std();
let tokio_duration = duration.into_tokio();
let duration_from_str = Duration::from_str(std_duration);
let duration_from_tokio = Duration::from_tokio(tokio_duration); |
So some change is needed to allow a We currently don't seem to support it during compilation because internally we convert it to |
I'd suggest you skip the constants for now until support for constructing them for custom types has been implemented so this and #819 can be merged as-is and further extended once support exists. |
@udoprog ready for review! 😀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, please remove commented out code and maintain in a separate branch instead if you want to keep it around for the future.
b68c00d
to
4ec1c7c
Compare
4ec1c7c
to
15abfa7
Compare
I used this as basis for #861, since Duration uses I also added methods (like |
This PR significantly enhances the time module introducing...
Duration, now public:
Interval:
Instant:
The examples provided were adapted from the Tokio Time library.