gstreamer/clock_time: Add From/TryFrom impls to convert between ClockTime and std::time::Duration

This commit is contained in:
Sebastian Dröge 2020-10-01 10:16:13 +03:00
parent 5d80372dd3
commit b4e8ace192

View file

@ -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"))
}
}