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

Commit b5b10ca

Browse files
Use Duration instead of u32 when representing milliseconds
1 parent ea407a5 commit b5b10ca

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
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: 5 additions & 4 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 || {

0 commit comments

Comments
 (0)