From d5e24b9fbdb1cb7d7370dfbbdcd6321f46732faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 22 Jan 2023 09:48:51 +0200 Subject: [PATCH] Update various object construction functions to more efficient approaches Part-of: --- examples/src/bin/cairo_compositor.rs | 2 +- examples/src/bin/glfilter.rs | 2 +- examples/src/bin/rtsp-server-subclass.rs | 8 +- examples/src/bin/subclass.rs | 2 +- gstreamer-app/src/app_sink.rs | 237 ++++++++++----------- gstreamer-app/src/app_src.rs | 254 ++++++++++------------- gstreamer/src/bin.rs | 50 ++--- gstreamer/src/element_factory.rs | 22 +- gstreamer/src/pad.rs | 23 +- gstreamer/src/pipeline.rs | 83 +++----- gstreamer/src/subclass/allocator.rs | 2 +- gstreamer/src/subclass/buffer_pool.rs | 2 +- gstreamer/src/subclass/element.rs | 2 +- gstreamer/src/subclass/pad.rs | 5 +- gstreamer/src/subclass/task_pool.rs | 2 +- 15 files changed, 304 insertions(+), 392 deletions(-) diff --git a/examples/src/bin/cairo_compositor.rs b/examples/src/bin/cairo_compositor.rs index 1ec6c2f2e..d17a919ab 100644 --- a/examples/src/bin/cairo_compositor.rs +++ b/examples/src/bin/cairo_compositor.rs @@ -400,7 +400,7 @@ mod cairo_compositor { impl CairoCompositor { // Creates a new instance of our compositor with the given name. pub fn new(name: Option<&str>) -> Self { - glib::Object::new(&[("name", &name)]) + glib::Object::builder().property("name", name).build() } } diff --git a/examples/src/bin/glfilter.rs b/examples/src/bin/glfilter.rs index c6138d6a4..f6b5b5000 100644 --- a/examples/src/bin/glfilter.rs +++ b/examples/src/bin/glfilter.rs @@ -54,7 +54,7 @@ mod mirror { impl GLMirrorFilter { pub fn new(name: Option<&str>) -> Self { - glib::Object::new(&[("name", &name)]) + glib::Object::builder().property("name", name).build() } } diff --git a/examples/src/bin/rtsp-server-subclass.rs b/examples/src/bin/rtsp-server-subclass.rs index 95ab2d583..7726391ef 100644 --- a/examples/src/bin/rtsp-server-subclass.rs +++ b/examples/src/bin/rtsp-server-subclass.rs @@ -154,7 +154,7 @@ mod media_factory { impl Default for Factory { // Creates a new instance of our factory fn default() -> Factory { - glib::Object::new(&[]) + glib::Object::new_default() } } } @@ -266,7 +266,7 @@ mod server { impl Default for Server { // Creates a new instance of our factory fn default() -> Server { - glib::Object::new(&[]) + glib::Object::new_default() } } } @@ -315,7 +315,7 @@ mod client { impl Default for Client { // Creates a new instance of our factory fn default() -> Client { - glib::Object::new(&[]) + glib::Object::new_default() } } } @@ -359,7 +359,7 @@ mod mount_points { impl Default for MountPoints { // Creates a new instance of our factory fn default() -> Self { - glib::Object::new(&[]) + glib::Object::new_default() } } } diff --git a/examples/src/bin/subclass.rs b/examples/src/bin/subclass.rs index 32397068c..45f14712d 100644 --- a/examples/src/bin/subclass.rs +++ b/examples/src/bin/subclass.rs @@ -216,7 +216,7 @@ mod fir_filter { impl FirFilter { // Creates a new instance of our filter with the given name pub fn new(name: Option<&str>) -> FirFilter { - glib::Object::new(&[("name", &name)]) + glib::Object::builder().property("name", name).build() } // Sets the coefficients by getting access to the private diff --git a/gstreamer-app/src/app_sink.rs b/gstreamer-app/src/app_sink.rs index b1c352da9..572fcf5f1 100644 --- a/gstreamer-app/src/app_sink.rs +++ b/gstreamer-app/src/app_sink.rs @@ -277,7 +277,7 @@ impl AppSink { /// This method returns an instance of [`AppSinkBuilder`](crate::builders::AppSinkBuilder) which can be used to create [`AppSink`] objects. pub fn builder() -> AppSinkBuilder { assert_initialized_main_thread!(); - AppSinkBuilder::default() + AppSinkBuilder::new() } #[doc(alias = "gst_app_sink_set_callbacks")] @@ -931,100 +931,31 @@ impl AppSink { } } -#[derive(Default)] // rustdoc-stripper-ignore-next /// A [builder-pattern] type to construct [`AppSink`] objects. /// /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html #[must_use = "The builder must be built to be used"] pub struct AppSinkBuilder { - async_: Option, - buffer_list: Option, + builder: glib::object::ObjectBuilder<'static, AppSink>, callbacks: Option, - caps: Option, - drop: Option, drop_out_of_segment: Option, - enable_last_sample: Option, - max_bitrate: Option, - max_buffers: Option, - max_lateness: Option, - #[cfg(any(feature = "v1_16", feature = "dox"))] - processing_deadline: Option, - qos: Option, - render_delay: Option>, - sync: Option, - throttle_time: Option, - ts_offset: Option, - wait_on_eos: Option, - name: Option, } impl AppSinkBuilder { - // rustdoc-stripper-ignore-next - /// Create a new [`AppSinkBuilder`]. - pub fn new() -> Self { - Self::default() + fn new() -> Self { + Self { + builder: glib::Object::builder(), + callbacks: None, + drop_out_of_segment: None, + } } // rustdoc-stripper-ignore-next /// Build the [`AppSink`]. #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"] pub fn build(self) -> AppSink { - let mut properties: Vec<(&str, &dyn ToValue)> = vec![]; - if let Some(ref async_) = self.async_ { - properties.push(("async", async_)); - } - if let Some(ref buffer_list) = self.buffer_list { - properties.push(("buffer-list", buffer_list)); - } - if let Some(ref caps) = self.caps { - properties.push(("caps", caps)); - } - if let Some(ref drop) = self.drop { - properties.push(("drop", drop)); - } - if let Some(ref enable_last_sample) = self.enable_last_sample { - properties.push(("enable-last-sample", enable_last_sample)); - } - if let Some(ref max_bitrate) = self.max_bitrate { - properties.push(("max-bitrate", max_bitrate)); - } - if let Some(ref max_buffers) = self.max_buffers { - properties.push(("max-buffers", max_buffers)); - } - if let Some(ref max_lateness) = self.max_lateness { - properties.push(("max-lateness", max_lateness)); - } - #[cfg(any(feature = "v1_16", feature = "dox"))] - if let Some(ref processing_deadline) = self.processing_deadline { - properties.push(("processing-deadline", processing_deadline)); - } - if let Some(ref qos) = self.qos { - properties.push(("qos", qos)); - } - if let Some(ref render_delay) = self.render_delay { - properties.push(("render-delay", render_delay)); - } - if let Some(ref sync) = self.sync { - properties.push(("sync", sync)); - } - if let Some(ref throttle_time) = self.throttle_time { - properties.push(("throttle-time", throttle_time)); - } - if let Some(ref ts_offset) = self.ts_offset { - properties.push(("ts-offset", ts_offset)); - } - if let Some(ref qos) = self.qos { - properties.push(("qos", qos)); - } - if let Some(ref wait_on_eos) = self.wait_on_eos { - properties.push(("wait-on-eos", wait_on_eos)); - } - if let Some(ref name) = self.name { - properties.push(("name", name)); - } - - let appsink = glib::Object::new::(&properties); + let appsink = self.builder.build(); if let Some(callbacks) = self.callbacks { appsink.set_callbacks(callbacks); @@ -1037,96 +968,138 @@ impl AppSinkBuilder { appsink } - pub fn async_(mut self, async_: bool) -> Self { - self.async_ = Some(async_); - self + pub fn async_(self, async_: bool) -> Self { + Self { + builder: self.builder.property("async", async_), + ..self + } } - pub fn buffer_list(mut self, buffer_list: bool) -> Self { - self.buffer_list = Some(buffer_list); - self + pub fn buffer_list(self, buffer_list: bool) -> Self { + Self { + builder: self.builder.property("buffer-list", buffer_list), + ..self + } } - pub fn callbacks(mut self, callbacks: AppSinkCallbacks) -> Self { - self.callbacks = Some(callbacks); - self + pub fn callbacks(self, callbacks: AppSinkCallbacks) -> Self { + Self { + callbacks: Some(callbacks), + ..self + } } - pub fn caps(mut self, caps: &gst::Caps) -> Self { - self.caps = Some(caps.clone()); - self + pub fn caps(self, caps: &gst::Caps) -> Self { + Self { + builder: self.builder.property("caps", caps), + ..self + } } - pub fn drop(mut self, drop: bool) -> Self { - self.drop = Some(drop); - self + pub fn drop(self, drop: bool) -> Self { + Self { + builder: self.builder.property("drop", drop), + ..self + } } - pub fn drop_out_of_segment(mut self, drop_out_of_segment: bool) -> Self { - self.drop_out_of_segment = Some(drop_out_of_segment); - self + pub fn drop_out_of_segment(self, drop_out_of_segment: bool) -> Self { + Self { + builder: self + .builder + .property("drop-out-of-segment", drop_out_of_segment), + ..self + } } - pub fn enable_last_sample(mut self, enable_last_sample: bool) -> Self { - self.enable_last_sample = Some(enable_last_sample); - self + pub fn enable_last_sample(self, enable_last_sample: bool) -> Self { + Self { + builder: self + .builder + .property("enable-last-sample", enable_last_sample), + ..self + } } - pub fn max_bitrate(mut self, max_bitrate: u64) -> Self { - self.max_bitrate = Some(max_bitrate); - self + pub fn max_bitrate(self, max_bitrate: u64) -> Self { + Self { + builder: self.builder.property("max-bitrate", max_bitrate), + ..self + } } - pub fn max_buffers(mut self, max_buffers: u32) -> Self { - self.max_buffers = Some(max_buffers); - self + pub fn max_buffers(self, max_buffers: u32) -> Self { + Self { + builder: self.builder.property("max-buffers", max_buffers), + ..self + } } - pub fn max_lateness(mut self, max_lateness: i64) -> Self { - self.max_lateness = Some(max_lateness); - self + pub fn max_lateness(self, max_lateness: i64) -> Self { + Self { + builder: self.builder.property("max-lateness", max_lateness), + ..self + } } #[cfg(any(feature = "v1_16", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))] - pub fn processing_deadline(mut self, processing_deadline: i64) -> Self { - self.processing_deadline = Some(processing_deadline); - self + pub fn processing_deadline(self, processing_deadline: i64) -> Self { + Self { + builder: self + .builder + .property("processing-deadline", processing_deadline), + ..self + } } - pub fn qos(mut self, qos: bool) -> Self { - self.qos = Some(qos); - self + pub fn qos(self, qos: bool) -> Self { + Self { + builder: self.builder.property("qos", qos), + ..self + } } - pub fn render_delay(mut self, render_delay: Option) -> Self { - self.render_delay = Some(render_delay); - self + pub fn render_delay(self, render_delay: Option) -> Self { + Self { + builder: self.builder.property("render-delay", render_delay), + ..self + } } - pub fn sync(mut self, sync: bool) -> Self { - self.sync = Some(sync); - self + pub fn sync(self, sync: bool) -> Self { + Self { + builder: self.builder.property("sync", sync), + ..self + } } - pub fn throttle_time(mut self, throttle_time: u64) -> Self { - self.throttle_time = Some(throttle_time); - self + pub fn throttle_time(self, throttle_time: u64) -> Self { + Self { + builder: self.builder.property("throttle-time", throttle_time), + ..self + } } - pub fn ts_offset(mut self, ts_offset: gst::ClockTimeDiff) -> Self { - self.ts_offset = Some(ts_offset); - self + pub fn ts_offset(self, ts_offset: gst::ClockTimeDiff) -> Self { + Self { + builder: self.builder.property("ts-offset", ts_offset), + ..self + } } - pub fn wait_on_eos(mut self, wait_on_eos: bool) -> Self { - self.wait_on_eos = Some(wait_on_eos); - self + pub fn wait_on_eos(self, wait_on_eos: bool) -> Self { + Self { + builder: self.builder.property("wait_on_eos", wait_on_eos), + ..self + } } - pub fn name(mut self, name: &str) -> Self { - self.name = Some(name.to_string()); - self + pub fn name(self, name: impl Into) -> Self { + Self { + builder: self.builder.property("name", name.into()), + ..self + } } } diff --git a/gstreamer-app/src/app_src.rs b/gstreamer-app/src/app_src.rs index 9560275ea..7e1966801 100644 --- a/gstreamer-app/src/app_src.rs +++ b/gstreamer-app/src/app_src.rs @@ -218,7 +218,7 @@ impl AppSrc { /// This method returns an instance of [`AppSrcBuilder`](crate::builders::AppSrcBuilder) which can be used to create [`AppSrc`] objects. pub fn builder() -> AppSrcBuilder { assert_initialized_main_thread!(); - AppSrcBuilder::default() + AppSrcBuilder::new() } #[doc(alias = "gst_app_src_set_callbacks")] @@ -353,111 +353,31 @@ impl AppSrc { } } -#[derive(Default)] // rustdoc-stripper-ignore-next /// A [builder-pattern] type to construct [`AppSrc`] objects. /// /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html #[must_use = "The builder must be built to be used"] pub struct AppSrcBuilder { - automatic_eos: Option, - block: Option, + builder: glib::object::ObjectBuilder<'static, AppSrc>, callbacks: Option, - caps: Option, - do_timestamp: Option, - duration: Option, - format: Option, - #[cfg(any(feature = "v1_18", feature = "dox"))] - #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] - handle_segment_change: Option, - is_live: Option, - #[cfg(any(feature = "v1_20", feature = "dox"))] - #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - leaky_type: Option, - #[cfg(any(feature = "v1_20", feature = "dox"))] - #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - max_buffers: Option, - max_bytes: Option, - max_latency: Option, - #[cfg(any(feature = "v1_20", feature = "dox"))] - #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - max_time: Option>, - min_latency: Option, - min_percent: Option, - size: Option, - stream_type: Option, - name: Option, + automatic_eos: Option, } impl AppSrcBuilder { - // rustdoc-stripper-ignore-next - /// Create a new [`AppSrcBuilder`]. - pub fn new() -> Self { - Self::default() + fn new() -> Self { + Self { + builder: glib::Object::builder(), + callbacks: None, + automatic_eos: None, + } } // rustdoc-stripper-ignore-next /// Build the [`AppSrc`]. #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"] pub fn build(self) -> AppSrc { - let mut properties: Vec<(&str, &dyn ToValue)> = vec![]; - if let Some(ref block) = self.block { - properties.push(("block", block)); - } - if let Some(ref caps) = self.caps { - properties.push(("caps", caps)); - } - if let Some(ref do_timestamp) = self.do_timestamp { - properties.push(("do-timestamp", do_timestamp)); - } - if let Some(ref duration) = self.duration { - properties.push(("duration", duration)); - } - if let Some(ref format) = self.format { - properties.push(("format", format)); - } - #[cfg(any(feature = "v1_18", feature = "dox"))] - if let Some(ref handle_segment_change) = self.handle_segment_change { - properties.push(("handle-segment-change", handle_segment_change)); - } - if let Some(ref is_live) = self.is_live { - properties.push(("is-live", is_live)); - } - #[cfg(any(feature = "v1_20", feature = "dox"))] - if let Some(ref leaky_type) = self.leaky_type { - properties.push(("leaky-type", leaky_type)); - } - #[cfg(any(feature = "v1_20", feature = "dox"))] - if let Some(ref max_buffers) = self.max_buffers { - properties.push(("max-buffers", max_buffers)); - } - if let Some(ref max_bytes) = self.max_bytes { - properties.push(("max-bytes", max_bytes)); - } - if let Some(ref max_latency) = self.max_latency { - properties.push(("max-latency", max_latency)); - } - #[cfg(any(feature = "v1_20", feature = "dox"))] - if let Some(ref max_time) = self.max_time { - properties.push(("max-time", max_time)); - } - if let Some(ref min_latency) = self.min_latency { - properties.push(("min-latency", min_latency)); - } - if let Some(ref min_percent) = self.min_percent { - properties.push(("min-percent", min_percent)); - } - if let Some(ref size) = self.size { - properties.push(("size", size)); - } - if let Some(ref stream_type) = self.stream_type { - properties.push(("stream-type", stream_type)); - } - if let Some(ref name) = self.name { - properties.push(("name", name)); - } - - let appsrc = glib::Object::new::(&properties); + let appsrc = self.builder.build(); if let Some(callbacks) = self.callbacks { appsrc.set_callbacks(callbacks); @@ -470,107 +390,147 @@ impl AppSrcBuilder { appsrc } - pub fn automatic_eos(mut self, automatic_eos: bool) -> Self { - self.automatic_eos = Some(automatic_eos); - self + pub fn automatic_eos(self, automatic_eos: bool) -> Self { + Self { + automatic_eos: Some(automatic_eos), + ..self + } } - pub fn block(mut self, block: bool) -> Self { - self.block = Some(block); - self + pub fn block(self, block: bool) -> Self { + Self { + builder: self.builder.property("block", block), + ..self + } } - pub fn callbacks(mut self, callbacks: AppSrcCallbacks) -> Self { - self.callbacks = Some(callbacks); - self + pub fn callbacks(self, callbacks: AppSrcCallbacks) -> Self { + Self { + callbacks: Some(callbacks), + ..self + } } - pub fn caps(mut self, caps: &gst::Caps) -> Self { - self.caps = Some(caps.clone()); - self + pub fn caps(self, caps: &gst::Caps) -> Self { + Self { + builder: self.builder.property("caps", caps), + ..self + } } - pub fn do_timestamp(mut self, do_timestamp: bool) -> Self { - self.do_timestamp = Some(do_timestamp); - self + pub fn do_timestamp(self, do_timestamp: bool) -> Self { + Self { + builder: self.builder.property("do-timestamp", do_timestamp), + ..self + } } - pub fn duration(mut self, duration: u64) -> Self { - self.duration = Some(duration); - self + pub fn duration(self, duration: u64) -> Self { + Self { + builder: self.builder.property("duration", duration), + ..self + } } - pub fn format(mut self, format: gst::Format) -> Self { - self.format = Some(format); - self + pub fn format(self, format: gst::Format) -> Self { + Self { + builder: self.builder.property("format", format), + ..self + } } #[cfg(any(feature = "v1_18", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] - pub fn handle_segment_change(mut self, handle_segment_change: bool) -> Self { - self.handle_segment_change = Some(handle_segment_change); - self + pub fn handle_segment_change(self, handle_segment_change: bool) -> Self { + Self { + builder: self + .builder + .property("handle-segment-change", handle_segment_change), + ..self + } } - pub fn is_live(mut self, is_live: bool) -> Self { - self.is_live = Some(is_live); - self + pub fn is_live(self, is_live: bool) -> Self { + Self { + builder: self.builder.property("is-live", is_live), + ..self + } } #[cfg(any(feature = "v1_20", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - pub fn leaky_type(mut self, leaky_type: crate::AppLeakyType) -> Self { - self.leaky_type = Some(leaky_type); - self + pub fn leaky_type(self, leaky_type: crate::AppLeakyType) -> Self { + Self { + builder: self.builder.property("leaky-type", leaky_type), + ..self + } } #[cfg(any(feature = "v1_20", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - pub fn max_buffers(mut self, max_buffers: u64) -> Self { - self.max_buffers = Some(max_buffers); - self + pub fn max_buffers(self, max_buffers: u64) -> Self { + Self { + builder: self.builder.property("max-buffers", max_buffers), + ..self + } } - pub fn max_bytes(mut self, max_bytes: u64) -> Self { - self.max_bytes = Some(max_bytes); - self + pub fn max_bytes(self, max_bytes: u64) -> Self { + Self { + builder: self.builder.property("max-bytes", max_bytes), + ..self + } } - pub fn max_latency(mut self, max_latency: i64) -> Self { - self.max_latency = Some(max_latency); - self + pub fn max_latency(self, max_latency: i64) -> Self { + Self { + builder: self.builder.property("max-latency", max_latency), + ..self + } } #[cfg(any(feature = "v1_20", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_20")))] - pub fn max_time(mut self, max_time: Option) -> Self { - self.max_time = Some(max_time); - self + pub fn max_time(self, max_time: Option) -> Self { + Self { + builder: self.builder.property("max-time", max_time), + ..self + } } - pub fn min_latency(mut self, min_latency: i64) -> Self { - self.min_latency = Some(min_latency); - self + pub fn min_latency(self, min_latency: i64) -> Self { + Self { + builder: self.builder.property("min-latency", min_latency), + ..self + } } - pub fn min_percent(mut self, min_percent: u32) -> Self { - self.min_percent = Some(min_percent); - self + pub fn min_percent(self, min_percent: u32) -> Self { + Self { + builder: self.builder.property("min-percent", min_percent), + ..self + } } - pub fn size(mut self, size: i64) -> Self { - self.size = Some(size); - self + pub fn size(self, size: i64) -> Self { + Self { + builder: self.builder.property("size", size), + ..self + } } - pub fn stream_type(mut self, stream_type: crate::AppStreamType) -> Self { - self.stream_type = Some(stream_type); - self + pub fn stream_type(self, stream_type: crate::AppStreamType) -> Self { + Self { + builder: self.builder.property("stream-type", stream_type), + ..self + } } - pub fn name(mut self, name: &str) -> Self { - self.name = Some(name.to_string()); - self + pub fn name(self, name: impl Into) -> Self { + Self { + builder: self.builder.property("name", name.into()), + ..self + } } } diff --git a/gstreamer/src/bin.rs b/gstreamer/src/bin.rs index 21a99e679..19858881a 100644 --- a/gstreamer/src/bin.rs +++ b/gstreamer/src/bin.rs @@ -17,7 +17,7 @@ impl Bin { /// /// This method returns an instance of [`BinBuilder`](crate::builders::BinBuilder) which can be used to create [`Bin`] objects. pub fn builder() -> BinBuilder { - BinBuilder::default() + BinBuilder::new() } } @@ -223,59 +223,49 @@ impl> GstBinExtManual for O { impl Default for Bin { fn default() -> Self { - glib::object::Object::new::(&[]) + glib::object::Object::new_default() } } -#[derive(Clone, Default)] // rustdoc-stripper-ignore-next /// A [builder-pattern] type to construct [`Bin`] objects. /// /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html #[must_use = "The builder must be built to be used"] pub struct BinBuilder { - async_handling: Option, - message_forward: Option, - name: Option, + builder: glib::object::ObjectBuilder<'static, Bin>, } impl BinBuilder { - // rustdoc-stripper-ignore-next - /// Create a new [`BinBuilder`]. - pub fn new() -> Self { - Self::default() + fn new() -> Self { + Self { + builder: glib::Object::builder(), + } } // rustdoc-stripper-ignore-next /// Build the [`Bin`]. #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"] pub fn build(self) -> Bin { - let mut properties: Vec<(&str, &dyn ToValue)> = vec![]; - if let Some(ref async_handling) = self.async_handling { - properties.push(("async-handling", async_handling)); - } - if let Some(ref message_forward) = self.message_forward { - properties.push(("message-forward", message_forward)); - } - if let Some(ref name) = self.name { - properties.push(("name", name)); - } - glib::Object::new::(&properties) + self.builder.build() } - pub fn async_handling(mut self, async_handling: bool) -> Self { - self.async_handling = Some(async_handling); - self + pub fn async_handling(self, async_handling: bool) -> Self { + Self { + builder: self.builder.property("async-handling", async_handling), + } } - pub fn message_forward(mut self, message_forward: bool) -> Self { - self.message_forward = Some(message_forward); - self + pub fn message_forward(self, message_forward: bool) -> Self { + Self { + builder: self.builder.property("message-forward", message_forward), + } } - pub fn name(mut self, name: &str) -> Self { - self.name = Some(name.to_string()); - self + pub fn name(self, name: impl Into) -> Self { + Self { + builder: self.builder.property("name", name.into()), + } } } diff --git a/gstreamer/src/element_factory.rs b/gstreamer/src/element_factory.rs index f46a0aa24..80603631b 100644 --- a/gstreamer/src/element_factory.rs +++ b/gstreamer/src/element_factory.rs @@ -48,7 +48,7 @@ impl ElementFactory { ElementBuilder { name_or_factory: NameOrFactory::Factory(self), - properties: Vec::new(), + properties: smallvec::SmallVec::new(), } } @@ -60,7 +60,7 @@ impl ElementFactory { ElementBuilder { name_or_factory: NameOrFactory::Name(factoryname), - properties: Vec::new(), + properties: smallvec::SmallVec::new(), } } @@ -219,7 +219,7 @@ impl ElementFactory { #[must_use = "The builder must be built to be used"] pub struct ElementBuilder<'a> { name_or_factory: NameOrFactory<'a>, - properties: Vec<(&'a str, ValueOrStr<'a>)>, + properties: smallvec::SmallVec<[(&'a str, ValueOrStr<'a>); 16]>, } #[derive(Copy, Clone)] @@ -236,12 +236,14 @@ enum ValueOrStr<'a> { impl<'a> ElementBuilder<'a> { // rustdoc-stripper-ignore-next /// Sets the name property to the given `name`. - pub fn name(self, name: &'a str) -> Self { - self.property("name", name) + #[inline] + pub fn name(self, name: impl Into) -> Self { + self.property("name", name.into()) } // rustdoc-stripper-ignore-next /// Set property `name` to the given value `value`. + #[inline] pub fn property(self, name: &'a str, value: impl Into + 'a) -> Self { Self { name_or_factory: self.name_or_factory, @@ -255,6 +257,7 @@ impl<'a> ElementBuilder<'a> { // rustdoc-stripper-ignore-next /// Set property `name` to the given string value `value`. + #[inline] pub fn property_from_str(self, name: &'a str, value: &'a str) -> Self { Self { name_or_factory: self.name_or_factory, @@ -328,7 +331,7 @@ impl<'a> ElementBuilder<'a> { )); } - let mut properties = Vec::with_capacity(self.properties.len()); + let mut properties = smallvec::SmallVec::<[_; 16]>::with_capacity(self.properties.len()); let klass = glib::Class::::from_type(element_type).unwrap(); for (name, value) in self.properties { match value { @@ -386,9 +389,10 @@ impl<'a> ElementBuilder<'a> { } } - let element = glib::Object::with_values(element_type, &properties) - .downcast::() - .unwrap(); + let element = unsafe { + glib::Object::with_mut_values(element_type, &mut properties) + .unsafe_cast::() + }; unsafe { use std::sync::atomic; diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index f460668f9..d87967d36 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -1617,7 +1617,10 @@ impl + IsA + glib::object::IsClass> PadBuilder { pub fn new(name: Option<&str>, direction: crate::PadDirection) -> Self { assert_initialized_main_thread!(); - let pad = glib::Object::new::(&[("name", &name), ("direction", &direction)]); + let pad = glib::Object::builder::() + .property("name", name) + .property("direction", direction) + .build(); // Ghost pads are a bit special if let Some(pad) = pad.dynamic_cast_ref::() { @@ -1660,16 +1663,14 @@ impl + IsA + glib::object::IsClass> PadBuilder { } } - let pad = glib::Object::with_type( - type_, - &[ - ("name", &name), - ("direction", &templ.direction()), - ("template", templ), - ], - ) - .downcast::() - .unwrap(); + let mut properties = [ + ("name", name.map(glib::GString::from).into()), + ("direction", templ.direction().into()), + ("template", templ.into()), + ]; + + let pad = + unsafe { glib::Object::with_mut_values(type_, &mut properties).unsafe_cast::() }; // Ghost pads are a bit special if let Some(pad) = pad.dynamic_cast_ref::() { diff --git a/gstreamer/src/pipeline.rs b/gstreamer/src/pipeline.rs index ebb33a557..5c0f88cf9 100644 --- a/gstreamer/src/pipeline.rs +++ b/gstreamer/src/pipeline.rs @@ -10,7 +10,7 @@ impl Pipeline { /// /// This method returns an instance of [`PipelineBuilder`](crate::builders::PipelineBuilder) which can be used to create [`Pipeline`] objects. pub fn builder() -> PipelineBuilder { - PipelineBuilder::default() + PipelineBuilder::new() } } @@ -51,85 +51,66 @@ impl> GstPipelineExtManual for O { impl Default for Pipeline { fn default() -> Self { - glib::object::Object::new::(&[]) + glib::object::Object::new_default() } } -#[derive(Clone, Default)] // rustdoc-stripper-ignore-next /// A [builder-pattern] type to construct [`Pipeline`] objects. /// /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html #[must_use = "The builder must be built to be used"] pub struct PipelineBuilder { - auto_flush_bus: Option, - delay: Option, - latency: Option, - async_handling: Option, - message_forward: Option, - name: Option, + builder: glib::object::ObjectBuilder<'static, Pipeline>, } impl PipelineBuilder { - // rustdoc-stripper-ignore-next - /// Create a new [`PipelineBuilder`]. - pub fn new() -> Self { - Self::default() + fn new() -> Self { + Self { + builder: glib::Object::builder(), + } } // rustdoc-stripper-ignore-next /// Build the [`Pipeline`]. #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"] pub fn build(self) -> Pipeline { - let mut properties: Vec<(&str, &dyn ToValue)> = vec![]; - if let Some(ref auto_flush_bus) = self.auto_flush_bus { - properties.push(("auto-flush-bus", auto_flush_bus)); - } - if let Some(ref delay) = self.delay { - properties.push(("delay", delay)); - } - if let Some(ref latency) = self.latency { - properties.push(("latency", latency)); - } - if let Some(ref async_handling) = self.async_handling { - properties.push(("async-handling", async_handling)); - } - if let Some(ref message_forward) = self.message_forward { - properties.push(("message-forward", message_forward)); - } - if let Some(ref name) = self.name { - properties.push(("name", name)); - } - glib::Object::new::(&properties) + self.builder.build() } - pub fn auto_flush_bus(mut self, auto_flush_bus: bool) -> Self { - self.auto_flush_bus = Some(auto_flush_bus); - self + pub fn auto_flush_bus(self, auto_flush_bus: bool) -> Self { + Self { + builder: self.builder.property("auto-flush-bus", auto_flush_bus), + } } - pub fn delay(mut self, delay: u64) -> Self { - self.delay = Some(delay); - self + pub fn delay(self, delay: u64) -> Self { + Self { + builder: self.builder.property("delay", delay), + } } - pub fn latency(mut self, latency: u64) -> Self { - self.latency = Some(latency); - self + pub fn latency(self, latency: crate::ClockTime) -> Self { + Self { + builder: self.builder.property("latency", latency), + } } - pub fn async_handling(mut self, async_handling: bool) -> Self { - self.async_handling = Some(async_handling); - self + pub fn async_handling(self, async_handling: bool) -> Self { + Self { + builder: self.builder.property("async-handling", async_handling), + } } - pub fn message_forward(mut self, message_forward: bool) -> Self { - self.message_forward = Some(message_forward); - self + pub fn message_forward(self, message_forward: bool) -> Self { + Self { + builder: self.builder.property("message-forward", message_forward), + } } - pub fn name(mut self, name: &str) -> Self { - self.name = Some(name.to_string()); - self + pub fn name(self, name: impl Into) -> Self { + Self { + builder: self.builder.property("name", name.into()), + } } } diff --git a/gstreamer/src/subclass/allocator.rs b/gstreamer/src/subclass/allocator.rs index 5a1651061..8623640c7 100644 --- a/gstreamer/src/subclass/allocator.rs +++ b/gstreamer/src/subclass/allocator.rs @@ -150,7 +150,7 @@ mod tests { impl Default for TestAllocator { fn default() -> Self { - glib::Object::new(&[]) + glib::Object::new_default() } } diff --git a/gstreamer/src/subclass/buffer_pool.rs b/gstreamer/src/subclass/buffer_pool.rs index bc329de00..1a999f9dd 100644 --- a/gstreamer/src/subclass/buffer_pool.rs +++ b/gstreamer/src/subclass/buffer_pool.rs @@ -517,7 +517,7 @@ mod tests { impl Default for TestBufferPool { fn default() -> Self { - glib::Object::new(&[]) + glib::Object::new_default() } } diff --git a/gstreamer/src/subclass/element.rs b/gstreamer/src/subclass/element.rs index 7a1b489a4..6c027e4a5 100644 --- a/gstreamer/src/subclass/element.rs +++ b/gstreamer/src/subclass/element.rs @@ -708,7 +708,7 @@ mod tests { impl TestElement { pub fn new(name: Option<&str>) -> Self { - glib::Object::new(&[("name", &name)]) + glib::Object::builder().property("name", name).build() } } diff --git a/gstreamer/src/subclass/pad.rs b/gstreamer/src/subclass/pad.rs index 3800fa34b..53a28bc0a 100644 --- a/gstreamer/src/subclass/pad.rs +++ b/gstreamer/src/subclass/pad.rs @@ -126,7 +126,10 @@ mod tests { impl TestPad { pub fn new(name: &str, direction: PadDirection) -> Self { - glib::Object::new(&[("name", &name), ("direction", &direction)]) + glib::Object::builder() + .property("name", name) + .property("direction", direction) + .build() } } diff --git a/gstreamer/src/subclass/task_pool.rs b/gstreamer/src/subclass/task_pool.rs index 5fa84b382..04440e2a4 100644 --- a/gstreamer/src/subclass/task_pool.rs +++ b/gstreamer/src/subclass/task_pool.rs @@ -306,7 +306,7 @@ mod tests { impl Default for TestPool { fn default() -> Self { - glib::Object::new(&[]) + glib::Object::new_default() } }