From 6086d523a5b2312cc5f9d9584b3b705eb9873d49 Mon Sep 17 00:00:00 2001 From: Moritz Borcherding Date: Mon, 4 Mar 2024 11:26:42 +0100 Subject: [PATCH] some let ... else syntax where it makes code cleaner --- rustbus/fuzz/fuzz_targets/fuzz_unmarshal.rs | 17 +++++++---------- rustbus/src/bin/fuzz_artifact.rs | 18 ++++++++---------- rustbus/src/wire/unmarshal/traits/container.rs | 5 ++--- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/rustbus/fuzz/fuzz_targets/fuzz_unmarshal.rs b/rustbus/fuzz/fuzz_targets/fuzz_unmarshal.rs index 183b566..a2cf705 100644 --- a/rustbus/fuzz/fuzz_targets/fuzz_unmarshal.rs +++ b/rustbus/fuzz/fuzz_targets/fuzz_unmarshal.rs @@ -5,23 +5,20 @@ extern crate rustbus; fuzz_target!(|data: &[u8]| { let mut cursor = rustbus::wire::unmarshal_context::Cursor::new(data); - let header = match rustbus::wire::unmarshal::unmarshal_header(&mut cursor) { - Ok(head) => head, - Err(_) => return, + let Ok(header) = rustbus::wire::unmarshal::unmarshal_header(&mut cursor) else { + return; }; - let dynheader = match rustbus::wire::unmarshal::unmarshal_dynamic_header(&header, &mut cursor) { - Ok(head) => head, - Err(_) => return, + let Ok(dynheader) = rustbus::wire::unmarshal::unmarshal_dynamic_header(&header, &mut cursor) else { + return; }; - let msg = match rustbus::wire::unmarshal::unmarshal_next_message( + let Ok(msg) = rustbus::wire::unmarshal::unmarshal_next_message( &header, dynheader, data.to_vec(), cursor.consumed(), - ) { - Ok(msg) => msg, - Err(_) => return, + ) else { + return; }; try_unmarhal_traits(&msg); msg.unmarshall_all().ok(); diff --git a/rustbus/src/bin/fuzz_artifact.rs b/rustbus/src/bin/fuzz_artifact.rs index 34e9a85..d9d9e85 100644 --- a/rustbus/src/bin/fuzz_artifact.rs +++ b/rustbus/src/bin/fuzz_artifact.rs @@ -19,28 +19,26 @@ fn run_artifact(path: &str) { let data = &data; let mut cursor = Cursor::new(data); - let header = match rustbus::wire::unmarshal::unmarshal_header(&mut cursor) { - Ok(head) => head, - Err(_) => return, + let Ok(header) = rustbus::wire::unmarshal::unmarshal_header(&mut cursor) else { + return; }; println!("Header: {:?}", header); - let dynheader = match rustbus::wire::unmarshal::unmarshal_dynamic_header(&header, &mut cursor) { - Ok(head) => head, - Err(_) => return, + let Ok(dynheader) = rustbus::wire::unmarshal::unmarshal_dynamic_header(&header, &mut cursor) + else { + return; }; println!("Dynheader: {:?}", dynheader); - let msg = match rustbus::wire::unmarshal::unmarshal_next_message( + let Ok(msg) = rustbus::wire::unmarshal::unmarshal_next_message( &header, dynheader, data.clone(), cursor.consumed(), - ) { - Ok(msg) => msg, - Err(_) => return, + ) else { + return; }; println!("Message: {:?}", msg); diff --git a/rustbus/src/wire/unmarshal/traits/container.rs b/rustbus/src/wire/unmarshal/traits/container.rs index 0ef462c..cd2b92c 100644 --- a/rustbus/src/wire/unmarshal/traits/container.rs +++ b/rustbus/src/wire/unmarshal/traits/container.rs @@ -269,9 +269,8 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for Variant<'fds, 'buf> { fn unmarshal(ctx: &mut UnmarshalContext<'fds, 'buf>) -> unmarshal::UnmarshalResult { let desc = ctx.read_signature()?; - let mut sigs = match signature::Type::parse_description(desc) { - Ok(sigs) => sigs, - Err(_) => return Err(UnmarshalError::WrongSignature), + let Ok(mut sigs) = signature::Type::parse_description(desc) else { + return Err(UnmarshalError::WrongSignature); }; if sigs.len() != 1 { return Err(UnmarshalError::WrongSignature);