Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 7e9eee4

Browse files
Merge pull request #693 from GuillaumeGomez/duration-for-milliseconds
Use Duration instead of u32 when representing milliseconds
2 parents 667635d + 3f68580 commit 7e9eee4

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/source.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::mem::transmute;
1010
use std::num::NonZeroU32;
1111
#[cfg(unix)]
1212
use std::os::unix::io::RawFd;
13+
use std::time::Duration;
1314
use translate::{from_glib, from_glib_full, FromGlib, ToGlib, ToGlibPtr};
1415
#[cfg(any(unix, feature = "dox"))]
1516
use IOCondition;
@@ -197,14 +198,14 @@ where
197198
///
198199
/// The default main loop almost always is the main loop of the main thread.
199200
/// Thus the closure is called on the main thread.
200-
pub fn timeout_add<F>(interval: u32, func: F) -> SourceId
201+
pub fn timeout_add<F>(interval: Duration, func: F) -> SourceId
201202
where
202203
F: FnMut() -> Continue + Send + 'static,
203204
{
204205
unsafe {
205206
from_glib(glib_sys::g_timeout_add_full(
206207
glib_sys::G_PRIORITY_DEFAULT,
207-
interval,
208+
interval.as_millis() as _,
208209
Some(trampoline::<F>),
209210
into_raw(func),
210211
Some(destroy_closure::<F>),
@@ -228,15 +229,15 @@ where
228229
///
229230
/// This function panics if called from a different thread than the one that
230231
/// owns the main context.
231-
pub fn timeout_add_local<F>(interval: u32, func: F) -> SourceId
232+
pub fn timeout_add_local<F>(interval: Duration, func: F) -> SourceId
232233
where
233234
F: FnMut() -> Continue + 'static,
234235
{
235236
unsafe {
236237
assert!(MainContext::default().is_owner());
237238
from_glib(glib_sys::g_timeout_add_full(
238239
glib_sys::G_PRIORITY_DEFAULT,
239-
interval,
240+
interval.as_millis() as _,
240241
Some(trampoline::<F>),
241242
into_raw(func),
242243
Some(destroy_closure::<F>),
@@ -536,7 +537,7 @@ where
536537
/// be delayed by other events. Prefer `timeout_add_seconds` when millisecond
537538
/// precision is not necessary.
538539
pub fn timeout_source_new<F>(
539-
interval: u32,
540+
interval: Duration,
540541
name: Option<&str>,
541542
priority: Priority,
542543
func: F,
@@ -545,7 +546,7 @@ where
545546
F: FnMut() -> Continue + Send + 'static,
546547
{
547548
unsafe {
548-
let source = glib_sys::g_timeout_source_new(interval);
549+
let source = glib_sys::g_timeout_source_new(interval.as_millis() as _);
549550
glib_sys::g_source_set_callback(
550551
source,
551552
Some(trampoline::<F>),

src/source_futures.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use futures_util::stream::StreamExt;
1212
use std::marker::Unpin;
1313
use std::pin;
1414
use std::pin::Pin;
15+
use std::time::Duration;
1516

1617
use Continue;
1718
use MainContext;
@@ -108,7 +109,7 @@ impl<T, F> Drop for SourceFuture<T, F> {
108109
/// Create a `Future` that will resolve after the given number of milliseconds.
109110
///
110111
/// The `Future` must be spawned on an `Executor` backed by a `glib::MainContext`.
111-
pub fn timeout_future(value: u32) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
112+
pub fn timeout_future(value: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
112113
timeout_future_with_priority(::PRIORITY_DEFAULT, value)
113114
}
114115

@@ -117,7 +118,7 @@ pub fn timeout_future(value: u32) -> Pin<Box<dyn Future<Output = ()> + Send + 's
117118
/// The `Future` must be spawned on an `Executor` backed by a `glib::MainContext`.
118119
pub fn timeout_future_with_priority(
119120
priority: Priority,
120-
value: u32,
121+
value: Duration,
121122
) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
122123
Box::pin(SourceFuture::new(move |send| {
123124
let mut send = Some(send);
@@ -295,7 +296,7 @@ impl<T, F> Drop for SourceStream<T, F> {
295296
/// Create a `Stream` that will provide a value every given number of milliseconds.
296297
///
297298
/// The `Future` must be spawned on an `Executor` backed by a `glib::MainContext`.
298-
pub fn interval_stream(value: u32) -> Pin<Box<dyn Stream<Item = ()> + Send + 'static>> {
299+
pub fn interval_stream(value: Duration) -> Pin<Box<dyn Stream<Item = ()> + Send + 'static>> {
299300
interval_stream_with_priority(::PRIORITY_DEFAULT, value)
300301
}
301302

@@ -304,7 +305,7 @@ pub fn interval_stream(value: u32) -> Pin<Box<dyn Stream<Item = ()> + Send + 'st
304305
/// The `Future` must be spawned on an `Executor` backed by a `glib::MainContext`.
305306
pub fn interval_stream_with_priority(
306307
priority: Priority,
307-
value: u32,
308+
value: Duration,
308309
) -> Pin<Box<dyn Stream<Item = ()> + Send + 'static>> {
309310
Box::pin(SourceStream::new(move |send| {
310311
::timeout_source_new(value, None, priority, move || {
@@ -372,13 +373,14 @@ pub fn unix_signal_stream_with_priority(
372373
#[cfg(test)]
373374
mod tests {
374375
use super::*;
376+
use std::time::Duration;
375377
use std::thread;
376378

377379
#[test]
378380
fn test_timeout() {
379381
let c = MainContext::new();
380382

381-
c.block_on(timeout_future(20));
383+
c.block_on(timeout_future(Duration::from_millis(20)));
382384
}
383385

384386
#[test]
@@ -387,7 +389,7 @@ mod tests {
387389
let l = ::MainLoop::new(Some(&c), false);
388390

389391
let l_clone = l.clone();
390-
c.spawn(timeout_future(20).then(move |()| {
392+
c.spawn(timeout_future(Duration::from_millis(20)).then(move |()| {
391393
l_clone.quit();
392394
futures_util::future::ready(())
393395
}));
@@ -404,7 +406,7 @@ mod tests {
404406
{
405407
let count = &mut count;
406408
c.block_on(
407-
interval_stream(20)
409+
interval_stream(Duration::from_millis(20))
408410
.take(2)
409411
.for_each(|()| {
410412
*count += 1;
@@ -422,7 +424,7 @@ mod tests {
422424
fn test_timeout_and_channel() {
423425
let c = MainContext::default();
424426

425-
let res = c.block_on(timeout_future(20).then(|()| {
427+
let res = c.block_on(timeout_future(Duration::from_millis(20)).then(|()| {
426428
let (sender, receiver) = oneshot::channel();
427429

428430
thread::spawn(move || {

0 commit comments

Comments
 (0)