Skip to content

Commit

Permalink
bindings(rust): make callbacks Send + Sync (#4289)
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft authored Nov 15, 2023
1 parent b82a5d4 commit 690298b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
6 changes: 3 additions & 3 deletions bindings/rust/s2n-tls/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ where
///
/// The implementation should verify the certificate host name and return `true`
/// if the name is valid, `false` otherwise.
pub trait VerifyHostNameCallback {
pub trait VerifyHostNameCallback: 'static + Send + Sync {
fn verify_host_name(&self, host_name: &str) -> bool;
}

/// A trait for the callback used to retrieve the system / wall clock time.
pub trait WallClock {
pub trait WallClock: 'static + Send + Sync {
fn get_time_since_epoch(&self) -> Duration;
}

/// A trait for the callback used to retrieve the monotonic time.
pub trait MonotonicClock {
pub trait MonotonicClock: 'static + Send + Sync {
fn get_time(&self) -> Duration;
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/rust/s2n-tls/src/callbacks/async_cb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::pin::Pin;
/// if it wants to run an asynchronous operation (disk read, network call).
/// The application can return an error ([Err(Error::application())])
/// to indicate connection failure.
pub trait ConnectionFuture {
pub trait ConnectionFuture: 'static + Send {
fn poll(
self: Pin<&mut Self>,
connection: &mut Connection,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl ConnectionFuture for ErrorFuture {
pin_project! {
/// A wrapper around an optional [`ConnectionFuture`]
/// which either polls the future or immediately reports success.
struct OptionalFuture{
struct OptionalFuture {
option: Option<Pin<Box<dyn ConnectionFuture>>>,
}
}
Expand Down
8 changes: 5 additions & 3 deletions bindings/rust/s2n-tls/src/callbacks/client_hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{future::Future, pin::Pin};
///
/// Use in conjunction with
/// [config::Builder::set_client_hello_callback](`crate::config::Builder::set_client_hello_callback()`).
pub trait ClientHelloCallback {
pub trait ClientHelloCallback: 'static + Send + Sync {
/// The application can return an `Ok(None)` to resolve the callback
/// synchronously or return an `Ok(Some(ConnectionFuture))` if it wants to
/// run some asynchronous task before resolving the callback.
Expand Down Expand Up @@ -42,13 +42,15 @@ pin_project! {
}
}

impl<F: Future<Output = Result<Config, Error>>> ConfigResolver<F> {
impl<F: 'static + Send + Future<Output = Result<Config, Error>>> ConfigResolver<F> {
pub fn new(fut: F) -> Self {
ConfigResolver { fut }
}
}

impl<F: Future<Output = Result<Config, Error>>> ConnectionFuture for ConfigResolver<F> {
impl<F: 'static + Send + Future<Output = Result<Config, Error>>> ConnectionFuture
for ConfigResolver<F>
{
fn poll(
self: Pin<&mut Self>,
connection: &mut Connection,
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/s2n-tls/src/callbacks/pkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Drop for PrivateKeyOperation {
}
}

pub trait PrivateKeyCallback {
pub trait PrivateKeyCallback: 'static + Send + Sync {
fn handle_operation(
&self,
connection: &mut Connection,
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/s2n-tls/src/callbacks/session_ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};

/// A trait to retrieve session tickets from the connection
pub trait SessionTicketCallback: Send + Sync + 'static {
pub trait SessionTicketCallback: 'static + Send + Sync {
fn on_session_ticket(&self, connection: &mut Connection, session_ticket: &SessionTicket);
}

Expand Down
12 changes: 12 additions & 0 deletions bindings/rust/s2n-tls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,15 @@ impl Default for Context {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

// ensure the config context is send and sync
#[test]
fn context_send_sync_test() {
fn assert_send_sync<T: 'static + Send + Sync>() {}
assert_send_sync::<Context>();
}
}
18 changes: 12 additions & 6 deletions bindings/rust/s2n-tls/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ impl fmt::Debug for Connection {
/// s2n_connection objects can be sent across threads
unsafe impl Send for Connection {}

/// # Safety
///
/// All C methods that mutate the s2n_connection are wrapped
/// in Rust methods that require a mutable reference.
unsafe impl Sync for Connection {}

impl Connection {
pub fn new(mode: Mode) -> Self {
crate::init::init();
Expand Down Expand Up @@ -942,3 +936,15 @@ impl Drop for Connection {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

// ensure the connection context is send
#[test]
fn context_send_test() {
fn assert_send<T: 'static + Send>() {}
assert_send::<Context>();
}
}

0 comments on commit 690298b

Please sign in to comment.