From 4a015d94af3d903e1402d76c3df6c4c54329f8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 30 Oct 2023 10:57:38 +0200 Subject: [PATCH] Use let-else instead of match for weak reference upgrades Part-of: --- examples/src/bin/custom_events.rs | 5 ++--- examples/src/bin/decodebin.rs | 5 ++--- examples/src/bin/encodebin.rs | 5 ++--- examples/src/bin/events.rs | 5 ++--- examples/src/bin/queries.rs | 5 ++--- examples/src/bin/rtpfecclient.rs | 5 ++--- examples/src/bin/toc.rs | 5 ++--- gstreamer-app/src/app_sink.rs | 5 ++--- gstreamer-app/src/app_src.rs | 15 ++++++--------- tutorials/src/bin/basic-tutorial-12.rs | 5 ++--- tutorials/src/bin/basic-tutorial-8.rs | 20 ++++++++------------ tutorials/src/bin/playback-tutorial-3.rs | 5 ++--- tutorials/src/bin/playback-tutorial-4.rs | 10 ++++------ 13 files changed, 38 insertions(+), 57 deletions(-) diff --git a/examples/src/bin/custom_events.rs b/examples/src/bin/custom_events.rs index 591da1f08..b1d61773a 100644 --- a/examples/src/bin/custom_events.rs +++ b/examples/src/bin/custom_events.rs @@ -115,9 +115,8 @@ fn example_main() { glib::timeout_add_seconds(2 + i as u32, move || { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Break, + let Some(pipeline) = pipeline_weak.upgrade() else { + return glib::ControlFlow::Break; }; println!("Sending custom event to the pipeline with send_eos={send_eos}"); let ev = ExampleCustomEvent::new(*send_eos); diff --git a/examples/src/bin/decodebin.rs b/examples/src/bin/decodebin.rs index e07ccc5ea..e612957e6 100644 --- a/examples/src/bin/decodebin.rs +++ b/examples/src/bin/decodebin.rs @@ -90,9 +90,8 @@ fn example_main() -> Result<(), Error> { decodebin.connect_pad_added(move |dbin, src_pad| { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return, + let Some(pipeline) = pipeline_weak.upgrade() else { + return; }; // Try to detect whether the raw stream decodebin provided us with diff --git a/examples/src/bin/encodebin.rs b/examples/src/bin/encodebin.rs index eda62e1d4..e17e147aa 100644 --- a/examples/src/bin/encodebin.rs +++ b/examples/src/bin/encodebin.rs @@ -120,9 +120,8 @@ fn example_main() -> Result<(), Error> { src.connect_pad_added(move |dbin, dbin_src_pad| { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return, + let Some(pipeline) = pipeline_weak.upgrade() else { + return; }; let (is_audio, is_video) = { diff --git a/examples/src/bin/events.rs b/examples/src/bin/events.rs index 40f76a339..b76f6a081 100644 --- a/examples/src/bin/events.rs +++ b/examples/src/bin/events.rs @@ -55,9 +55,8 @@ fn example_main() { glib::timeout_add_seconds(5, move || { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Break, + let Some(pipeline) = pipeline_weak.upgrade() else { + return glib::ControlFlow::Break; }; println!("sending eos"); diff --git a/examples/src/bin/queries.rs b/examples/src/bin/queries.rs index 02eec3cd0..461629646 100644 --- a/examples/src/bin/queries.rs +++ b/examples/src/bin/queries.rs @@ -50,9 +50,8 @@ fn example_main() { let timeout_id = glib::timeout_add_seconds(1, move || { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Continue, + let Some(pipeline) = pipeline_weak.upgrade() else { + return glib::ControlFlow::Break; }; //let pos = pipeline.query_position(gst::Format::Time).unwrap_or(-1); diff --git a/examples/src/bin/rtpfecclient.rs b/examples/src/bin/rtpfecclient.rs index afdaef32b..36986c8bb 100644 --- a/examples/src/bin/rtpfecclient.rs +++ b/examples/src/bin/rtpfecclient.rs @@ -203,9 +203,8 @@ fn example_main() -> Result<(), Error> { let depay_weak = depay.downgrade(); rtpbin.connect_pad_added(move |rtpbin, src_pad| { - let depay = match depay_weak.upgrade() { - Some(depay) => depay, - None => return, + let Some(depay) = depay_weak.upgrade() else { + return; }; match connect_rtpbin_srcpad(src_pad, &depay) { diff --git a/examples/src/bin/toc.rs b/examples/src/bin/toc.rs index 9bfb1f6cb..64d64e0fc 100644 --- a/examples/src/bin/toc.rs +++ b/examples/src/bin/toc.rs @@ -52,9 +52,8 @@ fn example_main() { decodebin.connect_pad_added(move |_, src_pad| { // Here we temporarily retrieve a strong reference on the pipeline from the weak one // we moved into this callback. - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return, + let Some(pipeline) = pipeline_weak.upgrade() else { + return; }; // In this example, we are only interested about parsing the ToC, so diff --git a/gstreamer-app/src/app_sink.rs b/gstreamer-app/src/app_sink.rs index 93dcbb81c..9c28f47f2 100644 --- a/gstreamer-app/src/app_sink.rs +++ b/gstreamer-app/src/app_sink.rs @@ -1254,9 +1254,8 @@ impl Stream for AppSinkStream { fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll> { let mut waker = self.waker_reference.lock().unwrap(); - let app_sink = match self.app_sink.upgrade() { - Some(app_sink) => app_sink, - None => return Poll::Ready(None), + let Some(app_sink) = self.app_sink.upgrade() else { + return Poll::Ready(None); }; app_sink diff --git a/gstreamer-app/src/app_src.rs b/gstreamer-app/src/app_src.rs index d1c231563..90822573e 100644 --- a/gstreamer-app/src/app_src.rs +++ b/gstreamer-app/src/app_src.rs @@ -588,9 +588,8 @@ impl Sink for AppSrcSink { fn poll_ready(self: Pin<&mut Self>, context: &mut Context) -> Poll> { let mut waker = self.waker_reference.lock().unwrap(); - let app_src = match self.app_src.upgrade() { - Some(app_src) => app_src, - None => return Poll::Ready(Err(gst::FlowError::Eos)), + let Some(app_src) = self.app_src.upgrade() else { + return Poll::Ready(Err(gst::FlowError::Eos)); }; let current_level_bytes = app_src.current_level_bytes(); @@ -606,9 +605,8 @@ impl Sink for AppSrcSink { } fn start_send(self: Pin<&mut Self>, sample: gst::Sample) -> Result<(), Self::Error> { - let app_src = match self.app_src.upgrade() { - Some(app_src) => app_src, - None => return Err(gst::FlowError::Eos), + let Some(app_src) = self.app_src.upgrade() else { + return Err(gst::FlowError::Eos); }; app_src.push_sample(&sample)?; @@ -621,9 +619,8 @@ impl Sink for AppSrcSink { } fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll> { - let app_src = match self.app_src.upgrade() { - Some(app_src) => app_src, - None => return Poll::Ready(Ok(())), + let Some(app_src) = self.app_src.upgrade() else { + return Poll::Ready(Ok(())); }; app_src.end_of_stream()?; diff --git a/tutorials/src/bin/basic-tutorial-12.rs b/tutorials/src/bin/basic-tutorial-12.rs index 0fe05044f..42e3b87bf 100644 --- a/tutorials/src/bin/basic-tutorial-12.rs +++ b/tutorials/src/bin/basic-tutorial-12.rs @@ -24,9 +24,8 @@ fn tutorial_main() -> Result<(), Error> { let bus = pipeline.bus().expect("Pipeline has no bus"); let _bus_watch = bus .add_watch(move |_, msg| { - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Continue, + let Some(pipeline) = pipeline_weak.upgrade() else { + return glib::ControlFlow::Continue; }; let main_loop = &main_loop_clone; match msg.view() { diff --git a/tutorials/src/bin/basic-tutorial-8.rs b/tutorials/src/bin/basic-tutorial-8.rs index 741480c30..bd66c57ea 100644 --- a/tutorials/src/bin/basic-tutorial-8.rs +++ b/tutorials/src/bin/basic-tutorial-8.rs @@ -165,9 +165,8 @@ fn main() { appsrc.set_callbacks( gst_app::AppSrcCallbacks::builder() .need_data(move |_, _size| { - let data = match data_weak.upgrade() { - Some(data) => data, - None => return, + let Some(data) = data_weak.upgrade() else { + return; }; let mut d = data.lock().unwrap(); @@ -176,9 +175,8 @@ fn main() { let data_weak = Arc::downgrade(&data); d.source_id = Some(glib::source::idle_add(move || { - let data = match data_weak.upgrade() { - Some(data) => data, - None => return glib::ControlFlow::Break, + let Some(data) = data_weak.upgrade() else { + return glib::ControlFlow::Break; }; let (appsrc, buffer) = { @@ -224,9 +222,8 @@ fn main() { } }) .enough_data(move |_| { - let data = match data_weak2.upgrade() { - Some(data) => data, - None => return, + let Some(data) = data_weak2.upgrade() else { + return; }; let mut data = data.lock().unwrap(); @@ -242,9 +239,8 @@ fn main() { appsink.set_callbacks( gst_app::AppSinkCallbacks::builder() .new_sample(move |_| { - let data = match data_weak.upgrade() { - Some(data) => data, - None => return Ok(gst::FlowSuccess::Ok), + let Some(data) = data_weak.upgrade() else { + return Ok(gst::FlowSuccess::Ok); }; let appsink = { diff --git a/tutorials/src/bin/playback-tutorial-3.rs b/tutorials/src/bin/playback-tutorial-3.rs index 1273e722a..f16693910 100644 --- a/tutorials/src/bin/playback-tutorial-3.rs +++ b/tutorials/src/bin/playback-tutorial-3.rs @@ -82,9 +82,8 @@ fn tutorial_main() -> Result<(), Error> { let data_weak = Arc::downgrade(data); d.source_id = Some(glib::source::idle_add(move || { - let data = match data_weak.upgrade() { - Some(data) => data, - None => return glib::ControlFlow::Break, + let Some(data) = data_weak.upgrade() else { + return glib::ControlFlow::Break; }; let (appsrc, buffer) = { diff --git a/tutorials/src/bin/playback-tutorial-4.rs b/tutorials/src/bin/playback-tutorial-4.rs index 79c67c5be..bccc3ef93 100644 --- a/tutorials/src/bin/playback-tutorial-4.rs +++ b/tutorials/src/bin/playback-tutorial-4.rs @@ -56,9 +56,8 @@ fn tutorial_main() -> Result<(), Error> { use gst::MessageView; let buffering_level = &buffering_level_clone; - let pipeline = match pipeline_weak.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Break, + let Some(pipeline) = pipeline_weak.upgrade() else { + return glib::ControlFlow::Break; }; let main_loop = &main_loop_clone; match msg.view() { @@ -116,9 +115,8 @@ fn tutorial_main() -> Result<(), Error> { let timeout_id = glib::timeout_add_seconds(1, move || { use gst::{format::Percent, GenericFormattedValue as GFV}; - let pipeline = match pipeline_weak_.upgrade() { - Some(pipeline) => pipeline, - None => return glib::ControlFlow::Break, + let Some(pipeline) = pipeline_weak_.upgrade() else { + return glib::ControlFlow::Break; }; let mut graph = vec![b' '; GRAPH_LENGTH]; let mut buffering = gst::query::Buffering::new(gst::Format::Percent);