From 7e9c37f9cb33d937a664dec83ee28da447a41aa6 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 24 Jun 2018 08:58:26 -0400 Subject: Change `allocate_and_copy_string` to take a `&str` instead of `&String`. --- src/rust/protover/ffi.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/rust/protover') diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs index cd49e5f931..0470cd487a 100644 --- a/src/rust/protover/ffi.rs +++ b/src/rust/protover/ffi.rs @@ -198,8 +198,7 @@ pub extern "C" fn protover_compute_vote( ) -> *mut c_char { if list.is_null() { - let empty = String::new(); - return allocate_and_copy_string(&empty); + return allocate_and_copy_string(""); } // Dereference of raw pointer requires an unsafe block. The pointer is -- cgit v1.2.3-54-g00ecf From 94880b2db73d514e0352d70be8eda2b36132aa4d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 24 Jun 2018 10:16:11 -0400 Subject: Utilize `if let` construct instead of explicit unwrapping. --- src/rust/protover/ffi.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/rust/protover') diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs index 0470cd487a..f668dba80b 100644 --- a/src/rust/protover/ffi.rs +++ b/src/rust/protover/ffi.rs @@ -61,10 +61,8 @@ pub extern "C" fn protover_all_supported( Ok(n) => n, Err(_) => return 1, }; - let maybe_unsupported: Option = relay_proto_entry.all_supported(); - if maybe_unsupported.is_some() { - let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap(); + if let Some(unsupported) = relay_proto_entry.all_supported() { let c_unsupported: CString = match CString::new(unsupported.to_string()) { Ok(n) => n, Err(_) => return 1, -- cgit v1.2.3-54-g00ecf From 59d4505749073f45db9a32d606b760eb9e07df69 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 24 Jun 2018 10:27:46 -0400 Subject: Utilize `if..else` for switching on boolean values. --- src/rust/protover/ffi.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/rust/protover') diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs index f668dba80b..ba156cb1ba 100644 --- a/src/rust/protover/ffi.rs +++ b/src/rust/protover/ffi.rs @@ -105,9 +105,10 @@ pub extern "C" fn protocol_list_supports_protocol( Ok(n) => n.into(), Err(_) => return 0, }; - match proto_entry.supports_protocol(&protocol, &version) { - false => return 0, - true => return 1, + if proto_entry.supports_protocol(&protocol, &version) { + 1 + } else { + 0 } } @@ -206,13 +207,16 @@ pub extern "C" fn protover_compute_vote( let mut proto_entries: Vec = Vec::new(); for datum in data { - let entry: UnvalidatedProtoEntry = match allow_long_proto_names { - true => match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) { + let entry: UnvalidatedProtoEntry = if allow_long_proto_names { + match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) { Ok(n) => n, - Err(_) => continue}, - false => match datum.parse() { + Err(_) => continue + } + } else { + match datum.parse() { Ok(n) => n, - Err(_) => continue}, + Err(_) => continue + } }; proto_entries.push(entry); } -- cgit v1.2.3-54-g00ecf From 6d2e4dea109ec5a499d11a4fee4f8ca3465ad30c Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 24 Jun 2018 22:45:07 -0400 Subject: Utilize type param in method invocation. --- src/rust/protover/ffi.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/rust/protover') diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs index ba156cb1ba..40a7354cbf 100644 --- a/src/rust/protover/ffi.rs +++ b/src/rust/protover/ffi.rs @@ -129,10 +129,7 @@ pub extern "C" fn protover_contains_long_protocol_names_( Err(_) => return 1 }; - let protocol_entry : Result = - protocol_list.parse(); - - match protocol_entry { + match protocol_list.parse::() { Ok(_) => 0, Err(_) => 1, } -- cgit v1.2.3-54-g00ecf From e62582e9fe67d742d5d422758fe588d22e8599f7 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 24 Jun 2018 22:56:58 -0400 Subject: Run rustfmt on 'src/rust/protover/ffi.rs'. --- src/rust/protover/ffi.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/rust/protover') diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs index 40a7354cbf..034f16e221 100644 --- a/src/rust/protover/ffi.rs +++ b/src/rust/protover/ffi.rs @@ -42,7 +42,6 @@ pub extern "C" fn protover_all_supported( c_relay_version: *const c_char, missing_out: *mut *mut c_char, ) -> c_int { - if c_relay_version.is_null() { return 1; } @@ -58,12 +57,13 @@ pub extern "C" fn protover_all_supported( let relay_proto_entry: UnvalidatedProtoEntry = match UnvalidatedProtoEntry::from_str_any_len(relay_version) { - Ok(n) => n, - Err(_) => return 1, - }; + Ok(n) => n, + Err(_) => return 1, + }; if let Some(unsupported) = relay_proto_entry.all_supported() { - let c_unsupported: CString = match CString::new(unsupported.to_string()) { + let c_unsupported: CString = match CString::new(unsupported.to_string()) + { Ok(n) => n, Err(_) => return 1, }; @@ -98,7 +98,7 @@ pub extern "C" fn protocol_list_supports_protocol( Err(_) => return 1, }; let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() { - Ok(n) => n, + Ok(n) => n, Err(_) => return 0, }; let protocol: UnknownProtocol = match translate_to_rust(c_protocol) { @@ -114,7 +114,7 @@ pub extern "C" fn protocol_list_supports_protocol( #[no_mangle] pub extern "C" fn protover_contains_long_protocol_names_( - c_protocol_list: *const c_char + c_protocol_list: *const c_char, ) -> c_int { if c_protocol_list.is_null() { return 1; @@ -126,7 +126,7 @@ pub extern "C" fn protover_contains_long_protocol_names_( let protocol_list = match c_str.to_str() { Ok(n) => n, - Err(_) => return 1 + Err(_) => return 1, }; match protocol_list.parse::() { @@ -162,7 +162,7 @@ pub extern "C" fn protocol_list_supports_protocol_or_later( }; let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() { - Ok(n) => n, + Ok(n) => n, Err(_) => return 1, }; @@ -192,7 +192,6 @@ pub extern "C" fn protover_compute_vote( threshold: c_int, allow_long_proto_names: bool, ) -> *mut c_char { - if list.is_null() { return allocate_and_copy_string(""); } @@ -206,18 +205,19 @@ pub extern "C" fn protover_compute_vote( for datum in data { let entry: UnvalidatedProtoEntry = if allow_long_proto_names { match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) { - Ok(n) => n, - Err(_) => continue + Ok(n) => n, + Err(_) => continue, } } else { match datum.parse() { - Ok(n) => n, - Err(_) => continue + Ok(n) => n, + Err(_) => continue, } }; proto_entries.push(entry); } - let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold); + let vote: UnvalidatedProtoEntry = + ProtoverVote::compute(&proto_entries, &hold); allocate_and_copy_string(&vote.to_string()) } @@ -242,7 +242,9 @@ pub extern "C" fn protover_is_supported_here( /// Provide an interface for C to translate arguments and return types for /// protover::compute_for_old_tor #[no_mangle] -pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char { +pub extern "C" fn protover_compute_for_old_tor( + version: *const c_char, +) -> *const c_char { let supported: &'static CStr; let empty: &'static CStr; -- cgit v1.2.3-54-g00ecf