Add new setters for collection Value setters in Builders

E.g. (also applies to `property`):

* `field_from_iter()`,
* `field_if_not_empty()`.

Use a macro to factorize implementation & documentation of `field` / `property`
convenience setters.

Also:

* add some `*_if_not_empty` for some iterator based setters.
* add `*_if` for predicate based setters.

Related to https://github.com/gtk-rs/gtk-rs-core/pull/1377

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1431>
This commit is contained in:
François Laignel 2024-06-14 17:00:36 +02:00
parent e4e5cfd63b
commit 8527c0e39e
26 changed files with 1883 additions and 141 deletions

View file

@ -72,6 +72,14 @@ impl AppSinkCallbacksBuilder {
}
}
pub fn eos_if<F: FnMut(&AppSink) + Send + 'static>(self, eos: F, predicate: bool) -> Self {
if predicate {
self.eos(eos)
} else {
self
}
}
pub fn eos_if_some<F: FnMut(&AppSink) + Send + 'static>(self, eos: Option<F>) -> Self {
if let Some(eos) = eos {
self.eos(eos)
@ -92,6 +100,20 @@ impl AppSinkCallbacksBuilder {
}
}
pub fn new_preroll_if<
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
>(
self,
new_preroll: F,
predicate: bool,
) -> Self {
if predicate {
self.new_preroll(new_preroll)
} else {
self
}
}
pub fn new_preroll_if_some<
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
>(
@ -117,6 +139,20 @@ impl AppSinkCallbacksBuilder {
}
}
pub fn new_sample_if<
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
>(
self,
new_sample: F,
predicate: bool,
) -> Self {
if predicate {
self.new_sample(new_sample)
} else {
self
}
}
pub fn new_sample_if_some<
F: FnMut(&AppSink) -> Result<gst::FlowSuccess, gst::FlowError> + Send + 'static,
>(
@ -139,6 +175,20 @@ impl AppSinkCallbacksBuilder {
}
}
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
pub fn new_event_if<F: FnMut(&AppSink) -> bool + Send + 'static>(
self,
new_event: F,
predicate: bool,
) -> Self {
if predicate {
self.new_event(new_event)
} else {
self
}
}
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
pub fn new_event_if_some<F: FnMut(&AppSink) -> bool + Send + 'static>(
@ -166,6 +216,22 @@ impl AppSinkCallbacksBuilder {
}
}
#[cfg(feature = "v1_24")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
pub fn propose_allocation_if<
F: FnMut(&AppSink, &mut gst::query::Allocation) -> bool + Send + 'static,
>(
self,
propose_allocation: F,
predicate: bool,
) -> Self {
if predicate {
self.propose_allocation(propose_allocation)
} else {
self
}
}
#[cfg(feature = "v1_24")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
pub fn propose_allocation_if_some<

View file

@ -61,6 +61,18 @@ impl AppSrcCallbacksBuilder {
}
}
pub fn need_data_if<F: FnMut(&AppSrc, u32) + Send + 'static>(
self,
need_data: F,
predicate: bool,
) -> Self {
if predicate {
self.need_data(need_data)
} else {
self
}
}
pub fn need_data_if_some<F: FnMut(&AppSrc, u32) + Send + 'static>(
self,
need_data: Option<F>,
@ -79,6 +91,18 @@ impl AppSrcCallbacksBuilder {
}
}
pub fn enough_data_if<F: Fn(&AppSrc) + Send + Sync + 'static>(
self,
enough_data: F,
predicate: bool,
) -> Self {
if predicate {
self.enough_data(enough_data)
} else {
self
}
}
pub fn enough_data_if_some<F: Fn(&AppSrc) + Send + Sync + 'static>(
self,
enough_data: Option<F>,
@ -100,6 +124,18 @@ impl AppSrcCallbacksBuilder {
}
}
pub fn seek_data_if<F: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>(
self,
seek_data: F,
predicate: bool,
) -> Self {
if predicate {
self.seek_data(seek_data)
} else {
self
}
}
pub fn seek_data_if_some<F: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>(
self,
seek_data: Option<F>,

View file

@ -97,6 +97,18 @@ impl<'a> AudioInfoBuilder<'a> {
}
}
pub fn positions_if(
self,
positions: &'a [crate::AudioChannelPosition],
predicate: bool,
) -> AudioInfoBuilder<'a> {
if predicate {
self.positions(positions)
} else {
self
}
}
pub fn positions_if_some(
self,
positions: Option<&'a [crate::AudioChannelPosition]>,
@ -115,6 +127,14 @@ impl<'a> AudioInfoBuilder<'a> {
}
}
pub fn flags_if(self, flags: crate::AudioFlags, predicate: bool) -> Self {
if predicate {
self.flags(flags)
} else {
self
}
}
pub fn flags_if_some(self, flags: Option<crate::AudioFlags>) -> Self {
if let Some(flags) = flags {
self.flags(flags)
@ -130,6 +150,14 @@ impl<'a> AudioInfoBuilder<'a> {
}
}
pub fn layout_if(self, layout: crate::AudioLayout, predicate: bool) -> Self {
if predicate {
self.layout(layout)
} else {
self
}
}
pub fn layout_if_some(self, layout: Option<crate::AudioLayout>) -> Self {
if let Some(layout) = layout {
self.layout(layout)

View file

@ -90,6 +90,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn format_if(self, format: AudioFormat, predicate: bool) -> Self {
if predicate {
self.format(format)
} else {
self
}
}
pub fn format_if_some(self, format: Option<AudioFormat>) -> Self {
if let Some(format) = format {
self.format(format)
@ -107,6 +115,18 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn format_list_if(
self,
formats: impl IntoIterator<Item = AudioFormat>,
predicate: bool,
) -> Self {
if predicate {
self.format_list(formats)
} else {
self
}
}
pub fn format_list_if_some(
self,
formats: Option<impl IntoIterator<Item = AudioFormat>>,
@ -118,12 +138,29 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn format_list_if_not_empty(self, formats: impl IntoIterator<Item = AudioFormat>) -> Self {
let mut formats = formats.into_iter().peekable();
if formats.peek().is_some() {
self.format_list(formats)
} else {
self
}
}
pub fn rate(self, rate: i32) -> Self {
Self {
builder: self.builder.field(glib::gstr!("rate"), rate),
}
}
pub fn rate_if(self, rate: i32, predicate: bool) -> Self {
if predicate {
self.rate(rate)
} else {
self
}
}
pub fn rate_if_some(self, rate: Option<i32>) -> Self {
if let Some(rate) = rate {
self.rate(rate)
@ -140,6 +177,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn rate_range_if(self, rates: impl RangeBounds<i32>, predicate: bool) -> Self {
if predicate {
self.rate_range(rates)
} else {
self
}
}
pub fn rate_range_if_some(self, rates: Option<impl RangeBounds<i32>>) -> Self {
if let Some(rates) = rates {
self.rate_range(rates)
@ -156,6 +201,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn rate_list_if(self, rates: impl IntoIterator<Item = i32>, predicate: bool) -> Self {
if predicate {
self.rate_list(rates)
} else {
self
}
}
pub fn rate_list_if_some(self, rates: Option<impl IntoIterator<Item = i32>>) -> Self {
if let Some(rates) = rates {
self.rate_list(rates)
@ -164,12 +217,29 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn rate_list_if_not_empty(self, rates: impl IntoIterator<Item = i32>) -> Self {
let mut rates = rates.into_iter().peekable();
if rates.peek().is_some() {
self.rate_list(rates)
} else {
self
}
}
pub fn channels(self, channels: i32) -> Self {
Self {
builder: self.builder.field(glib::gstr!("channels"), channels),
}
}
pub fn channels_if(self, channels: i32, predicate: bool) -> Self {
if predicate {
self.channels(channels)
} else {
self
}
}
pub fn channels_if_some(self, channels: Option<i32>) -> Self {
if let Some(channels) = channels {
self.channels(channels)
@ -186,6 +256,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn channels_range_if(self, channels: impl RangeBounds<i32>, predicate: bool) -> Self {
if predicate {
self.channels_range(channels)
} else {
self
}
}
pub fn channels_range_if_some(self, channels: Option<impl RangeBounds<i32>>) -> Self {
if let Some(channels) = channels {
self.channels_range(channels)
@ -202,6 +280,18 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn channels_list_if(
self,
channels: impl IntoIterator<Item = i32>,
predicate: bool,
) -> Self {
if predicate {
self.channels_list(channels)
} else {
self
}
}
pub fn channels_list_if_some(self, channels: Option<impl IntoIterator<Item = i32>>) -> Self {
if let Some(channels) = channels {
self.channels_list(channels)
@ -210,6 +300,15 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn channels_list_if_not_empty(self, channels: impl IntoIterator<Item = i32>) -> Self {
let mut channels = channels.into_iter().peekable();
if channels.peek().is_some() {
self.channels_list(channels)
} else {
self
}
}
pub fn layout(self, layout: AudioLayout) -> Self {
Self {
builder: self
@ -218,6 +317,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn layout_if(self, layout: AudioLayout, predicate: bool) -> Self {
if predicate {
self.layout(layout)
} else {
self
}
}
pub fn layout_if_some(self, layout: Option<AudioLayout>) -> Self {
if let Some(layout) = layout {
self.layout(layout)
@ -235,6 +342,18 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn layout_list_if(
self,
layouts: impl IntoIterator<Item = AudioLayout>,
predicate: bool,
) -> Self {
if predicate {
self.layout_list(layouts)
} else {
self
}
}
pub fn layout_list_if_some(
self,
layouts: Option<impl IntoIterator<Item = AudioLayout>>,
@ -246,6 +365,15 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn layout_list_if_not_empty(self, layouts: impl IntoIterator<Item = AudioLayout>) -> Self {
let mut layouts = layouts.into_iter().peekable();
if layouts.peek().is_some() {
self.layout_list(layouts)
} else {
self
}
}
pub fn channel_mask(self, channel_mask: u64) -> Self {
Self {
builder: self
@ -254,6 +382,14 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn channel_mask_if(self, channel_mask: u64, predicate: bool) -> Self {
if predicate {
self.channel_mask(channel_mask)
} else {
self
}
}
pub fn channel_mask_if_some(self, channel_mask: Option<u64>) -> Self {
if let Some(channel_mask) = channel_mask {
self.channel_mask(channel_mask)
@ -275,19 +411,18 @@ impl<T> AudioCapsBuilder<T> {
}
}
pub fn field(self, name: &str, value: impl Into<glib::Value> + Send) -> Self {
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn field(self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) -> Self {
Self {
builder: self.builder.field(name, value),
}
}
pub fn field_if_some(self, name: &str, value: Option<impl Into<glib::Value> + Send>) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(field);
#[must_use]
pub fn build(self) -> gst::Caps {
@ -321,7 +456,7 @@ fn layout_str(layout: AudioLayout) -> &'static glib::GStr {
#[cfg(test)]
mod tests {
use super::AudioCapsBuilder;
use super::{AudioCapsBuilder, AudioFormat};
#[test]
fn default_encoding() {
@ -336,4 +471,42 @@ mod tests {
let caps = AudioCapsBuilder::for_encoding("audio/mpeg").build();
assert_eq!(caps.structure(0).unwrap().name(), "audio/mpeg");
}
#[test]
fn format_if() {
gst::init().unwrap();
let formats = [AudioFormat::S24be, AudioFormat::S16be, AudioFormat::U8];
let caps_with_format = AudioCapsBuilder::for_encoding("audio/x-raw")
.format_list(formats)
.build();
assert!(caps_with_format
.structure(0)
.unwrap()
.get::<gst::List>("format")
.unwrap()
.iter()
.map(|f| f.get::<String>().unwrap())
.eq(formats.iter().map(|f| f.to_string())));
let caps = AudioCapsBuilder::for_encoding("audio/x-raw")
.format_list_if_some(Some(formats))
.build();
assert_eq!(caps, caps_with_format);
let caps = AudioCapsBuilder::for_encoding("audio/x-raw")
.format_list_if_some(Option::<Vec<AudioFormat>>::None)
.build();
assert!(!caps.structure(0).unwrap().has_field("format"));
let caps = AudioCapsBuilder::for_encoding("audio/x-raw")
.format_list_if_not_empty(formats)
.build();
assert_eq!(caps, caps_with_format);
let caps = AudioCapsBuilder::for_encoding("audio/x-raw")
.format_list_if_not_empty(Vec::<AudioFormat>::new())
.build();
assert!(!caps.structure(0).unwrap().has_field("format"));
}
}

View file

@ -137,24 +137,20 @@ pub struct ElementPropertiesGeneralBuilder {
}
impl ElementPropertiesGeneralBuilder {
pub fn field<T>(mut self, property_name: &str, value: T) -> Self
where
T: Into<glib::Value> + Send,
{
// rustdoc-stripper-ignore-next
/// Sets property `property_name` to the given value `value`.
///
/// Overrides any default or previously defined value for `property_name`.
pub fn field(
mut self,
property_name: impl glib::IntoGStr,
value: impl Into<glib::Value> + Send,
) -> Self {
self.structure.set(property_name, value);
self
}
pub fn field_if_some<T>(self, property_name: &str, value: Option<T>) -> Self
where
T: Into<glib::Value> + Send,
{
if let Some(value) = value {
self.field(property_name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(field);
pub fn field_value(mut self, property_name: &str, value: glib::SendValue) -> Self {
self.structure.set_value(property_name, value);
@ -186,6 +182,14 @@ impl ElementPropertiesMapBuilder {
self
}
pub fn item_if(self, item: ElementPropertiesMapItem, predicate: bool) -> Self {
if predicate {
self.item(item)
} else {
self
}
}
pub fn item_if_some(self, item: Option<ElementPropertiesMapItem>) -> Self {
if let Some(item) = item {
self.item(item)
@ -271,24 +275,20 @@ pub struct ElementPropertiesMapItemBuilder {
}
impl ElementPropertiesMapItemBuilder {
pub fn field<T>(mut self, property_name: &str, value: T) -> Self
where
T: Into<glib::Value> + Send,
{
// rustdoc-stripper-ignore-next
/// Sets property `property_name` to the given value `value`.
///
/// Overrides any default or previously defined value for `property_name`.
pub fn field(
mut self,
property_name: impl glib::IntoGStr,
value: impl Into<glib::Value> + Send,
) -> Self {
self.structure.set(property_name, value);
self
}
pub fn field_if_some<T>(self, property_name: &str, value: Option<T>) -> Self
where
T: Into<glib::Value> + Send,
{
if let Some(value) = value {
self.field(property_name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(field);
pub fn field_value(mut self, property_name: &str, value: glib::SendValue) -> Self {
self.structure.set_value(property_name, value);

View file

@ -348,6 +348,9 @@ pub trait EncodingProfileBuilder<'a>: Sized {
fn presence(self, presence: u32) -> Self;
#[doc(alias = "gst_encoding_profile_set_presence")]
#[must_use]
fn presence_if(self, presence: u32, predicate: bool) -> Self;
#[doc(alias = "gst_encoding_profile_set_presence")]
#[must_use]
fn presence_if_some(self, presence: Option<u32>) -> Self;
#[doc(alias = "gst_encoding_profile_set_allow_dynamic_output")]
#[must_use]
@ -376,6 +379,10 @@ pub trait EncodingProfileBuilder<'a>: Sized {
#[cfg(feature = "v1_20")]
#[doc(alias = "gst_encoding_profile_set_element_properties")]
#[must_use]
fn element_properties_if(self, element_properties: ElementProperties, predecate: bool) -> Self;
#[cfg(feature = "v1_20")]
#[doc(alias = "gst_encoding_profile_set_element_properties")]
#[must_use]
fn element_properties_if_some(self, element_properties: Option<ElementProperties>) -> Self;
}
@ -407,6 +414,14 @@ macro_rules! declare_encoding_profile_builder_common(
self
}
fn presence_if(self, presence: u32, predicate: bool) -> $name<'a> {
if predicate {
self.presence(presence)
} else {
self
}
}
fn presence_if_some(self, presence: Option<u32>) -> $name<'a> {
if let Some(presence) = presence {
self.presence(presence)
@ -462,6 +477,15 @@ macro_rules! declare_encoding_profile_builder_common(
self
}
#[cfg(feature = "v1_20")]
fn element_properties_if(self, element_properties: ElementProperties, predicate: bool) -> $name<'a> {
if predicate {
self.element_properties(element_properties)
} else {
self
}
}
#[cfg(feature = "v1_20")]
fn element_properties_if_some(self, element_properties: Option<ElementProperties>) -> $name<'a> {
if let Some(element_properties) = element_properties {

View file

@ -15,6 +15,15 @@ macro_rules! message_builder_generic_impl {
}
}
#[allow(clippy::needless_update)]
pub fn src_if<O: IsA<Element> + Cast + Clone>(self, src: &O, predicate: bool) -> Self {
if predicate {
self.src(src)
} else {
self
}
}
#[allow(clippy::needless_update)]
pub fn src_if_some<O: IsA<Element> + Cast + Clone>(self, src: Option<&O>) -> Self {
if let Some(src) = src {
@ -33,6 +42,16 @@ macro_rules! message_builder_generic_impl {
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if(self, seqnum: Seqnum, predicate: bool) -> Self {
if predicate {
self.seqnum(seqnum)
} else {
self
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if_some(self, seqnum: Option<Seqnum>) -> Self {
@ -53,6 +72,17 @@ macro_rules! message_builder_generic_impl {
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_message_set_details")]
pub fn details_if(self, details: gst::Structure, predicate: bool) -> Self {
if predicate {
self.details(details)
} else {
self
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_message_set_details")]
@ -74,6 +104,17 @@ macro_rules! message_builder_generic_impl {
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_missing_plugin_message_set_stream_id")]
pub fn stream_id_if(self, stream_id: &'a str, predicate: bool) -> Self {
if predicate {
self.stream_id(stream_id)
} else {
self
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_missing_plugin_message_set_stream_id")]
@ -85,6 +126,10 @@ macro_rules! message_builder_generic_impl {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self {
Self {
builder: self.builder.other_field(name, value),
@ -92,13 +137,7 @@ macro_rules! message_builder_generic_impl {
}
}
pub fn other_field_if_some(self, name: &'a str, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(other_field);
#[deprecated = "use builder.other_field() instead"]
#[allow(clippy::needless_update)]

View file

@ -108,17 +108,7 @@ impl Builder {
self
}
pub fn field_if_some(
self,
name: impl IntoGStr,
value: Option<impl Into<glib::Value> + Send>,
) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(field);
#[must_use = "Building the structure without using it has no effect"]
pub fn build(self) -> RTSPToken {

View file

@ -83,6 +83,14 @@ impl<'a> ActionParameterBuilder<'a> {
self
}
pub fn add_possible_variable_if(self, possible_variable: &str, predicate: bool) -> Self {
if predicate {
self.add_possible_variable(possible_variable)
} else {
self
}
}
pub fn add_possible_variable_if_some(self, possible_variable: Option<&str>) -> Self {
if let Some(possible_variable) = possible_variable {
self.add_possible_variable(possible_variable)
@ -101,6 +109,14 @@ impl<'a> ActionParameterBuilder<'a> {
self
}
pub fn default_value_if(self, default_value: &'a str, predicate: bool) -> Self {
if predicate {
self.default_value(default_value)
} else {
self
}
}
pub fn default_value_if_some(self, default_value: Option<&'a str>) -> Self {
if let Some(default_value) = default_value {
self.default_value(default_value)
@ -120,6 +136,14 @@ impl<'a> ActionParameterBuilder<'a> {
self
}
pub fn add_type_if(self, types: &str, predicate: bool) -> Self {
if predicate {
self.add_type(types)
} else {
self
}
}
pub fn add_type_if_some(self, types: Option<&str>) -> Self {
if let Some(types) = types {
self.add_type(types)
@ -198,6 +222,19 @@ impl<'a> ActionTypeBuilder<'a> {
self
}
pub fn implementer_namespace_if(
mut self,
implementer_namespace: &'a str,
predicate: bool,
) -> Self {
if predicate {
self.implementer_namespace = Some(implementer_namespace);
self
} else {
self
}
}
pub fn implementer_namespace_if_some(self, implementer_namespace: Option<&'a str>) -> Self {
if let Some(implementer_namespace) = implementer_namespace {
self.implementer_namespace(implementer_namespace)
@ -211,6 +248,15 @@ impl<'a> ActionTypeBuilder<'a> {
self
}
pub fn description_if(mut self, description: &'a str, predicate: bool) -> Self {
if predicate {
self.description = Some(description);
self
} else {
self
}
}
pub fn description_if_some(self, description: Option<&'a str>) -> Self {
if let Some(description) = description {
self.description(description)
@ -224,6 +270,15 @@ impl<'a> ActionTypeBuilder<'a> {
self
}
pub fn parameter_if(mut self, parameter: ActionParameter, predicate: bool) -> Self {
if predicate {
self.parameters.push(parameter);
self
} else {
self
}
}
pub fn parameter_if_some(self, parameter: Option<ActionParameter>) -> Self {
if let Some(parameter) = parameter {
self.parameter(parameter)
@ -237,6 +292,15 @@ impl<'a> ActionTypeBuilder<'a> {
self
}
pub fn flags_if(mut self, flags: crate::ActionTypeFlags, predicate: bool) -> Self {
if predicate {
self.flags |= flags;
self
} else {
self
}
}
pub fn flags_if_some(self, flags: Option<crate::ActionTypeFlags>) -> Self {
if let Some(flags) = flags {
self.flags(flags)

View file

@ -72,6 +72,14 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn format_if(self, format: VideoFormat, predicate: bool) -> Self {
if predicate {
self.format(format)
} else {
self
}
}
pub fn format_if_some(self, format: Option<VideoFormat>) -> Self {
if let Some(format) = format {
self.format(format)
@ -89,6 +97,18 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn format_list_if(
self,
formats: impl IntoIterator<Item = VideoFormat>,
predicate: bool,
) -> Self {
if predicate {
self.format_list(formats)
} else {
self
}
}
pub fn format_list_if_some(
self,
formats: Option<impl IntoIterator<Item = VideoFormat>>,
@ -100,12 +120,29 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn format_list_if_not_empty(self, formats: impl IntoIterator<Item = VideoFormat>) -> Self {
let mut formats = formats.into_iter().peekable();
if formats.peek().is_some() {
self.format_list(formats)
} else {
self
}
}
pub fn width(self, width: i32) -> Self {
Self {
builder: self.builder.field(glib::gstr!("width"), width),
}
}
pub fn width_if(self, width: i32, predicate: bool) -> Self {
if predicate {
self.width(width)
} else {
self
}
}
pub fn width_if_some(self, width: Option<i32>) -> Self {
if let Some(width) = width {
self.width(width)
@ -130,6 +167,14 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn width_range_if(self, widths: impl RangeBounds<i32>, predicate: bool) -> Self {
if predicate {
self.width_range(widths)
} else {
self
}
}
pub fn width_list(self, widths: impl IntoIterator<Item = i32>) -> Self {
Self {
builder: self
@ -138,6 +183,14 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn width_list_if(self, widths: impl IntoIterator<Item = i32>, predicate: bool) -> Self {
if predicate {
self.width_list(widths)
} else {
self
}
}
pub fn width_list_if_some(self, widths: Option<impl IntoIterator<Item = i32>>) -> Self {
if let Some(widths) = widths {
self.width_list(widths)
@ -146,12 +199,29 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn width_list_if_not_empty(self, widths: impl IntoIterator<Item = i32>) -> Self {
let mut widths = widths.into_iter().peekable();
if widths.peek().is_some() {
self.width_list(widths)
} else {
self
}
}
pub fn height(self, height: i32) -> Self {
Self {
builder: self.builder.field(glib::gstr!("height"), height),
}
}
pub fn height_if(self, height: i32, predicate: bool) -> Self {
if predicate {
self.height(height)
} else {
self
}
}
pub fn height_if_some(self, height: Option<i32>) -> Self {
if let Some(height) = height {
self.height(height)
@ -168,6 +238,14 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn height_range_if(self, heights: impl RangeBounds<i32>, predicate: bool) -> Self {
if predicate {
self.height_range(heights)
} else {
self
}
}
pub fn height_range_if_some(self, heights: Option<impl RangeBounds<i32>>) -> Self {
if let Some(heights) = heights {
self.height_range(heights)
@ -184,6 +262,14 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn height_list_if(self, heights: impl IntoIterator<Item = i32>, predicate: bool) -> Self {
if predicate {
self.height_list(heights)
} else {
self
}
}
pub fn height_list_if_some(self, heights: Option<impl IntoIterator<Item = i32>>) -> Self {
if let Some(heights) = heights {
self.height_list(heights)
@ -192,12 +278,29 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn height_list_if_not_empty(self, heights: impl IntoIterator<Item = i32>) -> Self {
let mut heights = heights.into_iter().peekable();
if heights.peek().is_some() {
self.height_list(heights)
} else {
self
}
}
pub fn framerate(self, framerate: gst::Fraction) -> Self {
Self {
builder: self.builder.field(glib::gstr!("framerate"), framerate),
}
}
pub fn framerate_if(self, framerate: gst::Fraction, predicate: bool) -> Self {
if predicate {
self.framerate(framerate)
} else {
self
}
}
pub fn framerate_if_some(self, framerate: Option<gst::Fraction>) -> Self {
if let Some(framerate) = framerate {
self.framerate(framerate)
@ -230,6 +333,18 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn framerate_range_if(
self,
framerates: impl RangeBounds<gst::Fraction>,
predicate: bool,
) -> Self {
if predicate {
self.framerate_range(framerates)
} else {
self
}
}
pub fn framerate_range_if_some(
self,
framerates: Option<impl RangeBounds<gst::Fraction>>,
@ -249,6 +364,18 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn framerate_list_if(
self,
framerates: impl IntoIterator<Item = gst::Fraction>,
predicate: bool,
) -> Self {
if predicate {
self.framerate_list(framerates)
} else {
self
}
}
pub fn framerate_list_if_some(
self,
framerates: Option<impl IntoIterator<Item = gst::Fraction>>,
@ -260,12 +387,32 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn framerate_list_if_not_empty(
self,
framerates: impl IntoIterator<Item = gst::Fraction>,
) -> Self {
let mut framerates = framerates.into_iter().peekable();
if framerates.peek().is_some() {
self.framerate_list(framerates)
} else {
self
}
}
pub fn pixel_aspect_ratio(self, pixel_aspect_ratio: gst::Fraction) -> Self {
Self {
builder: self.builder.field("pixel-aspect-ratio", pixel_aspect_ratio),
}
}
pub fn pixel_aspect_ratio_if(self, pixel_aspect_ratio: gst::Fraction, predicate: bool) -> Self {
if predicate {
self.pixel_aspect_ratio(pixel_aspect_ratio)
} else {
self
}
}
pub fn pixel_aspect_ratio_if_some(self, pixel_aspect_ratio: Option<gst::Fraction>) -> Self {
if let Some(pixel_aspect_ratio) = pixel_aspect_ratio {
self.pixel_aspect_ratio(pixel_aspect_ratio)
@ -303,6 +450,18 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn pixel_aspect_ratio_range_if(
self,
pixel_aspect_ratios: impl RangeBounds<gst::Fraction>,
predicate: bool,
) -> Self {
if predicate {
self.pixel_aspect_ratio_range(pixel_aspect_ratios)
} else {
self
}
}
pub fn pixel_aspect_ratio_range_if_some(
self,
pixel_aspect_ratios: Option<impl RangeBounds<gst::Fraction>>,
@ -325,6 +484,18 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn pixel_aspect_ratio_list_if(
self,
pixel_aspect_ratios: impl IntoIterator<Item = gst::Fraction>,
predicate: bool,
) -> Self {
if predicate {
self.pixel_aspect_ratio_list(pixel_aspect_ratios)
} else {
self
}
}
pub fn pixel_aspect_ratio_list_if_some(
self,
pixel_aspect_ratios: Option<impl IntoIterator<Item = gst::Fraction>>,
@ -336,19 +507,30 @@ impl<T> VideoCapsBuilder<T> {
}
}
pub fn field(self, name: &str, value: impl Into<glib::Value> + Send) -> Self {
pub fn pixel_aspect_ratio_list_if_not_empty(
self,
pixel_aspect_ratios: impl IntoIterator<Item = gst::Fraction>,
) -> Self {
let mut pixel_aspect_ratios = pixel_aspect_ratios.into_iter().peekable();
if pixel_aspect_ratios.peek().is_some() {
self.pixel_aspect_ratio_list(pixel_aspect_ratios)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn field(self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) -> Self {
Self {
builder: self.builder.field(name, value),
}
}
pub fn field_if_some(self, name: &str, value: Option<impl Into<glib::Value> + Send>) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(field);
#[must_use]
pub fn build(self) -> gst::Caps {

View file

@ -19,6 +19,14 @@ macro_rules! event_builder_generic_impl {
}
}
pub fn seqnum_if(self, seqnum: gst::Seqnum, predicate: bool) -> Self {
if predicate {
self.seqnum(seqnum)
} else {
self
}
}
pub fn seqnum_if_some(self, seqnum: Option<gst::Seqnum>) -> Self {
if let Some(seqnum) = seqnum {
self.seqnum(seqnum)
@ -34,6 +42,14 @@ macro_rules! event_builder_generic_impl {
}
}
pub fn running_time_offset_if(self, running_time_offset: i64, predicate: bool) -> Self {
if predicate {
self.running_time_offset(running_time_offset)
} else {
self
}
}
pub fn running_time_offset_if_some(self, running_time_offset: Option<i64>) -> Self {
if let Some(running_time_offset) = running_time_offset {
self.running_time_offset(running_time_offset)
@ -52,13 +68,7 @@ macro_rules! event_builder_generic_impl {
}
}
pub fn other_field_if_some(self, name: &'a str, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(other_field);
#[deprecated = "use build.other_field() instead"]
pub fn other_fields(
@ -138,6 +148,14 @@ impl<'a> DownstreamForceKeyUnitEventBuilder<'a> {
}
}
pub fn timestamp_if(self, timestamp: gst::ClockTime, predicate: bool) -> Self {
if predicate {
self.timestamp(timestamp)
} else {
self
}
}
pub fn timestamp_if_some(self, timestamp: Option<gst::ClockTime>) -> Self {
if let Some(timestamp) = timestamp {
self.timestamp(timestamp)
@ -153,6 +171,14 @@ impl<'a> DownstreamForceKeyUnitEventBuilder<'a> {
}
}
pub fn stream_time_if(self, stream_time: gst::ClockTime, predicate: bool) -> Self {
if predicate {
self.stream_time(stream_time)
} else {
self
}
}
pub fn stream_time_if_some(self, stream_time: Option<gst::ClockTime>) -> Self {
if let Some(stream_time) = stream_time {
self.stream_time(stream_time)
@ -168,6 +194,14 @@ impl<'a> DownstreamForceKeyUnitEventBuilder<'a> {
}
}
pub fn running_time_if(self, running_time: gst::ClockTime, predicate: bool) -> Self {
if predicate {
self.running_time(running_time)
} else {
self
}
}
pub fn running_time_if_some(self, running_time: Option<gst::ClockTime>) -> Self {
if let Some(running_time) = running_time {
self.running_time(running_time)
@ -195,6 +229,14 @@ impl<'a> DownstreamForceKeyUnitEventBuilder<'a> {
Self { count, ..self }
}
pub fn count_if(self, count: u32, predicate: bool) -> Self {
if predicate {
self.count(count)
} else {
self
}
}
pub fn count_if_some(self, count: Option<u32>) -> Self {
if let Some(count) = count {
self.count(count)
@ -292,6 +334,14 @@ impl<'a> UpstreamForceKeyUnitEventBuilder<'a> {
}
}
pub fn running_time_if(self, running_time: gst::ClockTime, predicate: bool) -> Self {
if predicate {
self.running_time(running_time)
} else {
self
}
}
pub fn running_time_if_some(self, running_time: Option<gst::ClockTime>) -> Self {
if let Some(running_time) = running_time {
self.running_time(running_time)
@ -319,6 +369,14 @@ impl<'a> UpstreamForceKeyUnitEventBuilder<'a> {
Self { count, ..self }
}
pub fn count_if(self, count: u32, predicate: bool) -> Self {
if predicate {
self.count(count)
} else {
self
}
}
pub fn count_if_some(self, count: Option<u32>) -> Self {
if let Some(count) = count {
self.count(count)

View file

@ -447,6 +447,18 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn interlace_mode_if(
self,
interlace_mode: crate::VideoInterlaceMode,
predicate: bool,
) -> VideoInfoBuilder<'a> {
if predicate {
self.interlace_mode(interlace_mode)
} else {
self
}
}
pub fn interlace_mode_if_some(
self,
interlace_mode: Option<crate::VideoInterlaceMode>,
@ -465,6 +477,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn flags_if(self, flags: crate::VideoFlags, predicate: bool) -> Self {
if predicate {
self.flags(flags)
} else {
self
}
}
pub fn flags_if_some(self, flags: Option<crate::VideoFlags>) -> Self {
if let Some(flags) = flags {
self.flags(flags)
@ -480,6 +500,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn size_if(self, size: usize, predicate: bool) -> Self {
if predicate {
self.size(size)
} else {
self
}
}
pub fn size_if_some(self, size: Option<usize>) -> Self {
if let Some(size) = size {
self.size(size)
@ -495,6 +523,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn views_if(self, views: u32, predicate: bool) -> Self {
if predicate {
self.views(views)
} else {
self
}
}
pub fn views_if_some(self, views: Option<u32>) -> Self {
if let Some(views) = views {
self.views(views)
@ -510,6 +546,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn chroma_site_if(self, chroma_site: crate::VideoChromaSite, predicate: bool) -> Self {
if predicate {
self.chroma_site(chroma_site)
} else {
self
}
}
pub fn chroma_site_if_some(self, chroma_site: Option<crate::VideoChromaSite>) -> Self {
if let Some(chroma_site) = chroma_site {
self.chroma_site(chroma_site)
@ -525,6 +569,18 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn colorimetry_if(
self,
colorimetry: &'a crate::VideoColorimetry,
predicate: bool,
) -> VideoInfoBuilder<'a> {
if predicate {
self.colorimetry(colorimetry)
} else {
self
}
}
pub fn colorimetry_if_some(
self,
colorimetry: Option<&'a crate::VideoColorimetry>,
@ -543,6 +599,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn par_if<T: Into<gst::Fraction>>(self, par: T, predicate: bool) -> Self {
if predicate {
self.par(par)
} else {
self
}
}
pub fn par_if_some<T: Into<gst::Fraction>>(self, par: Option<T>) -> Self {
if let Some(par) = par {
self.par(par)
@ -558,6 +622,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn fps_if<T: Into<gst::Fraction>>(self, fps: T, predicate: bool) -> Self {
if predicate {
self.fps(fps)
} else {
self
}
}
pub fn fps_if_some<T: Into<gst::Fraction>>(self, fps: Option<T>) -> Self {
if let Some(fps) = fps {
self.fps(fps)
@ -573,6 +645,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn offset_if(self, offset: &'a [usize], predicate: bool) -> VideoInfoBuilder<'a> {
if predicate {
self.offset(offset)
} else {
self
}
}
pub fn offset_if_some(self, offset: Option<&'a [usize]>) -> VideoInfoBuilder<'a> {
if let Some(offset) = offset {
self.offset(offset)
@ -588,6 +668,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn stride_if(self, stride: &'a [i32], predicate: bool) -> VideoInfoBuilder<'a> {
if predicate {
self.stride(stride)
} else {
self
}
}
pub fn stride_if_some(self, stride: Option<&'a [i32]>) -> VideoInfoBuilder<'a> {
if let Some(stride) = stride {
self.stride(stride)
@ -603,6 +691,18 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn multiview_mode_if(
self,
multiview_mode: crate::VideoMultiviewMode,
predicate: bool,
) -> Self {
if predicate {
self.multiview_mode(multiview_mode)
} else {
self
}
}
pub fn multiview_mode_if_some(self, multiview_mode: Option<crate::VideoMultiviewMode>) -> Self {
if let Some(multiview_mode) = multiview_mode {
self.multiview_mode(multiview_mode)
@ -618,6 +718,18 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn multiview_flags_if(
self,
multiview_flags: crate::VideoMultiviewFlags,
predicate: bool,
) -> Self {
if predicate {
self.multiview_flags(multiview_flags)
} else {
self
}
}
pub fn multiview_flags_if_some(
self,
multiview_flags: Option<crate::VideoMultiviewFlags>,
@ -636,6 +748,14 @@ impl<'a> VideoInfoBuilder<'a> {
}
}
pub fn field_order_if(self, field_order: crate::VideoFieldOrder, predicate: bool) -> Self {
if predicate {
self.field_order(field_order)
} else {
self
}
}
pub fn field_order_if_some(self, field_order: Option<crate::VideoFieldOrder>) -> Self {
if let Some(field_order) = field_order {
self.field_order(field_order)

View file

@ -20,6 +20,15 @@ macro_rules! message_builder_generic_impl {
}
}
#[allow(clippy::needless_update)]
pub fn src_if<O: IsA<Object> + Cast + Clone>(self, src: &O, predicate: bool) -> Self {
if predicate {
self.src(src)
} else {
self
}
}
#[allow(clippy::needless_update)]
pub fn src_if_some<O: IsA<Object> + Cast + Clone>(self, src: Option<&O>) -> Self {
if let Some(src) = src {
@ -38,6 +47,16 @@ macro_rules! message_builder_generic_impl {
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if(self, seqnum: Seqnum, predicate: bool) -> Self {
if predicate {
self.seqnum(seqnum)
} else {
self
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if_some(self, seqnum: Option<Seqnum>) -> Self {
@ -55,13 +74,7 @@ macro_rules! message_builder_generic_impl {
}
}
pub fn other_field_if_some(self, name: &'a str, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
gst::impl_builder_gvalue_extra_setters!(other_field);
#[deprecated = "use builder.other_field() instead"]
#[allow(clippy::needless_update)]

View file

@ -278,6 +278,14 @@ impl BinBuilder {
}
}
pub fn name_if(self, name: impl Into<glib::GString>, predicate: bool) -> Self {
if predicate {
self.name(name)
} else {
self
}
}
pub fn name_if_some(self, name: Option<impl Into<glib::GString>>) -> Self {
if let Some(name) = name {
self.name(name)

View file

@ -2,7 +2,11 @@
use std::{fmt, marker::PhantomData, ptr, str};
use glib::{prelude::*, translate::*, value::ToSendValue};
use glib::{
prelude::*,
translate::*,
value::{SendValue, ToSendValue},
};
use crate::{caps_features::*, ffi, structure::*, CapsIntersectMode};
@ -298,6 +302,10 @@ impl std::iter::Extend<Caps> for CapsRef {
}
impl CapsRef {
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[doc(alias = "gst_caps_set_value")]
#[doc(alias = "gst_caps_set_simple")]
pub fn set(&mut self, name: impl IntoGStr, value: impl ToSendValue + Sync) {
@ -305,6 +313,67 @@ impl CapsRef {
self.set_value(name, value);
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_caps_set_value")]
#[doc(alias = "gst_caps_set_simple")]
pub fn set_if(&mut self, name: impl IntoGStr, value: impl ToSendValue + Sync, predicate: bool) {
if predicate {
self.set(name, value);
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_caps_set_value")]
#[doc(alias = "gst_caps_set_simple")]
pub fn set_if_some(&mut self, name: impl IntoGStr, value: Option<impl ToSendValue + Sync>) {
if let Some(value) = value {
self.set(name, value);
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s the `Item`s.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn set_from_iter<V: ValueType + ToSendValue + FromIterator<SendValue> + Sync>(
&mut self,
name: impl IntoGStr,
iter: impl IntoIterator<Item = impl ToSendValue>,
) {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.set(name, V::from_iter(iter));
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s Item`s,
/// if `iter` is not empty.
///
/// This has no effect if `iter` is empty, i.e. previous value for `name` is unchanged.
#[inline]
pub fn set_if_not_empty<V: ValueType + ToSendValue + FromIterator<SendValue> + Sync>(
&mut self,
name: impl IntoGStr,
iter: impl IntoIterator<Item = impl ToSendValue>,
) {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.set(name, V::from_iter(iter));
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[doc(alias = "gst_caps_set_value")]
pub fn set_value(&mut self, name: impl IntoGStr, value: glib::SendValue) {
unsafe {
@ -314,6 +383,29 @@ impl CapsRef {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_caps_set_value")]
pub fn set_value_if(&mut self, name: impl IntoGStr, value: SendValue, predicate: bool) {
if predicate {
self.set_value(name, value);
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_caps_set_value")]
pub fn set_value_if_some(&mut self, name: impl IntoGStr, value: Option<SendValue>) {
if let Some(value) = value {
self.set_value(name, value);
}
}
#[doc(alias = "get_structure")]
#[doc(alias = "gst_caps_get_structure")]
pub fn structure(&self, idx: usize) -> Option<&StructureRef> {
@ -1014,22 +1106,17 @@ impl Builder<NoFeature> {
}
impl<T> Builder<T> {
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn field(mut self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) -> Self {
self.s.set(name, value);
self
}
pub fn field_if_some(
self,
name: impl IntoGStr,
value: Option<impl Into<glib::Value> + Send>,
) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
impl_builder_gvalue_extra_setters!(field);
#[must_use = "Building the caps without using them has no effect"]
pub fn build(self) -> Caps {

View file

@ -225,6 +225,8 @@ impl<'a> ElementBuilder<'a> {
// rustdoc-stripper-ignore-next
/// Sets property `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn property(self, name: &'a str, value: impl Into<glib::Value> + 'a) -> Self {
Self {
@ -237,23 +239,10 @@ impl<'a> ElementBuilder<'a> {
}
}
// rustdoc-stripper-ignore-next
/// Sets property `name` to the given value `value` if it is `Some`.
#[inline]
pub fn property_if_some(
self,
name: &'a str,
value: Option<impl Into<glib::Value> + 'a>,
) -> Self {
if let Some(value) = value {
self.property(name, value)
} else {
self
}
}
impl_builder_gvalue_extra_setters!(property);
// rustdoc-stripper-ignore-next
/// Set property `name` to the given string value `value`.
/// Sets property `name` to the given string value `value`.
#[inline]
pub fn property_from_str(self, name: &'a str, value: &'a str) -> Self {
Self {
@ -267,7 +256,7 @@ impl<'a> ElementBuilder<'a> {
}
// rustdoc-stripper-ignore-next
/// Set property `name` to the given string value `value` if it is `Some`.
/// Sets property `name` to the given string value `value` if it is `Some`.
#[inline]
pub fn property_from_str_if_some(self, name: &'a str, value: Option<&'a str>) -> Self {
if let Some(value) = value {
@ -278,7 +267,7 @@ impl<'a> ElementBuilder<'a> {
}
// rustdoc-stripper-ignore-next
/// Build the element with the provided properties.
/// Builds the element with the provided properties.
///
/// This fails if there is no such element factory or the element factory can't be loaded.
///

View file

@ -2099,6 +2099,16 @@ macro_rules! event_builder_generic_impl {
}
}
#[doc(alias = "gst_event_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if(self, seqnum: Seqnum, predicate: bool) -> Self {
if predicate {
self.seqnum(seqnum)
} else {
self
}
}
#[doc(alias = "gst_event_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if_some(self, seqnum: Option<Seqnum>) -> Self {
@ -2118,6 +2128,16 @@ macro_rules! event_builder_generic_impl {
}
}
#[doc(alias = "gst_event_set_running_time_offset")]
#[allow(clippy::needless_update)]
pub fn running_time_offset_if(self, running_time_offset: i64, predicate: bool) -> Self {
if predicate {
self.running_time_offset(running_time_offset)
} else {
self
}
}
#[doc(alias = "gst_event_set_running_time_offset")]
#[allow(clippy::needless_update)]
pub fn running_time_offset_if_some(self, running_time_offset: Option<i64>) -> Self {
@ -2128,6 +2148,10 @@ macro_rules! event_builder_generic_impl {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[allow(clippy::needless_update)]
pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self {
Self {
@ -2136,14 +2160,7 @@ macro_rules! event_builder_generic_impl {
}
}
#[allow(clippy::needless_update)]
pub fn other_field_if_some(self, name: &'a str, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
impl_builder_gvalue_extra_setters!(other_field);
#[deprecated = "use build.other_field() instead"]
#[allow(clippy::needless_update)]
@ -2249,6 +2266,14 @@ impl<'a> StreamStartBuilder<'a> {
}
}
pub fn flags_if(self, flags: crate::StreamFlags, predicate: bool) -> Self {
if predicate {
self.flags(flags)
} else {
self
}
}
pub fn flags_if_some(self, flags: Option<crate::StreamFlags>) -> Self {
if let Some(flags) = flags {
self.flags(flags)
@ -2264,6 +2289,14 @@ impl<'a> StreamStartBuilder<'a> {
}
}
pub fn group_id_if(self, group_id: GroupId, predicate: bool) -> Self {
if predicate {
self.group_id(group_id)
} else {
self
}
}
pub fn group_id_if_some(self, group_id: Option<GroupId>) -> Self {
if let Some(group_id) = group_id {
self.group_id(group_id)
@ -2279,6 +2312,14 @@ impl<'a> StreamStartBuilder<'a> {
}
}
pub fn stream_if(self, stream: crate::Stream, predicate: bool) -> Self {
if predicate {
self.stream(stream)
} else {
self
}
}
pub fn stream_if_some(self, stream: Option<crate::Stream>) -> Self {
if let Some(stream) = stream {
self.stream(stream)
@ -2555,6 +2596,14 @@ impl<'a> ProtectionBuilder<'a> {
}
}
pub fn origin_if(self, origin: &'a str, predicate: bool) -> Self {
if predicate {
self.origin(origin)
} else {
self
}
}
pub fn origin_if_some(self, origin: Option<&'a str>) -> Self {
if let Some(origin) = origin {
self.origin(origin)
@ -2620,6 +2669,16 @@ impl<'a> GapBuilder<'a> {
self
}
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
pub fn gap_flags_if(self, flags: crate::GapFlags, predicate: bool) -> Self {
if predicate {
self.gap_flags(flags)
} else {
self
}
}
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
pub fn gap_flags_if_some(self, flags: Option<crate::GapFlags>) -> Self {
@ -2635,6 +2694,14 @@ impl<'a> GapBuilder<'a> {
self
}
pub fn duration_if(self, duration: ClockTime, predicate: bool) -> Self {
if predicate {
self.duration(duration)
} else {
self
}
}
pub fn duration_if_some(self, duration: Option<ClockTime>) -> Self {
if let Some(duration) = duration {
self.duration(duration)
@ -2709,6 +2776,14 @@ impl<'a> QosBuilder<'a> {
self
}
pub fn timestamp_if(self, timestamp: ClockTime, predicate: bool) -> Self {
if predicate {
self.timestamp(timestamp)
} else {
self
}
}
pub fn timestamp_if_some(self, timestamp: Option<ClockTime>) -> Self {
if let Some(timestamp) = timestamp {
self.timestamp(timestamp)

View file

@ -72,9 +72,8 @@ pub use crate::error::*;
#[macro_use]
pub mod miniobject;
pub use miniobject::{MiniObject, MiniObjectRef};
pub mod message;
pub use crate::message::{Message, MessageErrorDomain, MessageRef, MessageView};
#[macro_use]
mod value;
pub use crate::value::{
Array, ArrayRef, Bitmask, Fraction, FractionRange, IntRange, List, ListRef,
@ -86,6 +85,9 @@ mod value_serde;
#[cfg(feature = "serde")]
mod flag_serde;
pub mod message;
pub use crate::message::{Message, MessageErrorDomain, MessageRef, MessageView};
pub mod structure;
pub use crate::structure::{Structure, StructureRef};
#[cfg(feature = "serde")]

View file

@ -2,7 +2,10 @@
use std::{borrow::Borrow, ffi::CStr, fmt, mem, num::NonZeroU32, ops::Deref, ptr};
use glib::translate::*;
use glib::{
translate::*,
value::{SendValue, ValueType},
};
use crate::{
ffi,
@ -2612,6 +2615,16 @@ macro_rules! message_builder_generic_impl {
..self
}
}
#[allow(clippy::needless_update)]
pub fn src_if<O: IsA<Object> + Cast + Clone>(self, src: &O, predicate: bool) -> Self {
if predicate {
self.src(src)
} else {
self
}
}
#[allow(clippy::needless_update)]
pub fn src_if_some<O: IsA<Object> + Cast + Clone>(self, src: Option<&O>) -> Self {
if let Some(src) = src {
@ -2630,6 +2643,16 @@ macro_rules! message_builder_generic_impl {
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if(self, seqnum: Seqnum, predicate: bool) -> Self {
if predicate {
self.seqnum(seqnum)
} else {
self
}
}
#[doc(alias = "gst_message_set_seqnum")]
#[allow(clippy::needless_update)]
pub fn seqnum_if_some(self, seqnum: Option<Seqnum>) -> Self {
@ -2651,6 +2674,18 @@ macro_rules! message_builder_generic_impl {
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_message_set_details")]
#[allow(clippy::needless_update)]
pub fn details_if(self, details: Structure, predicate: bool) -> Self {
if predicate {
self.details(details)
} else {
self
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(alias = "gst_message_set_details")]
@ -2663,6 +2698,10 @@ macro_rules! message_builder_generic_impl {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[allow(clippy::needless_update)]
pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self {
Self {
@ -2671,14 +2710,7 @@ macro_rules! message_builder_generic_impl {
}
}
#[allow(clippy::needless_update)]
pub fn other_field_if_some(self, name: &'a str, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
impl_builder_gvalue_extra_setters!(other_field);
#[deprecated = "use build.other_field() instead"]
#[allow(clippy::needless_update)]
@ -2775,6 +2807,14 @@ impl<'a> ErrorBuilder<'a> {
}
}
pub fn debug_if(self, debug: &'a str, predicate: bool) -> Self {
if predicate {
self.debug(debug)
} else {
self
}
}
pub fn debug_if_some(self, debug: Option<&'a str>) -> Self {
if let Some(debug) = debug {
self.debug(debug)
@ -2791,6 +2831,15 @@ impl<'a> ErrorBuilder<'a> {
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if(self, details: Structure, predicate: bool) -> Self {
if predicate {
self.details(details)
} else {
self
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if_some(self, details: Option<Structure>) -> Self {
if let Some(details) = details {
@ -2842,6 +2891,14 @@ impl<'a> WarningBuilder<'a> {
}
}
pub fn debug_if(self, debug: &'a str, predicate: bool) -> Self {
if predicate {
self.debug(debug)
} else {
self
}
}
pub fn debug_if_some(self, debug: Option<&'a str>) -> Self {
if let Some(debug) = debug {
self.debug(debug)
@ -2858,6 +2915,15 @@ impl<'a> WarningBuilder<'a> {
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if(self, details: Structure, predicate: bool) -> Self {
if predicate {
self.details(details)
} else {
self
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if_some(self, details: Option<Structure>) -> Self {
if let Some(details) = details {
@ -2909,6 +2975,14 @@ impl<'a> InfoBuilder<'a> {
}
}
pub fn debug_if(self, debug: &'a str, predicate: bool) -> Self {
if predicate {
self.debug(debug)
} else {
self
}
}
pub fn debug_if_some(self, debug: Option<&'a str>) -> Self {
if let Some(debug) = debug {
self.debug(debug)
@ -2925,6 +2999,15 @@ impl<'a> InfoBuilder<'a> {
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if(self, details: Structure, predicate: bool) -> Self {
if predicate {
self.details(details)
} else {
self
}
}
#[cfg(not(feature = "v1_26"))]
pub fn details_if_some(self, details: Option<Structure>) -> Self {
if let Some(details) = details {
@ -3225,6 +3308,14 @@ impl<'a> StreamStatusBuilder<'a> {
}
}
pub fn status_object_if(self, status_object: impl ToSendValue, predicate: bool) -> Self {
if predicate {
self.status_object(status_object)
} else {
self
}
}
pub fn status_object_if_some(self, status_object: Option<impl ToSendValue>) -> Self {
if let Some(status_object) = status_object {
self.status_object(status_object)
@ -3397,6 +3488,14 @@ impl<'a> AsyncDoneBuilder<'a> {
self
}
pub fn running_time_if(self, running_time: crate::ClockTime, predicate: bool) -> Self {
if predicate {
self.running_time(running_time)
} else {
self
}
}
pub fn running_time_if_some(self, running_time: Option<crate::ClockTime>) -> Self {
if let Some(running_time) = running_time {
self.running_time(running_time)
@ -3504,6 +3603,14 @@ impl<'a> QosBuilder<'a> {
self
}
pub fn running_time_if(self, running_time: crate::ClockTime, predicate: bool) -> Self {
if predicate {
self.running_time(running_time)
} else {
self
}
}
pub fn running_time_if_some(self, running_time: Option<crate::ClockTime>) -> Self {
if let Some(running_time) = running_time {
self.running_time(running_time)
@ -3517,6 +3624,14 @@ impl<'a> QosBuilder<'a> {
self
}
pub fn stream_time_if(self, stream_time: crate::ClockTime, predicate: bool) -> Self {
if predicate {
self.stream_time(stream_time)
} else {
self
}
}
pub fn stream_time_if_some(self, stream_time: Option<crate::ClockTime>) -> Self {
if let Some(stream_time) = stream_time {
self.stream_time(stream_time)
@ -3530,6 +3645,14 @@ impl<'a> QosBuilder<'a> {
self
}
pub fn timestamp_if(self, timestamp: crate::ClockTime, predicate: bool) -> Self {
if predicate {
self.timestamp(timestamp)
} else {
self
}
}
pub fn timestamp_if_some(self, timestamp: Option<crate::ClockTime>) -> Self {
if let Some(timestamp) = timestamp {
self.timestamp(timestamp)
@ -3543,6 +3666,14 @@ impl<'a> QosBuilder<'a> {
self
}
pub fn duration_if(self, duration: crate::ClockTime, predicate: bool) -> Self {
if predicate {
self.duration(duration)
} else {
self
}
}
pub fn duration_if_some(self, duration: Option<crate::ClockTime>) -> Self {
if let Some(duration) = duration {
self.duration(duration)
@ -3688,6 +3819,14 @@ impl<'a> StreamStartBuilder<'a> {
}
}
pub fn group_id_if(self, group_id: GroupId, predicate: bool) -> Self {
if predicate {
self.group_id(group_id)
} else {
self
}
}
pub fn group_id_if_some(self, group_id: Option<GroupId>) -> Self {
if let Some(group_id) = group_id {
self.group_id(group_id)
@ -3813,6 +3952,14 @@ impl<'a> PropertyNotifyBuilder<'a> {
}
}
pub fn value_if(self, value: impl ToSendValue, predicate: bool) -> Self {
if predicate {
self.value(value)
} else {
self
}
}
pub fn value_if_some(self, value: Option<impl ToSendValue>) -> Self {
if let Some(value) = value {
self.value(value)
@ -3821,6 +3968,29 @@ impl<'a> PropertyNotifyBuilder<'a> {
}
}
pub fn value_from_iter<V: ValueType + ToSendValue + FromIterator<SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl ToSendValue>,
) -> Self {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.other_field(name, V::from_iter(iter))
}
pub fn value_field_if_not_empty<V: ValueType + ToSendValue + FromIterator<SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl ToSendValue>,
) -> Self {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.other_field(name, V::from_iter(iter))
} else {
self
}
}
message_builder_generic_impl!(|s: &mut Self, src| {
let v = s.value.take();
ffi::gst_message_new_property_notify(
@ -3883,6 +4053,18 @@ impl<'a> StreamsSelectedBuilder<'a> {
}
}
pub fn streams_if(
self,
streams: impl IntoIterator<Item = impl std::borrow::Borrow<crate::Stream>>,
predicate: bool,
) -> Self {
if predicate {
self.streams(streams)
} else {
self
}
}
pub fn streams_if_some(
self,
streams: Option<impl IntoIterator<Item = impl std::borrow::Borrow<crate::Stream>>>,
@ -3894,6 +4076,18 @@ impl<'a> StreamsSelectedBuilder<'a> {
}
}
pub fn streams_if_not_empty(
self,
streams: impl IntoIterator<Item = impl std::borrow::Borrow<crate::Stream>>,
) -> Self {
let mut streams = streams.into_iter().peekable();
if streams.peek().is_some() {
self.streams(streams)
} else {
self
}
}
message_builder_generic_impl!(|s: &mut Self, src| {
let msg = ffi::gst_message_new_streams_selected(src, s.collection.to_glib_none().0);
if let Some(ref streams) = s.streams {
@ -3934,6 +4128,14 @@ impl<'a> RedirectBuilder<'a> {
}
}
pub fn tag_list_if(self, tag_list: &'a TagList, predicate: bool) -> Self {
if predicate {
self.tag_list(tag_list)
} else {
self
}
}
pub fn tag_list_if_some(self, tag_list: Option<&'a TagList>) -> Self {
if let Some(tag_list) = tag_list {
self.tag_list(tag_list)
@ -3949,6 +4151,14 @@ impl<'a> RedirectBuilder<'a> {
}
}
pub fn entry_struct_if(self, entry_struct: Structure, predicate: bool) -> Self {
if predicate {
self.entry_struct(entry_struct)
} else {
self
}
}
pub fn entry_struct_if_some(self, entry_struct: Option<Structure>) -> Self {
if let Some(entry_struct) = entry_struct {
self.entry_struct(entry_struct)
@ -3968,6 +4178,19 @@ impl<'a> RedirectBuilder<'a> {
}
}
#[allow(clippy::type_complexity)]
pub fn entries_if(
self,
entries: &'a [(&'a str, Option<&'a TagList>, Option<&'a Structure>)],
predicate: bool,
) -> Self {
if predicate {
self.entries(entries)
} else {
self
}
}
#[allow(clippy::type_complexity)]
pub fn entries_if_some(
self,

View file

@ -119,6 +119,17 @@ impl<'a> PadTemplateBuilder<'a> {
}
}
pub fn gtype_if(self, gtype: glib::Type, predicate: bool) -> Self {
if predicate {
PadTemplateBuilder {
gtype: Some(gtype),
..self
}
} else {
self
}
}
pub fn gtype_if_some(self, gtype: Option<glib::Type>) -> Self {
if let Some(gtype) = gtype {
self.gtype(gtype)
@ -136,6 +147,19 @@ impl<'a> PadTemplateBuilder<'a> {
}
}
#[cfg(feature = "v1_18")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
pub fn documentation_caps_if(self, documentation_caps: &'a Caps, predicate: bool) -> Self {
if predicate {
PadTemplateBuilder {
documentation_caps: Some(documentation_caps),
..self
}
} else {
self
}
}
#[cfg(feature = "v1_18")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
pub fn documentation_caps_if_some(self, documentation_caps: Option<&'a Caps>) -> Self {

View file

@ -123,6 +123,14 @@ impl PipelineBuilder {
}
}
pub fn delay_if(self, delay: u64, predicate: bool) -> Self {
if predicate {
self.delay(delay)
} else {
self
}
}
pub fn delay_if_some(self, delay: Option<u64>) -> Self {
if let Some(delay) = delay {
self.delay(delay)
@ -141,6 +149,14 @@ impl PipelineBuilder {
}
}
pub fn latency_if(self, latency: impl Into<Option<crate::ClockTime>>, predicate: bool) -> Self {
if predicate {
self.latency(latency)
} else {
self
}
}
pub fn latency_if_some(self, latency: Option<crate::ClockTime>) -> Self {
if let Some(latency) = latency {
self.latency(latency)
@ -183,6 +199,14 @@ impl PipelineBuilder {
}
}
pub fn name_if(self, name: impl Into<glib::GString>, predicate: bool) -> Self {
if predicate {
self.name(name)
} else {
self
}
}
pub fn name_if_some(self, name: Option<impl Into<glib::GString>>) -> Self {
if let Some(name) = name {
self.name(name)

View file

@ -115,6 +115,22 @@ impl StreamCollectionBuilder {
self
}
#[doc(alias = "gst_stream_collection_add_stream")]
pub fn stream_if(self, stream: Stream, predicate: bool) -> Self {
if predicate {
unsafe {
ffi::gst_stream_collection_add_stream(
(self.0).to_glib_none().0,
stream.into_glib_ptr(),
);
}
self
} else {
self
}
}
#[doc(alias = "gst_stream_collection_add_stream")]
pub fn stream_if_some(self, stream: Option<Stream>) -> Self {
if let Some(stream) = stream {
@ -137,6 +153,23 @@ impl StreamCollectionBuilder {
self
}
pub fn streams_if(self, streams: impl IntoIterator<Item = Stream>, predicate: bool) -> Self {
if predicate {
for stream in streams.into_iter() {
unsafe {
ffi::gst_stream_collection_add_stream(
(self.0).to_glib_none().0,
stream.into_glib_ptr(),
);
}
}
self
} else {
self
}
}
pub fn streams_if_some(self, streams: Option<impl IntoIterator<Item = Stream>>) -> Self {
if let Some(streams) = streams {
self.streams(streams)
@ -145,6 +178,15 @@ impl StreamCollectionBuilder {
}
}
pub fn streams_if_not_empty(self, streams: impl IntoIterator<Item = Stream>) -> Self {
let mut streams = streams.into_iter().peekable();
if streams.peek().is_some() {
self.streams(streams)
} else {
self
}
}
#[must_use = "Building the stream collection without using it has no effect"]
pub fn build(self) -> StreamCollection {
self.0

View file

@ -12,7 +12,7 @@ use std::{
use glib::{
prelude::*,
translate::*,
value::{FromValue, SendValue},
value::{FromValue, SendValue, Value},
IntoGStr,
};
@ -493,12 +493,37 @@ impl StructureRef {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[doc(alias = "gst_structure_set")]
pub fn set(&mut self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) {
let value = glib::SendValue::from_owned(value);
self.set_value(name, value);
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_structure_set")]
pub fn set_if(
&mut self,
name: impl IntoGStr,
value: impl Into<glib::Value> + Send,
predicate: bool,
) {
if predicate {
self.set(name, value);
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_structure_set")]
pub fn set_if_some(
&mut self,
@ -510,6 +535,42 @@ impl StructureRef {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s the `Item`s.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn set_from_iter<V: ValueType + Into<Value> + FromIterator<SendValue> + Send>(
&mut self,
name: impl IntoGStr,
iter: impl IntoIterator<Item = impl ToSendValue>,
) {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.set(name, V::from_iter(iter));
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s Item`s,
/// if `iter` is not empty.
///
/// This has no effect if `iter` is empty, i.e. previous value for `name` is unchanged.
#[inline]
pub fn set_if_not_empty<V: ValueType + Into<Value> + FromIterator<SendValue> + Send>(
&mut self,
name: impl IntoGStr,
iter: impl IntoIterator<Item = impl ToSendValue>,
) {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.set(name, V::from_iter(iter));
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[doc(alias = "gst_structure_set_value")]
pub fn set_value(&mut self, name: impl IntoGStr, value: SendValue) {
unsafe {
@ -519,6 +580,22 @@ impl StructureRef {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_structure_set_value")]
pub fn set_value_if(&mut self, name: impl IntoGStr, value: SendValue, predicate: bool) {
if predicate {
self.set_value(name, value);
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[doc(alias = "gst_structure_set_value")]
pub fn set_value_if_some(&mut self, name: impl IntoGStr, value: Option<SendValue>) {
if let Some(value) = value {
@ -1167,22 +1244,17 @@ impl Builder {
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given value `value`.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn field(mut self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) -> Self {
self.s.set(name, value);
self
}
pub fn field_if_some(
self,
name: impl IntoGStr,
value: Option<impl Into<glib::Value> + Send>,
) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
impl_builder_gvalue_extra_setters!(field);
#[must_use = "Building the structure without using it has no effect"]
pub fn build(self) -> Structure {
@ -1207,6 +1279,7 @@ mod tests {
s.set("f2", &String::from("bcd"));
s.set("f3", 123i32);
s.set("f5", Some("efg"));
s.set("f7", 42i32);
assert_eq!(s.get::<&str>("f1"), Ok("abc"));
assert_eq!(s.get::<Option<&str>>("f2"), Ok(Some("bcd")));
@ -1216,6 +1289,7 @@ mod tests {
assert_eq!(s.get_optional::<i32>("f3"), Ok(Some(123i32)));
assert_eq!(s.get_optional::<i32>("f4"), Ok(None));
assert_eq!(s.get::<&str>("f5"), Ok("efg"));
assert_eq!(s.get::<i32>("f7"), Ok(42i32));
assert_eq!(
s.get::<i32>("f2"),
@ -1237,10 +1311,13 @@ mod tests {
);
assert_eq!(s.get::<i32>("f4"), Err(GetError::new_field_not_found("f4")));
assert_eq!(s.fields().collect::<Vec<_>>(), vec!["f1", "f2", "f3", "f5"]);
assert_eq!(
s.fields().collect::<Vec<_>>(),
vec!["f1", "f2", "f3", "f5", "f7"]
);
let v = s.iter().map(|(f, v)| (f, v.clone())).collect::<Vec<_>>();
assert_eq!(v.len(), 4);
assert_eq!(v.len(), 5);
assert_eq!(v[0].0, "f1");
assert_eq!(v[0].1.get::<&str>(), Ok("abc"));
assert_eq!(v[1].0, "f2");
@ -1249,6 +1326,8 @@ mod tests {
assert_eq!(v[2].1.get::<i32>(), Ok(123i32));
assert_eq!(v[3].0, "f5");
assert_eq!(v[3].1.get::<&str>(), Ok("efg"));
assert_eq!(v[4].0, "f7");
assert_eq!(v[4].1.get::<i32>(), Ok(42i32));
let s2 = Structure::builder("test")
.field("f1", "abc")
@ -1257,6 +1336,8 @@ mod tests {
.field_if_some("f4", Option::<i32>::None)
.field_if_some("f5", Some("efg"))
.field_if_some("f6", Option::<&str>::None)
.field_if("f7", 42i32, true)
.field_if("f8", 21i32, false)
.build();
assert_eq!(s, s2);
@ -1268,6 +1349,8 @@ mod tests {
s3.set_if_some("f4", Option::<i32>::None);
s3.set_if_some("f5", Some("efg"));
s3.set_if_some("f6", Option::<&str>::None);
s3.set_if("f7", 42i32, true);
s3.set_if("f8", 21i32, false);
assert_eq!(s, s3);
}
@ -1337,4 +1420,64 @@ mod tests {
assert_eq!(format!("{s:?}"), "Structure(test { f1: (gchararray) \"abc\", f2: (gchararray) \"bcd\", f3: (gint) 123, f4: Structure(nested { badger: (gboolean) TRUE }), f5: Array([(gchararray) \"a\", (gchararray) \"b\", (gchararray) \"c\"]), f6: List([(gchararray) \"d\", (gchararray) \"e\", (gchararray) \"f\"]) })");
}
#[test]
fn builder_field_from_iter() {
crate::init().unwrap();
let s = Structure::builder("test")
.field_from_iter::<crate::Array>("array", [&1, &2, &3])
.field_from_iter::<crate::List>("list", [&4, &5, &6])
.build();
assert!(s
.get::<crate::Array>("array")
.unwrap()
.iter()
.map(|val| val.get::<i32>().unwrap())
.eq([1, 2, 3]));
assert!(s
.get::<crate::List>("list")
.unwrap()
.iter()
.map(|val| val.get::<i32>().unwrap())
.eq([4, 5, 6]));
let array = Vec::<i32>::new();
let s = Structure::builder("test")
.field_from_iter::<crate::Array>("array", &array)
.field_from_iter::<crate::List>("list", &array)
.build();
assert!(s.get::<crate::Array>("array").unwrap().as_ref().is_empty());
assert!(s.get::<crate::List>("list").unwrap().as_ref().is_empty());
}
#[test]
fn builder_field_if_not_empty() {
crate::init().unwrap();
let s = Structure::builder("test")
.field_if_not_empty::<crate::Array>("array", [&1, &2, &3])
.field_if_not_empty::<crate::List>("list", [&4, &5, &6])
.build();
assert!(s
.get::<crate::Array>("array")
.unwrap()
.iter()
.map(|val| val.get::<i32>().unwrap())
.eq([1, 2, 3]));
assert!(s
.get::<crate::List>("list")
.unwrap()
.iter()
.map(|val| val.get::<i32>().unwrap())
.eq([4, 5, 6]));
let array = Vec::<i32>::new();
let s = Structure::builder("test")
.field_if_not_empty::<crate::Array>("array", &array)
.field_if_not_empty::<crate::List>("list", &array)
.build();
assert!(!s.has_field("array"));
assert!(!s.has_field("list"));
}
}

View file

@ -563,7 +563,7 @@ unsafe extern "C" fn element_post_message<T: ElementImpl>(
#[cfg(test)]
mod tests {
use std::sync::atomic;
use std::sync::{atomic, Arc, Mutex, OnceLock};
use super::*;
use crate::ElementFactory;
@ -576,6 +576,7 @@ mod tests {
pub(super) sinkpad: crate::Pad,
pub(super) n_buffers: atomic::AtomicU32,
pub(super) reached_playing: atomic::AtomicBool,
pub(super) array: Arc<Mutex<Vec<String>>>,
}
impl TestElement {
@ -658,6 +659,10 @@ mod tests {
Self {
n_buffers: atomic::AtomicU32::new(0),
reached_playing: atomic::AtomicBool::new(false),
array: Arc::new(Mutex::new(vec![
"default0".to_string(),
"default1".to_string(),
])),
srcpad,
sinkpad,
}
@ -672,6 +677,30 @@ mod tests {
element.add_pad(&self.sinkpad).unwrap();
element.add_pad(&self.srcpad).unwrap();
}
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: OnceLock<Vec<glib::ParamSpec>> = OnceLock::new();
PROPERTIES.get_or_init(|| vec![crate::ParamSpecArray::builder("array").build()])
}
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
match pspec.name() {
"array" => {
let value = value.get::<crate::Array>().unwrap();
let mut array = self.array.lock().unwrap();
array.clear();
array.extend(value.iter().map(|v| v.get().unwrap()));
}
_ => unimplemented!(),
}
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
"array" => crate::Array::new(&*self.array.lock().unwrap()).to_value(),
_ => unimplemented!(),
}
}
}
impl GstObjectImpl for TestElement {}
@ -741,9 +770,40 @@ mod tests {
}
}
fn plugin_init(plugin: &crate::Plugin) -> Result<(), glib::BoolError> {
crate::Element::register(
Some(plugin),
"testelement",
crate::Rank::MARGINAL,
TestElement::static_type(),
)
}
crate::plugin_define!(
rssubclasstestelem,
env!("CARGO_PKG_DESCRIPTION"),
plugin_init,
env!("CARGO_PKG_VERSION"),
"MPL-2.0",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_REPOSITORY"),
"1970-01-01"
);
fn init() {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
crate::init().unwrap();
plugin_register_static().expect("gstreamer subclass element test");
});
}
#[test]
fn test_element_subclass() {
crate::init().unwrap();
init();
let element = TestElement::new(Some("test"));
@ -778,4 +838,47 @@ mod tests {
assert_eq!(imp.n_buffers.load(atomic::Ordering::SeqCst), 100);
assert!(imp.reached_playing.load(atomic::Ordering::SeqCst));
}
#[test]
fn property_from_iter_if_not_empty() {
init();
let elem = crate::ElementFactory::make("testelement").build().unwrap();
assert!(elem
.property::<crate::Array>("array")
.iter()
.map(|val| val.get::<&str>().unwrap())
.eq(["default0", "default1"]));
let elem = crate::ElementFactory::make("testelement")
.property_from_iter::<crate::Array>("array", ["value0", "value1"])
.build()
.unwrap();
assert!(elem
.property::<crate::Array>("array")
.iter()
.map(|val| val.get::<&str>().unwrap())
.eq(["value0", "value1"]));
let array = Vec::<String>::new();
let elem = crate::ElementFactory::make("testelement")
.property_if_not_empty::<crate::Array>("array", &array)
.build()
.unwrap();
assert!(elem
.property::<crate::Array>("array")
.iter()
.map(|val| val.get::<&str>().unwrap())
.eq(["default0", "default1"]));
let elem = crate::ElementFactory::make("testelement")
.property_if_not_empty::<crate::Array>("array", ["value0", "value1"])
.build()
.unwrap();
assert!(elem
.property::<crate::Array>("array")
.iter()
.map(|val| val.get::<&str>().unwrap())
.eq(["value0", "value1"]));
}
}

View file

@ -22,6 +22,20 @@ impl<F: FnMut(&Task) + Send + 'static> TaskBuilder<F> {
..self
}
}
#[doc(alias = "gst_task_set_enter_callback")]
pub fn enter_callback_if<E: FnMut(&Task) + Send + 'static>(
self,
enter_callback: E,
predicate: bool,
) -> Self {
if predicate {
self.enter_callback(enter_callback)
} else {
self
}
}
#[doc(alias = "gst_task_set_enter_callback")]
pub fn enter_callback_if_some<E: FnMut(&Task) + Send + 'static>(
self,
@ -42,6 +56,19 @@ impl<F: FnMut(&Task) + Send + 'static> TaskBuilder<F> {
}
}
#[doc(alias = "gst_task_set_leave_callback")]
pub fn leave_callback_if<E: FnMut(&Task) + Send + 'static>(
self,
leave_callback: E,
predicate: bool,
) -> Self {
if predicate {
self.leave_callback(leave_callback)
} else {
self
}
}
#[doc(alias = "gst_task_set_leave_callback")]
pub fn leave_callback_if_some<E: FnMut(&Task) + Send + 'static>(
self,
@ -62,6 +89,15 @@ impl<F: FnMut(&Task) + Send + 'static> TaskBuilder<F> {
}
}
#[doc(alias = "gst_task_set_lock")]
pub fn lock_if(self, lock: &TaskLock, predicate: bool) -> Self {
if predicate {
self.lock(lock)
} else {
self
}
}
#[doc(alias = "gst_task_set_lock")]
pub fn lock_if_some(self, lock: Option<&TaskLock>) -> Self {
if let Some(lock) = lock {

View file

@ -1463,6 +1463,199 @@ impl GstValueExt for glib::Value {
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! impl_builder_gvalue_extra_setters (
(field) => {
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if the `predicate` evaluates to `true`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[inline]
pub fn field_if(self, name: impl $crate::glib::IntoGStr, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
if predicate {
self.field(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[inline]
pub fn field_if_some(self, name: impl $crate::glib::IntoGStr, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
if let Some(value) = value {
self.field(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s the `Item`s.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn field_from_iter<V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send>(
self,
name: impl $crate::glib::IntoGStr,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.field(name, V::from_iter(iter))
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s Item`s,
/// if `iter` is not empty.
///
/// This has no effect if `iter` is empty, i.e. previous value for `name` is unchanged.
#[inline]
pub fn field_if_not_empty<V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send>(
self,
name: impl $crate::glib::IntoGStr,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.field(name, V::from_iter(iter))
} else {
self
}
}
};
(other_field) => {
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if the `predicate` evaluates to `true`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[inline]
pub fn other_field_if(self, name: &'a str, value: impl $crate::glib::value::ToSendValue, predicate: bool) -> Self {
if predicate {
self.other_field(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[inline]
pub fn other_field_if_some(self, name: &'a str, value: Option<impl $crate::glib::value::ToSendValue>) -> Self {
if let Some(value) = value {
self.other_field(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s the `Item`s.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn other_field_from_iter<V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.other_field(name, V::from_iter(iter))
}
// rustdoc-stripper-ignore-next
/// Sets field `name` using the given `ValueType` `V` built from `iter`'s Item`s,
/// if `iter` is not empty.
///
/// This has no effect if `iter` is empty, i.e. previous value for `name` is unchanged.
#[inline]
pub fn other_field_if_not_empty<V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.other_field(name, V::from_iter(iter))
} else {
self
}
}
};
(property) => {
// rustdoc-stripper-ignore-next
/// Sets property `name` to the given inner value if the `predicate` evaluates to `true`.
///
/// This has no effect if the `predicate` evaluates to `false`,
/// i.e. default or previous value for `name` is kept.
#[inline]
pub fn property_if(self, name: &'a str, value: impl Into<$crate::glib::Value> + 'a, predicate: bool) -> Self {
if predicate {
self.property(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets property `name` to the given inner value if `value` is `Some`.
///
/// This has no effect if the value is `None`, i.e. default or previous value for `name` is kept.
#[inline]
pub fn property_if_some(self, name: &'a str, value: Option<impl Into<$crate::glib::Value> + 'a>) -> Self {
if let Some(value) = value {
self.property(name, value)
} else {
self
}
}
// rustdoc-stripper-ignore-next
/// Sets property `name` using the given `ValueType` `V` built from `iter`'s the `Item`s.
///
/// Overrides any default or previously defined value for `name`.
#[inline]
pub fn property_from_iter<V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let iter = iter.into_iter().map(|item| item.to_send_value());
self.property(name, V::from_iter(iter))
}
// rustdoc-stripper-ignore-next
/// Sets property `name` using the given `ValueType` `V` built from `iter`'s Item`s,
/// if `iter` is not empty.
///
/// This has no effect if `iter` is empty, i.e. previous value for `name` is unchanged.
#[inline]
pub fn property_if_not_empty<V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>>(
self,
name: &'a str,
iter: impl IntoIterator<Item = impl $crate::glib::value::ToSendValue>,
) -> Self {
let mut iter = iter.into_iter().peekable();
if iter.peek().is_some() {
let iter = iter.map(|item| item.to_send_value());
self.property(name, V::from_iter(iter))
} else {
self
}
}
};
);
#[cfg(test)]
mod tests {
use super::*;