Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add try_take! macro, handling AlreadyUsed Options #360

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::rslice::{rustls_slice_bytes, rustls_str};
use crate::server::rustls_server_config;
use crate::{
ffi_panic_boundary, free_box, rustls_result, set_boxed_mut_ptr, to_box, to_boxed_mut_ptr,
try_arc_from_ptr, try_callback, try_mut_from_ptr, try_ref_from_ptr, Castable, OwnershipBox,
try_arc_from_ptr, try_callback, try_mut_from_ptr, try_ref_from_ptr, try_take, Castable,
OwnershipBox,
};
use rustls_result::NullParameter;

Expand Down Expand Up @@ -398,10 +399,7 @@ impl rustls_accepted {
) -> rustls_result {
ffi_panic_boundary! {
let accepted: &mut Option<Accepted> = try_mut_from_ptr!(accepted);
let accepted = match accepted.take() {
Some(a) => a,
None => return rustls_result::AlreadyUsed,
};
let accepted = try_take!(accepted);
let config: Arc<ServerConfig> = try_arc_from_ptr!(config);
match accepted.into_connection(config) {
Ok(built) => {
Expand Down
20 changes: 4 additions & 16 deletions src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use crate::error::{map_error, rustls_result};
use crate::rslice::{rustls_slice_bytes, rustls_str};
use crate::{
ffi_panic_boundary, free_arc, to_arc_const_ptr, to_boxed_mut_ptr, try_box_from_ptr,
try_mut_from_ptr, try_ref_from_ptr, try_slice, Castable, OwnershipArc, OwnershipBox,
try_mut_from_ptr, try_ref_from_ptr, try_slice, try_take, Castable, OwnershipArc, OwnershipBox,
OwnershipRef,
};
use rustls_result::{AlreadyUsed, NullParameter};
use rustls_result::NullParameter;

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

let client_cert_verifier = match client_cert_verifier_builder.take() {
None => {
return AlreadyUsed;
},
Some(x) => x,
};

let client_cert_verifier = try_take!(client_cert_verifier_builder);
match client_cert_verifier.with_crls(crls_der) {
Ok(v) => client_cert_verifier_builder.replace(v),
Err(e) => return map_error(rustls::Error::InvalidCertRevocationList(e)),
Expand Down Expand Up @@ -710,13 +704,7 @@ impl rustls_allow_any_anonymous_or_authenticated_client_builder {
Err(_) => return rustls_result::CertificateRevocationListParseError,
};

let client_cert_verifier = match client_cert_verifier_builder.take() {
None => {
return AlreadyUsed;
},
Some(x) => x,
};

let client_cert_verifier = try_take!(client_cert_verifier_builder);
match client_cert_verifier.with_crls(crls_der) {
Ok(v) => client_cert_verifier_builder.replace(v),
Err(e) => return map_error(rustls::Error::InvalidCertRevocationList(e)),
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,19 @@ macro_rules! try_callback {

pub(crate) use try_callback;

macro_rules! try_take {
( $var:ident ) => {
match $var.take() {
None => {
return $crate::rustls_result::AlreadyUsed;
}
Some(x) => x,
}
};
}

pub(crate) use try_take;

/// Returns a static string containing the rustls-ffi version as well as the
/// rustls version. The string is alive for the lifetime of the program and does
/// not need to be freed.
Expand Down