Skip to content

Commit 0b35e62

Browse files
committed
lib: add try_take! macro, handling AlreadyUsed Options
In several places we represent something that could be consumed as an `Option<T>`. When we try to use it, we `take()` the option, match the result, and return `rustls_result::AlreadyUsed` if `take()` returned `None`. Since this pattern is becoming more common with the use of more builder patterns this commit adds a `try_take!` macro that can do this repetitive work for us.
1 parent c179c22 commit 0b35e62

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/acceptor.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::rslice::{rustls_slice_bytes, rustls_str};
1212
use crate::server::rustls_server_config;
1313
use crate::{
1414
ffi_panic_boundary, free_box, rustls_result, set_boxed_mut_ptr, to_box, to_boxed_mut_ptr,
15-
try_arc_from_ptr, try_callback, try_mut_from_ptr, try_ref_from_ptr, Castable, OwnershipBox,
15+
try_arc_from_ptr, try_callback, try_mut_from_ptr, try_ref_from_ptr, try_take, Castable,
16+
OwnershipBox,
1617
};
1718
use rustls_result::NullParameter;
1819

@@ -398,10 +399,7 @@ impl rustls_accepted {
398399
) -> rustls_result {
399400
ffi_panic_boundary! {
400401
let accepted: &mut Option<Accepted> = try_mut_from_ptr!(accepted);
401-
let accepted = match accepted.take() {
402-
Some(a) => a,
403-
None => return rustls_result::AlreadyUsed,
404-
};
402+
let accepted = try_take!(accepted);
405403
let config: Arc<ServerConfig> = try_arc_from_ptr!(config);
406404
match accepted.into_connection(config) {
407405
Ok(built) => {

src/cipher.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use crate::error::{map_error, rustls_result};
1919
use crate::rslice::{rustls_slice_bytes, rustls_str};
2020
use crate::{
2121
ffi_panic_boundary, free_arc, to_arc_const_ptr, to_boxed_mut_ptr, try_box_from_ptr,
22-
try_mut_from_ptr, try_ref_from_ptr, try_slice, Castable, OwnershipArc, OwnershipBox,
22+
try_mut_from_ptr, try_ref_from_ptr, try_slice, try_take, Castable, OwnershipArc, OwnershipBox,
2323
OwnershipRef,
2424
};
25-
use rustls_result::{AlreadyUsed, NullParameter};
25+
use rustls_result::NullParameter;
2626

2727
/// An X.509 certificate, as used in rustls.
2828
/// Corresponds to `Certificate` in the Rust API.
@@ -566,13 +566,7 @@ impl rustls_allow_any_authenticated_client_builder {
566566
Err(_) => return rustls_result::CertificateRevocationListParseError,
567567
};
568568

569-
let client_cert_verifier = match client_cert_verifier_builder.take() {
570-
None => {
571-
return AlreadyUsed;
572-
},
573-
Some(x) => x,
574-
};
575-
569+
let client_cert_verifier = try_take!(client_cert_verifier_builder);
576570
match client_cert_verifier.with_crls(crls_der) {
577571
Ok(v) => client_cert_verifier_builder.replace(v),
578572
Err(e) => return map_error(rustls::Error::InvalidCertRevocationList(e)),
@@ -710,13 +704,7 @@ impl rustls_allow_any_anonymous_or_authenticated_client_builder {
710704
Err(_) => return rustls_result::CertificateRevocationListParseError,
711705
};
712706

713-
let client_cert_verifier = match client_cert_verifier_builder.take() {
714-
None => {
715-
return AlreadyUsed;
716-
},
717-
Some(x) => x,
718-
};
719-
707+
let client_cert_verifier = try_take!(client_cert_verifier_builder);
720708
match client_cert_verifier.with_crls(crls_der) {
721709
Ok(v) => client_cert_verifier_builder.replace(v),
722710
Err(e) => return map_error(rustls::Error::InvalidCertRevocationList(e)),

src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,19 @@ macro_rules! try_callback {
643643

644644
pub(crate) use try_callback;
645645

646+
macro_rules! try_take {
647+
( $var:ident ) => {
648+
match $var.take() {
649+
None => {
650+
return $crate::rustls_result::AlreadyUsed;
651+
}
652+
Some(x) => x,
653+
}
654+
};
655+
}
656+
657+
pub(crate) use try_take;
658+
646659
/// Returns a static string containing the rustls-ffi version as well as the
647660
/// rustls version. The string is alive for the lifetime of the program and does
648661
/// not need to be freed.

0 commit comments

Comments
 (0)