Skip to content

Commit

Permalink
Remove Sync trait bounds on callback futures
Browse files Browse the repository at this point in the history
They are unnecessary as we ever only retrieve the futures from ex data
to poll them, thus when we have mutable access to them, so Send is all we need.
  • Loading branch information
nox authored and ghedo committed Nov 3, 2023
1 parent 7a7de40 commit 7c5fdfa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tokio-boring/src/async_callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::mut_only::MutOnly;
use boring::ex_data::Index;
use boring::ssl::{self, ClientHello, PrivateKeyMethod, Ssl, SslContextBuilder};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -29,17 +30,18 @@ pub type BoxGetSessionFinish = Box<dyn FnOnce(&mut ssl::SslRef, &[u8]) -> Option
/// Convenience alias for futures stored in [`Ssl`] ex data by [`SslContextBuilderExt`] methods.
///
/// Public for documentation purposes.
pub type ExDataFuture<T> = Pin<Box<dyn Future<Output = T> + Send + Sync>>;
pub type ExDataFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

pub(crate) static TASK_WAKER_INDEX: Lazy<Index<Ssl, Option<Waker>>> =
Lazy::new(|| Ssl::new_ex_index().unwrap());
pub(crate) static SELECT_CERT_FUTURE_INDEX: Lazy<Index<Ssl, Option<BoxSelectCertFuture>>> =
pub(crate) static SELECT_CERT_FUTURE_INDEX: Lazy<Index<Ssl, MutOnly<Option<BoxSelectCertFuture>>>> =
Lazy::new(|| Ssl::new_ex_index().unwrap());
pub(crate) static SELECT_PRIVATE_KEY_METHOD_FUTURE_INDEX: Lazy<
Index<Ssl, Option<BoxPrivateKeyMethodFuture>>,
Index<Ssl, MutOnly<Option<BoxPrivateKeyMethodFuture>>>,
> = Lazy::new(|| Ssl::new_ex_index().unwrap());
pub(crate) static SELECT_GET_SESSION_FUTURE_INDEX: Lazy<
Index<Ssl, MutOnly<Option<BoxGetSessionFuture>>>,
> = Lazy::new(|| Ssl::new_ex_index().unwrap());
pub(crate) static SELECT_GET_SESSION_FUTURE_INDEX: Lazy<Index<Ssl, Option<BoxGetSessionFuture>>> =
Lazy::new(|| Ssl::new_ex_index().unwrap());

/// Extensions to [`SslContextBuilder`].
///
Expand Down Expand Up @@ -270,7 +272,7 @@ fn with_private_key_method(
/// created by `create_fut` returns `Poll::Ready(_)` on the first poll call.
fn with_ex_data_future<H, R, T, E>(
ssl_handle: &mut H,
index: Index<ssl::Ssl, Option<ExDataFuture<R>>>,
index: Index<ssl::Ssl, MutOnly<Option<ExDataFuture<R>>>>,
get_ssl_mut: impl Fn(&mut H) -> &mut ssl::SslRef,
create_fut: impl FnOnce(&mut H) -> Result<ExDataFuture<R>, E>,
into_result: impl Fn(R) -> Result<T, E>,
Expand All @@ -284,7 +286,7 @@ fn with_ex_data_future<H, R, T, E>(

let mut ctx = Context::from_waker(&waker);

if let Some(data @ Some(_)) = ssl.ex_data_mut(index) {
if let Some(data @ Some(_)) = ssl.ex_data_mut(index).map(MutOnly::get_mut) {
let fut_result = into_result(ready!(data.as_mut().unwrap().as_mut().poll(&mut ctx)));

*data = None;
Expand All @@ -296,7 +298,7 @@ fn with_ex_data_future<H, R, T, E>(
match fut.as_mut().poll(&mut ctx) {
Poll::Ready(fut_result) => Poll::Ready(into_result(fut_result)),
Poll::Pending => {
get_ssl_mut(ssl_handle).set_ex_data(index, Some(fut));
get_ssl_mut(ssl_handle).set_ex_data(index, MutOnly::new(Some(fut)));

Poll::Pending
}
Expand Down
1 change: 1 addition & 0 deletions tokio-boring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

mod async_callbacks;
mod bridge;
mod mut_only;

use self::async_callbacks::TASK_WAKER_INDEX;
pub use self::async_callbacks::{
Expand Down
14 changes: 14 additions & 0 deletions tokio-boring/src/mut_only.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub(crate) struct MutOnly<T>(T);

impl<T> MutOnly<T> {
pub(crate) fn new(value: T) -> Self {
Self(value)
}

pub(crate) fn get_mut(&mut self) -> &mut T {
&mut self.0
}
}

/// SAFETY: The type does not let anyone get a &T so Sync is irrelevant.
unsafe impl<T> Sync for MutOnly<T> {}

0 comments on commit 7c5fdfa

Please sign in to comment.