Skip to content

builders: add field_if_not_empty and field_from_iter variants

François Laignel requested to merge fengalin/gstreamer-rs:field_if_any into main

This commit adds field_if_not_empty variants for IntoIterator builders fields. The field will only be set if the provided collection is not empty.

For Element properties and other Value based setters, the function takes a parameter to indicate the ValueType to use for the resulting Value.

This allows converting this code:

let webrtcsrc = gst::ElementFactory::make("webrtcsrc")
    .build()
    .unwrap();

if !args.audio_codecs.is_empty() {
    webrtcsrc.set_property("audio-codecs", gst::Array::new(&args.audio_codecs));
}

to:

let webrtcsrc = gst::ElementFactory::make("webrtcsrc")
    .field_if_not_empty::<gst::Array>("audio-codecs", &args.audio_codecs)
    .build()
    .unwrap();

Similarly, a new function field_from_iter() allows settings the property or field, regardless of whether it is empty or not:

let webrtcsrc = gst::ElementFactory::make("webrtcsrc")
    .property_from_iter::<gst::Array>("audio-codecs", &args.audio_codecs)
    .build()
    .unwrap();

The above will override the default value if args.audio_codecs is empty.

Edited by François Laignel

Merge request reports