From b4e8ace1928acb0b1dc3ec360003f9f9f1f7413e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 1 Oct 2020 10:16:13 +0300 Subject: [PATCH] gstreamer/clock_time: Add From/TryFrom impls to convert between ClockTime and std::time::Duration --- gstreamer/src/clock_time.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/gstreamer/src/clock_time.rs b/gstreamer/src/clock_time.rs index 77da81925..1aa55d56f 100644 --- a/gstreamer/src/clock_time.rs +++ b/gstreamer/src/clock_time.rs @@ -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); @@ -160,3 +161,29 @@ impl glib::StaticType for ClockTime { ::static_type() } } + +impl From 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 for Duration { + type Error = glib::BoolError; + + fn try_from(t: ClockTime) -> Result { + skip_assert_initialized!(); + + t.nanoseconds() + .map(Duration::from_nanos) + .ok_or_else(|| glib_bool_error!("Can't convert ClockTime::NONE to Duration")) + } +}