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.
This commit is contained in:
Sebastian Dröge 2021-10-18 16:31:46 +03:00
parent f315d3a052
commit e6b78d1489

View file

@ -100,17 +100,15 @@ impl Structure {
} }
#[allow(clippy::should_implement_trait)] #[allow(clippy::should_implement_trait)]
pub fn from_iter<'a, 'b>( pub fn from_iter<'a>(
name: &str, name: &str,
iter: impl IntoIterator<Item = (&'a str, &'b SendValue)>, iter: impl IntoIterator<Item = (&'a str, SendValue)>,
) -> Structure { ) -> Structure {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
let mut structure = Structure::new_empty(name); let mut structure = Structure::new_empty(name);
iter.into_iter().for_each(|(f, v)| unsafe { iter.into_iter()
let mut value = v.clone().into_raw(); .for_each(|(f, v)| structure.set_value(f, v));
ffi::gst_structure_take_value(structure.0.as_mut(), f.to_glib_none().0, &mut value);
});
structure structure
} }
@ -446,8 +444,11 @@ impl StructureRef {
pub fn set_value(&mut self, name: &str, value: SendValue) { pub fn set_value(&mut self, name: &str, value: SendValue) {
unsafe { unsafe {
let mut value = value.into_raw(); ffi::gst_structure_take_value(
ffi::gst_structure_take_value(&mut self.0, name.to_glib_none().0, &mut 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) { pub fn set_value_by_quark(&mut self, name: glib::Quark, value: SendValue) {
unsafe { unsafe {
let mut value = value.into_raw(); ffi::gst_structure_id_take_value(&mut self.0, name.into_glib(), &mut value.into_raw());
ffi::gst_structure_id_take_value(&mut self.0, name.into_glib(), &mut value);
} }
} }
@ -979,7 +979,12 @@ mod tests {
.field("f3", &123i32) .field("f3", &123i32)
.build(); .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.name(), "test");
assert_eq!(s2.get::<&str>("f1"), Ok("abc")); assert_eq!(s2.get::<&str>("f1"), Ok("abc"));