From e6b78d14894df57b02057a48a907db52783fbd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 18 Oct 2021 16:31:46 +0300 Subject: [PATCH] gstreamer: Have Structure::from_iter() take owned values They were copied internally, which can be avoided in many cases if working with owned values anyway. --- gstreamer/src/structure.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index 9018f5873..4faa5933e 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -100,17 +100,15 @@ impl Structure { } #[allow(clippy::should_implement_trait)] - pub fn from_iter<'a, 'b>( + pub fn from_iter<'a>( name: &str, - iter: impl IntoIterator, + iter: impl IntoIterator, ) -> Structure { assert_initialized_main_thread!(); let mut structure = Structure::new_empty(name); - iter.into_iter().for_each(|(f, v)| unsafe { - let mut value = v.clone().into_raw(); - ffi::gst_structure_take_value(structure.0.as_mut(), f.to_glib_none().0, &mut value); - }); + iter.into_iter() + .for_each(|(f, v)| structure.set_value(f, v)); structure } @@ -446,8 +444,11 @@ impl StructureRef { pub fn set_value(&mut self, name: &str, value: SendValue) { unsafe { - let mut value = value.into_raw(); - ffi::gst_structure_take_value(&mut self.0, name.to_glib_none().0, &mut value); + ffi::gst_structure_take_value( + &mut self.0, + name.to_glib_none().0, + &mut value.into_raw(), + ); } } @@ -458,8 +459,7 @@ impl StructureRef { pub fn set_value_by_quark(&mut self, name: glib::Quark, value: SendValue) { unsafe { - let mut value = value.into_raw(); - ffi::gst_structure_id_take_value(&mut self.0, name.into_glib(), &mut value); + ffi::gst_structure_id_take_value(&mut self.0, name.into_glib(), &mut value.into_raw()); } } @@ -979,7 +979,12 @@ mod tests { .field("f3", &123i32) .build(); - let s2 = Structure::from_iter(s.name(), s.iter().filter(|(f, _)| *f == "f1")); + let s2 = Structure::from_iter( + s.name(), + s.iter() + .filter(|(f, _)| *f == "f1") + .map(|(f, v)| (f, v.clone())), + ); assert_eq!(s2.name(), "test"); assert_eq!(s2.get::<&str>("f1"), Ok("abc"));