Provide a more convenient function for setting other fields on message/event builders

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1372>
This commit is contained in:
Sebastian Dröge 2023-12-31 15:49:42 +02:00
parent 3b3c3baee5
commit 0b4c602c6f
4 changed files with 116 additions and 64 deletions

View file

@ -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<gst::Seqnum>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>,
other_fields: Vec<(&'a str, glib::SendValue)>,
timestamp: Option<gst::ClockTime>,
stream_time: Option<gst::ClockTime>,
running_time: Option<gst::ClockTime>,
@ -193,7 +202,7 @@ impl DownstreamForceKeyUnitEvent {
pub struct UpstreamForceKeyUnitEventBuilder<'a> {
seqnum: Option<gst::Seqnum>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>,
other_fields: Vec<(&'a str, glib::SendValue)>,
running_time: Option<gst::ClockTime>,
all_headers: bool,
count: u32,
@ -306,7 +315,7 @@ impl ForceKeyUnitEvent {
pub struct StillFrameEventBuilder<'a> {
seqnum: Option<gst::Seqnum>,
running_time_offset: Option<i64>,
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<gst::Seqnum>,
running_time_offset: Option<i64>,
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<gst::Seqnum>,
running_time_offset: Option<i64>,
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")))]

View file

@ -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<Object>,
pub seqnum: Option<Seqnum>,
#[allow(unused)]
pub other_fields: Vec<(&'a str, &'a (dyn ToSendValue + Sync))>,
src: Option<Object>,
seqnum: Option<Seqnum>,
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"]

View file

@ -2040,7 +2040,7 @@ impl std::fmt::Debug for Other<Event> {
struct EventBuilder<'a> {
seqnum: Option<Seqnum>,
running_time_offset: Option<i64>,
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) => {

View file

@ -2474,8 +2474,7 @@ impl std::fmt::Debug for InstantRateRequest<Message> {
struct MessageBuilder<'a> {
src: Option<Object>,
seqnum: Option<Seqnum>,
#[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<glib::SendValue>,
}
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<glib::SendValue>,
}
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) => {