Commit da924e5d authored by François Laignel's avatar François Laignel Committed by Sebastian Dröge

Event & Message: factorize common attributes in generic builders

Concrete events and messages share common attributes which can be factorized in generic builder `struct`s. This reduces noise in the concrete implementations.
parent 2d1218e6
...@@ -873,27 +873,64 @@ declare_concrete_event!(CustomDownstreamSticky); ...@@ -873,27 +873,64 @@ declare_concrete_event!(CustomDownstreamSticky);
declare_concrete_event!(CustomBoth); declare_concrete_event!(CustomBoth);
declare_concrete_event!(CustomBothOob); declare_concrete_event!(CustomBothOob);
struct EventBuilder<'a> {
seqnum: Option<Seqnum>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
}
impl<'a> EventBuilder<'a> {
fn new() -> Self {
Self {
seqnum: None,
running_time_offset: None,
other_fields: Vec::new()
}
}
fn seqnum(self, seqnum: Seqnum) -> Self {
Self {
seqnum: Some(seqnum),
.. self
}
}
fn running_time_offset(self, running_time_offset: i64) -> Self {
Self {
running_time_offset: Some(running_time_offset),
.. self
}
}
fn other_fields(self, other_fields: &[(&'a str, &'a ToSendValue)]) -> Self {
Self {
other_fields: self.other_fields.iter().cloned()
.chain(other_fields.iter().cloned())
.collect(),
.. self
}
}
}
macro_rules! event_builder_generic_impl { macro_rules! event_builder_generic_impl {
($new_fn:expr) => { ($new_fn:expr) => {
pub fn seqnum(self, seqnum: Seqnum) -> Self { pub fn seqnum(self, seqnum: Seqnum) -> Self {
Self { Self {
seqnum: Some(seqnum), builder: self.builder.seqnum(seqnum),
.. self .. self
} }
} }
pub fn running_time_offset(self, running_time_offset: i64) -> Self { pub fn running_time_offset(self, running_time_offset: i64) -> Self {
Self { Self {
running_time_offset: Some(running_time_offset), builder: self.builder.running_time_offset(running_time_offset),
.. self .. self
} }
} }
pub fn other_fields(self, other_fields: &[(&'a str, &'a ToSendValue)]) -> Self { pub fn other_fields(self, other_fields: &[(&'a str, &'a ToSendValue)]) -> Self {
Self { Self {
other_fields: self.other_fields.iter().cloned() builder: self.builder.other_fields(other_fields),
.chain(other_fields.iter().cloned())
.collect(),
.. self .. self
} }
} }
...@@ -902,20 +939,20 @@ macro_rules! event_builder_generic_impl { ...@@ -902,20 +939,20 @@ macro_rules! event_builder_generic_impl {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
let event = $new_fn(&mut self); let event = $new_fn(&mut self);
if let Some(seqnum) = self.seqnum { if let Some(seqnum) = self.builder.seqnum {
ffi::gst_event_set_seqnum(event, seqnum.to_glib()); ffi::gst_event_set_seqnum(event, seqnum.to_glib());
} }
if let Some(running_time_offset) = self.running_time_offset { if let Some(running_time_offset) = self.builder.running_time_offset {
ffi::gst_event_set_running_time_offset(event, running_time_offset); ffi::gst_event_set_running_time_offset(event, running_time_offset);
} }
if !self.other_fields.is_empty() { if !self.builder.other_fields.is_empty() {
let s = StructureRef::from_glib_borrow_mut( let s = StructureRef::from_glib_borrow_mut(
ffi::gst_event_writable_structure(event) ffi::gst_event_writable_structure(event)
); );
for (k, v) in self.other_fields { for (k, v) in self.builder.other_fields {
s.set_value(k, v.to_send_value()); s.set_value(k, v.to_send_value());
} }
} }
...@@ -927,17 +964,13 @@ macro_rules! event_builder_generic_impl { ...@@ -927,17 +964,13 @@ macro_rules! event_builder_generic_impl {
} }
pub struct FlushStartBuilder<'a> { pub struct FlushStartBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
} }
impl<'a> FlushStartBuilder<'a> { impl<'a> FlushStartBuilder<'a> {
fn new() -> Self { fn new() -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
} }
} }
...@@ -945,18 +978,14 @@ impl<'a> FlushStartBuilder<'a> { ...@@ -945,18 +978,14 @@ impl<'a> FlushStartBuilder<'a> {
} }
pub struct FlushStopBuilder<'a> { pub struct FlushStopBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
reset_time: bool, reset_time: bool,
} }
impl<'a> FlushStopBuilder<'a> { impl<'a> FlushStopBuilder<'a> {
fn new(reset_time: bool) -> Self { fn new(reset_time: bool) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
reset_time: reset_time, reset_time: reset_time,
} }
} }
...@@ -965,9 +994,7 @@ impl<'a> FlushStopBuilder<'a> { ...@@ -965,9 +994,7 @@ impl<'a> FlushStopBuilder<'a> {
} }
pub struct StreamStartBuilder<'a> { pub struct StreamStartBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
stream_id: &'a str, stream_id: &'a str,
flags: Option<::StreamFlags>, flags: Option<::StreamFlags>,
group_id: Option<GroupId>, group_id: Option<GroupId>,
...@@ -976,9 +1003,7 @@ impl<'a> StreamStartBuilder<'a> { ...@@ -976,9 +1003,7 @@ impl<'a> StreamStartBuilder<'a> {
fn new(stream_id: &'a str) -> Self { fn new(stream_id: &'a str) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
stream_id: stream_id, stream_id: stream_id,
flags: None, flags: None,
group_id: None, group_id: None,
...@@ -1012,18 +1037,14 @@ impl<'a> StreamStartBuilder<'a> { ...@@ -1012,18 +1037,14 @@ impl<'a> StreamStartBuilder<'a> {
} }
pub struct CapsBuilder<'a> { pub struct CapsBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
caps: &'a ::Caps, caps: &'a ::Caps,
} }
impl<'a> CapsBuilder<'a> { impl<'a> CapsBuilder<'a> {
fn new(caps: &'a ::Caps) -> Self { fn new(caps: &'a ::Caps) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
caps: caps, caps: caps,
} }
} }
...@@ -1032,18 +1053,14 @@ impl<'a> CapsBuilder<'a> { ...@@ -1032,18 +1053,14 @@ impl<'a> CapsBuilder<'a> {
} }
pub struct SegmentBuilder<'a> { pub struct SegmentBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
segment: &'a ::Segment, segment: &'a ::Segment,
} }
impl<'a> SegmentBuilder<'a> { impl<'a> SegmentBuilder<'a> {
fn new(segment: &'a ::Segment) -> Self { fn new(segment: &'a ::Segment) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
segment: segment, segment: segment,
} }
} }
...@@ -1053,9 +1070,7 @@ impl<'a> SegmentBuilder<'a> { ...@@ -1053,9 +1070,7 @@ impl<'a> SegmentBuilder<'a> {
#[cfg(any(feature = "v1_10", feature = "dox"))] #[cfg(any(feature = "v1_10", feature = "dox"))]
pub struct StreamCollectionBuilder<'a> { pub struct StreamCollectionBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
stream_collection: &'a ::StreamCollection, stream_collection: &'a ::StreamCollection,
} }
#[cfg(any(feature = "v1_10", feature = "dox"))] #[cfg(any(feature = "v1_10", feature = "dox"))]
...@@ -1063,9 +1078,7 @@ impl<'a> StreamCollectionBuilder<'a> { ...@@ -1063,9 +1078,7 @@ impl<'a> StreamCollectionBuilder<'a> {
fn new(stream_collection: &'a ::StreamCollection) -> Self { fn new(stream_collection: &'a ::StreamCollection) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
stream_collection: stream_collection, stream_collection: stream_collection,
} }
} }
...@@ -1076,18 +1089,14 @@ impl<'a> StreamCollectionBuilder<'a> { ...@@ -1076,18 +1089,14 @@ impl<'a> StreamCollectionBuilder<'a> {
} }
pub struct TagBuilder<'a> { pub struct TagBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
tags: Option<::TagList>, tags: Option<::TagList>,
} }
impl<'a> TagBuilder<'a> { impl<'a> TagBuilder<'a> {
fn new(tags: ::TagList) -> Self { fn new(tags: ::TagList) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
tags: Some(tags), tags: Some(tags),
} }
} }
...@@ -1099,9 +1108,7 @@ impl<'a> TagBuilder<'a> { ...@@ -1099,9 +1108,7 @@ impl<'a> TagBuilder<'a> {
} }
pub struct BufferSizeBuilder<'a> { pub struct BufferSizeBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
minsize: GenericFormattedValue, minsize: GenericFormattedValue,
maxsize: GenericFormattedValue, maxsize: GenericFormattedValue,
async: bool, async: bool,
...@@ -1110,9 +1117,7 @@ impl<'a> BufferSizeBuilder<'a> { ...@@ -1110,9 +1117,7 @@ impl<'a> BufferSizeBuilder<'a> {
fn new(minsize: GenericFormattedValue, maxsize: GenericFormattedValue, async: bool) -> Self { fn new(minsize: GenericFormattedValue, maxsize: GenericFormattedValue, async: bool) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
minsize: minsize, minsize: minsize,
maxsize: maxsize, maxsize: maxsize,
async: async, async: async,
...@@ -1128,9 +1133,7 @@ impl<'a> BufferSizeBuilder<'a> { ...@@ -1128,9 +1133,7 @@ impl<'a> BufferSizeBuilder<'a> {
} }
pub struct SinkMessageBuilder<'a> { pub struct SinkMessageBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
name: &'a str, name: &'a str,
msg: &'a ::Message, msg: &'a ::Message,
} }
...@@ -1138,9 +1141,7 @@ impl<'a> SinkMessageBuilder<'a> { ...@@ -1138,9 +1141,7 @@ impl<'a> SinkMessageBuilder<'a> {
fn new(name: &'a str, msg: &'a ::Message) -> Self { fn new(name: &'a str, msg: &'a ::Message) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
name: name, name: name,
msg: msg, msg: msg,
} }
...@@ -1154,9 +1155,7 @@ impl<'a> SinkMessageBuilder<'a> { ...@@ -1154,9 +1155,7 @@ impl<'a> SinkMessageBuilder<'a> {
#[cfg(any(feature = "v1_10", feature = "dox"))] #[cfg(any(feature = "v1_10", feature = "dox"))]
pub struct StreamGroupDoneBuilder<'a> { pub struct StreamGroupDoneBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
group_id: GroupId, group_id: GroupId,
} }
#[cfg(any(feature = "v1_10", feature = "dox"))] #[cfg(any(feature = "v1_10", feature = "dox"))]
...@@ -1164,9 +1163,7 @@ impl<'a> StreamGroupDoneBuilder<'a> { ...@@ -1164,9 +1163,7 @@ impl<'a> StreamGroupDoneBuilder<'a> {
fn new(group_id: GroupId) -> Self { fn new(group_id: GroupId) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
group_id: group_id, group_id: group_id,
} }
} }
...@@ -1177,17 +1174,13 @@ impl<'a> StreamGroupDoneBuilder<'a> { ...@@ -1177,17 +1174,13 @@ impl<'a> StreamGroupDoneBuilder<'a> {
} }
pub struct EosBuilder<'a> { pub struct EosBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
} }
impl<'a> EosBuilder<'a> { impl<'a> EosBuilder<'a> {
fn new() -> Self { fn new() -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
} }
} }
...@@ -1195,9 +1188,7 @@ impl<'a> EosBuilder<'a> { ...@@ -1195,9 +1188,7 @@ impl<'a> EosBuilder<'a> {
} }
pub struct TocBuilder<'a> { pub struct TocBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
toc: &'a ::Toc, toc: &'a ::Toc,
updated: bool, updated: bool,
} }
...@@ -1205,9 +1196,7 @@ impl<'a> TocBuilder<'a> { ...@@ -1205,9 +1196,7 @@ impl<'a> TocBuilder<'a> {
fn new(toc: &'a ::Toc, updated: bool) -> Self { fn new(toc: &'a ::Toc, updated: bool) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
toc: toc, toc: toc,
updated: updated, updated: updated,
} }
...@@ -1220,9 +1209,7 @@ impl<'a> TocBuilder<'a> { ...@@ -1220,9 +1209,7 @@ impl<'a> TocBuilder<'a> {
} }
pub struct ProtectionBuilder<'a> { pub struct ProtectionBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
system_id: &'a str, system_id: &'a str,
data: &'a ::Buffer, data: &'a ::Buffer,
origin: Option<&'a str>, origin: Option<&'a str>,
...@@ -1231,9 +1218,7 @@ impl<'a> ProtectionBuilder<'a> { ...@@ -1231,9 +1218,7 @@ impl<'a> ProtectionBuilder<'a> {
fn new(system_id: &'a str, data: &'a ::Buffer) -> Self { fn new(system_id: &'a str, data: &'a ::Buffer) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
system_id: system_id, system_id: system_id,
data: data, data: data,
origin: None, origin: None,
...@@ -1255,18 +1240,14 @@ impl<'a> ProtectionBuilder<'a> { ...@@ -1255,18 +1240,14 @@ impl<'a> ProtectionBuilder<'a> {
} }
pub struct SegmentDoneBuilder<'a> { pub struct SegmentDoneBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
position: GenericFormattedValue, position: GenericFormattedValue,
} }
impl<'a> SegmentDoneBuilder<'a> { impl<'a> SegmentDoneBuilder<'a> {
fn new(position: GenericFormattedValue) -> Self { fn new(position: GenericFormattedValue) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
position: position, position: position,
} }
} }
...@@ -1278,9 +1259,7 @@ impl<'a> SegmentDoneBuilder<'a> { ...@@ -1278,9 +1259,7 @@ impl<'a> SegmentDoneBuilder<'a> {
} }
pub struct GapBuilder<'a> { pub struct GapBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
timestamp: ::ClockTime, timestamp: ::ClockTime,
duration: ::ClockTime, duration: ::ClockTime,
} }
...@@ -1288,9 +1267,7 @@ impl<'a> GapBuilder<'a> { ...@@ -1288,9 +1267,7 @@ impl<'a> GapBuilder<'a> {
fn new(timestamp: ::ClockTime, duration: ::ClockTime) -> Self { fn new(timestamp: ::ClockTime, duration: ::ClockTime) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
timestamp: timestamp, timestamp: timestamp,
duration: duration, duration: duration,
} }
...@@ -1303,9 +1280,7 @@ impl<'a> GapBuilder<'a> { ...@@ -1303,9 +1280,7 @@ impl<'a> GapBuilder<'a> {
} }
pub struct QosBuilder<'a> { pub struct QosBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
type_: ::QOSType, type_: ::QOSType,
proportion: f64, proportion: f64,
diff: i64, diff: i64,
...@@ -1315,9 +1290,7 @@ impl<'a> QosBuilder<'a> { ...@@ -1315,9 +1290,7 @@ impl<'a> QosBuilder<'a> {
fn new(type_: ::QOSType, proportion: f64, diff: i64, timestamp: ::ClockTime) -> Self { fn new(type_: ::QOSType, proportion: f64, diff: i64, timestamp: ::ClockTime) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
Self { Self {
seqnum: None, builder: EventBuilder::new(),
running_time_offset: None,
other_fields: Vec::new(),
type_: type_, type_: type_,
proportion: proportion, proportion: proportion,
diff: diff, diff: diff,
...@@ -1334,9 +1307,7 @@ impl<'a> QosBuilder<'a> { ...@@ -1334,9 +1307,7 @@ impl<'a> QosBuilder<'a> {
} }
pub struct SeekBuilder<'a> { pub struct SeekBuilder<'a> {
seqnum: Option<Seqnum>, builder: EventBuilder<'a>,
running_time_offset: Option<i64>,
other_fields: Vec<(&'a str, &'a ToSendValue)>,
rate: f64, rate: f64,
flags: ::SeekFlags, flags: ::SeekFlags,
start_type: ::SeekType, start_type: ::SeekType,
...@@ -1355,9 +1326,7 @@ impl<'a> SeekBuilder<'a> { ...@@ -1355,9 +1326,7 @@ impl<'a> SeekBuilder<'a> {
) -> Self { ) -> Self {