mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-14 13:31:09 +00:00
gstreamer/clock_time: Add From/TryFrom impls to convert between ClockTime and std::time::Duration
This commit is contained in:
parent
5d80372dd3
commit
b4e8ace192
1 changed files with 28 additions and 1 deletions
|
@ -9,7 +9,8 @@
|
|||
use glib;
|
||||
use glib::translate::*;
|
||||
use gst_sys;
|
||||
use std::{cmp, fmt};
|
||||
use std::time::Duration;
|
||||
use std::{cmp, convert, fmt};
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
|
||||
pub struct ClockTime(pub Option<u64>);
|
||||
|
@ -160,3 +161,29 @@ impl glib::StaticType for ClockTime {
|
|||
<u64 as glib::StaticType>::static_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Duration> for ClockTime {
|
||||
fn from(d: Duration) -> Self {
|
||||
skip_assert_initialized!();
|
||||
|
||||
let nanos = d.as_nanos();
|
||||
|
||||
if nanos > std::u64::MAX as u128 {
|
||||
::CLOCK_TIME_NONE
|
||||
} else {
|
||||
ClockTime::from_nseconds(nanos as u64)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl convert::TryFrom<ClockTime> for Duration {
|
||||
type Error = glib::BoolError;
|
||||
|
||||
fn try_from(t: ClockTime) -> Result<Self, Self::Error> {
|
||||
skip_assert_initialized!();
|
||||
|
||||
t.nanoseconds()
|
||||
.map(Duration::from_nanos)
|
||||
.ok_or_else(|| glib_bool_error!("Can't convert ClockTime::NONE to Duration"))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue