From 0b4c602c6fc96d530dee4b53c21980f40609975e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 31 Dec 2023 15:49:42 +0200 Subject: [PATCH] Provide a more convenient function for setting other fields on message/event builders Part-of: --- gstreamer-video/src/video_event.rs | 37 ++++++++++------ gstreamer-video/src/video_message.rs | 39 +++++++++++----- gstreamer/src/event.rs | 38 +++++++++++----- gstreamer/src/message.rs | 66 ++++++++++++++++------------ 4 files changed, 116 insertions(+), 64 deletions(-) diff --git a/gstreamer-video/src/video_event.rs b/gstreamer-video/src/video_event.rs index 0ce401ede..b31f3c79f 100644 --- a/gstreamer-video/src/video_event.rs +++ b/gstreamer-video/src/video_event.rs @@ -26,19 +26,28 @@ macro_rules! event_builder_generic_impl { } } + pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + let mut other_fields = self.other_fields; + other_fields.push((name, value.to_send_value())); + + Self { + other_fields, + ..self + } + } + + #[deprecated = "use build.other_field() instead"] pub fn other_fields( self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))], ) -> Self { - Self { - other_fields: self - .other_fields - .iter() - .cloned() - .chain(other_fields.iter().cloned()) - .collect(), - ..self + let mut s = self; + + for (name, value) in other_fields { + s = s.other_field(name, value.to_send_value()); } + + s } #[must_use = "Building the event without using it has no effect"] @@ -61,7 +70,7 @@ macro_rules! event_builder_generic_impl { ); for (k, v) in self.other_fields { - s.set_value(k, v.to_send_value()); + s.set_value(k, v); } } @@ -75,7 +84,7 @@ macro_rules! event_builder_generic_impl { pub struct DownstreamForceKeyUnitEventBuilder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, timestamp: Option, stream_time: Option, running_time: Option, @@ -193,7 +202,7 @@ impl DownstreamForceKeyUnitEvent { pub struct UpstreamForceKeyUnitEventBuilder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, running_time: Option, all_headers: bool, count: u32, @@ -306,7 +315,7 @@ impl ForceKeyUnitEvent { pub struct StillFrameEventBuilder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, in_still: bool, } @@ -364,7 +373,7 @@ macro_rules! nav_event_builder { pub struct $builder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, $($field_names: $field_types,)* #[cfg(feature = "v1_22")] #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))] @@ -495,7 +504,7 @@ nav_event_builder!( pub struct CommandEventBuilder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, command: NavigationCommand, #[cfg(feature = "v1_22")] #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))] diff --git a/gstreamer-video/src/video_message.rs b/gstreamer-video/src/video_message.rs index 1f162acf8..2704500bd 100644 --- a/gstreamer-video/src/video_message.rs +++ b/gstreamer-video/src/video_message.rs @@ -29,6 +29,14 @@ macro_rules! message_builder_generic_impl { } } + pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + Self { + builder: self.builder.other_field(name, value), + ..self + } + } + + #[deprecated = "use builder.other_field() instead"] #[allow(clippy::needless_update)] pub fn other_fields( self, @@ -59,7 +67,7 @@ macro_rules! message_builder_generic_impl { gst::StructureRef::from_glib_borrow_mut(structure as *mut _); for (k, v) in self.builder.other_fields { - structure.set_value(k, v.to_send_value()); + structure.set_value(k, v); } } } @@ -71,10 +79,9 @@ macro_rules! message_builder_generic_impl { } struct MessageBuilder<'a> { - pub src: Option, - pub seqnum: Option, - #[allow(unused)] - pub other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + src: Option, + seqnum: Option, + other_fields: Vec<(&'a str, glib::SendValue)>, } impl<'a> MessageBuilder<'a> { @@ -101,17 +108,25 @@ impl<'a> MessageBuilder<'a> { } } - pub fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + let mut other_fields = self.other_fields; + other_fields.push((name, value.to_send_value())); + Self { - other_fields: self - .other_fields - .iter() - .cloned() - .chain(other_fields.iter().cloned()) - .collect(), + other_fields, ..self } } + + fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + let mut s = self; + + for (name, value) in other_fields { + s = s.other_field(name, value.to_send_value()); + } + + s + } } #[must_use = "The builder must be built to be used"] diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index db4e3325b..4b527d456 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -2040,7 +2040,7 @@ impl std::fmt::Debug for Other { struct EventBuilder<'a> { seqnum: Option, running_time_offset: Option, - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, } impl<'a> EventBuilder<'a> { @@ -2066,17 +2066,25 @@ impl<'a> EventBuilder<'a> { } } - fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + let mut other_fields = self.other_fields; + other_fields.push((name, value.to_send_value())); + Self { - other_fields: self - .other_fields - .iter() - .cloned() - .chain(other_fields.iter().cloned()) - .collect(), + other_fields, ..self } } + + fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + let mut s = self; + + for (name, value) in other_fields { + s = s.other_field(name, value.to_send_value()); + } + + s + } } macro_rules! event_builder_generic_impl { @@ -2099,6 +2107,15 @@ macro_rules! event_builder_generic_impl { } } + #[allow(clippy::needless_update)] + pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + Self { + builder: self.builder.other_field(name, value), + ..self + } + } + + #[deprecated = "use build.other_field() instead"] #[allow(clippy::needless_update)] pub fn other_fields( self, @@ -2129,7 +2146,7 @@ macro_rules! event_builder_generic_impl { )); for (k, v) in self.builder.other_fields { - s.set_value(k, v.to_send_value()); + s.set_value(k, v); } } @@ -2947,6 +2964,7 @@ mod tests { use super::*; #[test] + #[allow(deprecated)] fn test_simple() { crate::init().unwrap(); @@ -2975,7 +2993,7 @@ mod tests { // Event with arguments let flush_stop_evt = FlushStop::builder(true) - .other_fields(&[("extra-field", &true)]) + .other_field("extra-field", true) .build(); match flush_stop_evt.view() { EventView::FlushStop(flush_stop_evt) => { diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 60eb5600b..923168fee 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -2474,8 +2474,7 @@ impl std::fmt::Debug for InstantRateRequest { struct MessageBuilder<'a> { src: Option, seqnum: Option, - #[allow(unused)] - other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>, + other_fields: Vec<(&'a str, glib::SendValue)>, } impl<'a> MessageBuilder<'a> { @@ -2501,17 +2500,25 @@ impl<'a> MessageBuilder<'a> { } } - fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + let mut other_fields = self.other_fields; + other_fields.push((name, value.to_send_value())); + Self { - other_fields: self - .other_fields - .iter() - .cloned() - .chain(other_fields.iter().cloned()) - .collect(), + other_fields, ..self } } + + fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self { + let mut s = self; + + for (name, value) in other_fields { + s = s.other_field(name, value.to_send_value()); + } + + s + } } macro_rules! message_builder_generic_impl { @@ -2533,6 +2540,15 @@ macro_rules! message_builder_generic_impl { } } + #[allow(clippy::needless_update)] + pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self { + Self { + builder: self.builder.other_field(name, value), + ..self + } + } + + #[deprecated = "use build.other_field() instead"] #[allow(clippy::needless_update)] pub fn other_fields( self, @@ -2561,7 +2577,7 @@ macro_rules! message_builder_generic_impl { let structure = StructureRef::from_glib_borrow_mut(structure as *mut _); for (k, v) in self.builder.other_fields { - structure.set_value(k, v.to_send_value()); + structure.set_value(k, v); } } } @@ -2997,7 +3013,7 @@ pub struct StreamStatusBuilder<'a> { builder: MessageBuilder<'a>, type_: crate::StreamStatusType, owner: &'a crate::Element, - status_object: Option<&'a (dyn glib::ToSendValue + Sync)>, + status_object: Option, } impl<'a> StreamStatusBuilder<'a> { @@ -3011,9 +3027,9 @@ impl<'a> StreamStatusBuilder<'a> { } } - pub fn status_object(self, status_object: &'a (dyn glib::ToSendValue + Sync)) -> Self { + pub fn status_object(self, status_object: impl ToSendValue) -> Self { Self { - status_object: Some(status_object), + status_object: Some(status_object.to_send_value()), ..self } } @@ -3021,11 +3037,8 @@ impl<'a> StreamStatusBuilder<'a> { message_builder_generic_impl!(|s: &mut Self, src| { let msg = ffi::gst_message_new_stream_status(src, s.type_.into_glib(), s.owner.to_glib_none().0); - if let Some(status_object) = s.status_object { - ffi::gst_message_set_stream_status_object( - msg, - status_object.to_send_value().to_glib_none().0, - ); + if let Some(ref status_object) = s.status_object { + ffi::gst_message_set_stream_status_object(msg, status_object.to_glib_none().0); } msg }); @@ -3533,7 +3546,7 @@ impl<'a> DeviceRemovedBuilder<'a> { pub struct PropertyNotifyBuilder<'a> { builder: MessageBuilder<'a>, property_name: &'a str, - value: Option<&'a (dyn glib::ToSendValue + Sync)>, + value: Option, } impl<'a> PropertyNotifyBuilder<'a> { @@ -3546,23 +3559,19 @@ impl<'a> PropertyNotifyBuilder<'a> { } } - pub fn value(self, value: &'a (dyn glib::ToSendValue + Sync)) -> Self { + pub fn value(self, value: impl ToSendValue) -> Self { Self { - value: Some(value), + value: Some(value.to_send_value()), ..self } } message_builder_generic_impl!(|s: &mut Self, src| { - let val = s.value.map(|v| v.to_send_value()); + let v = s.value.take(); ffi::gst_message_new_property_notify( src, s.property_name.to_glib_none().0, - mut_override( - val.as_ref() - .map(|v| v.to_glib_none().0) - .unwrap_or(ptr::null()), - ), + v.as_ref().map(|v| v.as_ptr()).unwrap_or(ptr::null_mut()), ) }); } @@ -3793,6 +3802,7 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_other_fields() { crate::init().unwrap(); @@ -3812,7 +3822,7 @@ mod tests { } let buffering_msg = Buffering::builder(42) - .other_fields(&[("extra-field", &true)]) + .other_field("extra-field", true) .build(); match buffering_msg.view() { MessageView::Buffering(buffering_msg) => {