From f3096ac76a5e7c11f48d8ef1901a68dfe3e44f65 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 16 Jul 2023 15:00:09 -0500 Subject: [PATCH] Simplify select with guards --- src/upload.rs | 68 +++++++++------------------------------------------ 1 file changed, 11 insertions(+), 57 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index bf81086..1062e3d 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -44,6 +44,10 @@ where self.1 = opt.is_none(); opt } + + fn is_closed(&self) -> bool { + self.1 + } } struct ReceiverStream(Receiver); @@ -301,8 +305,6 @@ where let mut m = Streamer(m, false); - let mut stream_finished = false; - let mut error: Option = None; let mut provided_error: Option = None; @@ -319,63 +321,15 @@ where } } - break 'outer; - } - - if stream_finished { - while let Some(res) = set.join_next().await { - let (name_parts, content) = match res { - Ok(Ok(Ok(tup))) => tup, - Ok(Ok(Err(e))) => { - provided_error = Some(e); - continue 'outer; - } - Ok(Err(e)) => { - error = Some(e); - continue 'outer; - } - Err(e) => { - error = Some(e.into()); - continue 'outer; - } - }; - - let (l, r) = match count(&content, file_count, field_count, &form) { - Ok(tup) => tup, - Err(e) => { - error = Some(e); - continue 'outer; - } - }; - - file_count = l; - field_count = r; - - multipart_form.push((name_parts, content)); + while let Some(_) = set.join_next().await { + tracing::trace!("Throwing away joined result"); } break 'outer; } - if set.is_empty() { - if let Some(res) = m.next().await { - match res { - Ok(field) => { - set.spawn_local(handle_stream_field(field, Rc::clone(&form))); - } - Err(e) => { - error = Some(e.into()); - continue 'outer; - } - } - } else { - stream_finished = true; - continue 'outer; - } - } - tokio::select! { - opt = m.next() => { + opt = m.next(), if !m.is_closed() => { tracing::trace!("Selected stream"); if let Some(res) = opt { match res { @@ -387,12 +341,9 @@ where continue 'outer; } } - } else { - stream_finished = true; - continue 'outer; } } - opt = set.join_next() => { + opt = set.join_next(), if !set.is_empty() => { tracing::trace!("Selected set"); if let Some(res) = opt { let (name_parts, content) = match res { @@ -425,6 +376,9 @@ where multipart_form.push((name_parts, content)); } } + else => { + break 'outer; + } } }