From 20ac231146cde04487e3aaec410ba4c2992e5117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 27 Jun 2022 22:19:42 +0300 Subject: [PATCH] gstreamer: Allow borrowing `Segment`s from `Value`s without copying --- gstreamer-base/src/aggregator.rs | 2 +- gstreamer/src/sample.rs | 2 +- gstreamer/src/segment.rs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/gstreamer-base/src/aggregator.rs b/gstreamer-base/src/aggregator.rs index eee4d91c3..8ac0dce86 100644 --- a/gstreamer-base/src/aggregator.rs +++ b/gstreamer-base/src/aggregator.rs @@ -245,7 +245,7 @@ impl> AggregatorExtManual for O { let f: &F = &*(f as *const F); f( Aggregator::from_glib_borrow(this).unsafe_cast_ref(), - &gst::Segment::from_glib_borrow(segment), + gst::Segment::from_ptr(segment), from_glib(pts), from_glib(dts), from_glib(duration), diff --git a/gstreamer/src/sample.rs b/gstreamer/src/sample.rs index 2208867cf..bd02d2dbc 100644 --- a/gstreamer/src/sample.rs +++ b/gstreamer/src/sample.rs @@ -173,7 +173,7 @@ impl SampleRef { if ptr.is_null() { None } else { - Some(&*(ptr as *const crate::Segment)) + Some(crate::Segment::from_ptr(ptr)) } } } diff --git a/gstreamer/src/segment.rs b/gstreamer/src/segment.rs index 625bdff67..f4e1e926d 100644 --- a/gstreamer/src/segment.rs +++ b/gstreamer/src/segment.rs @@ -18,6 +18,11 @@ pub type Segment = FormattedSegment; pub struct FormattedSegment(ffi::GstSegment, PhantomData); impl Segment { + pub unsafe fn from_ptr<'a>(ptr: *const ffi::GstSegment) -> &'a Segment { + assert!(!ptr.is_null()); + &*(ptr as *const Self) + } + pub fn reset_with_format(&mut self, format: Format) { unsafe { ffi::gst_segment_init(self.to_glib_none_mut().0, format.into_glib()); @@ -649,6 +654,18 @@ unsafe impl<'a> glib::value::FromValue<'a> for Segment { } } +#[doc(hidden)] +unsafe impl<'a> glib::value::FromValue<'a> for &'a Segment { + type Checker = glib::value::GenericValueTypeOrNoneChecker; + + unsafe fn from_value(value: &'a glib::Value) -> Self { + skip_assert_initialized!(); + Segment::from_ptr( + glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *const ffi::GstSegment + ) + } +} + #[doc(hidden)] impl glib::value::ToValue for FormattedSegment { fn to_value(&self) -> glib::Value {