Use let-else instead of match for weak reference upgrades

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1333>
This commit is contained in:
Sebastian Dröge 2023-10-30 10:57:38 +02:00
parent fc4a0d29c6
commit 4a015d94af
13 changed files with 38 additions and 57 deletions

View file

@ -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);

View file

@ -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

View file

@ -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) = {

View file

@ -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");

View file

@ -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);

View file

@ -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) {

View file

@ -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

View file

@ -1254,9 +1254,8 @@ impl Stream for AppSinkStream {
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<Self::Item>> {
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

View file

@ -588,9 +588,8 @@ impl Sink<gst::Sample> for AppSrcSink {
fn poll_ready(self: Pin<&mut Self>, context: &mut Context) -> Poll<Result<(), Self::Error>> {
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<gst::Sample> 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<gst::Sample> for AppSrcSink {
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
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()?;

View file

@ -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() {

View file

@ -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 = {

View file

@ -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) = {

View file

@ -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);