Skip to content

Commit

Permalink
Merge #262
Browse files Browse the repository at this point in the history
262: Make Context Sync r=YxC a=jethrogb

cc `@zvolin`

Co-authored-by: Jethro Beekman <[email protected]>
Co-authored-by: YxC <[email protected]>
  • Loading branch information
3 people authored May 11, 2023
2 parents d310d3d + 5bd9186 commit 3d90448
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions mbedtls/src/ssl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ pub struct Context<T> {
client_transport_id: Option<Vec<u8>>,
}

// # Safety
//
// It is safe for `Context` to be `Sync` as all its methods taking `&self`
// can only trigger `MbedTLS` functions that have no side effects.
// When introducing any new FFI call inside `&self` methods, `MbedTLS` sources
// should be reviewed whether they don't result in any side-effect.
unsafe impl<T: Sync> Sync for Context<T> {}

impl<'a, T> Into<*const ssl_context> for &'a Context<T> {
fn into(self) -> *const ssl_context {
self.handle()
Expand Down Expand Up @@ -603,19 +611,19 @@ mod tests {
assert!(!TestTrait::<dyn Sync, HandshakeContext>::new().impls_trait(), "HandshakeContext must be !Sync");
}

struct NonSendStream {
struct NonSendSyncStream {
_buffer: core::ptr::NonNull<u8>,
}

#[cfg(feature = "std")]
impl Read for NonSendStream {
impl Read for NonSendSyncStream {
fn read(&mut self, _: &mut [u8]) -> IoResult<usize> {
unimplemented!()
}
}

#[cfg(feature = "std")]
impl Write for NonSendStream {
impl Write for NonSendSyncStream {
fn write(&mut self, _: &[u8]) -> IoResult<usize> {
unimplemented!()
}
Expand All @@ -625,19 +633,19 @@ mod tests {
}
}

struct SendStream {
struct SendSyncStream {
_buffer: u8,
}

#[cfg(feature = "std")]
impl Read for SendStream {
impl Read for SendSyncStream {
fn read(&mut self, _: &mut [u8]) -> IoResult<usize> {
unimplemented!()
}
}

#[cfg(feature = "std")]
impl Write for SendStream {
impl Write for SendSyncStream {
fn write(&mut self, _: &[u8]) -> IoResult<usize> {
unimplemented!()
}
Expand All @@ -648,12 +656,16 @@ mod tests {
}

#[test]
fn context_send() {
assert!(!TestTrait::<dyn Send, NonSendStream>::new().impls_trait(), "NonSendStream can't be send");
assert!(!TestTrait::<dyn Send, Context<NonSendStream>>::new().impls_trait(), "Context<NonSendStream> can't be send");

assert!(TestTrait::<dyn Send, SendStream>::new().impls_trait(), "SendStream is send");
assert!(TestTrait::<dyn Send, Context<SendStream>>::new().impls_trait(), "Context<SendStream> is send");
fn context_send_sync() {
assert!(!TestTrait::<dyn Send, NonSendSyncStream>::new().impls_trait(), "NonSendSyncStream can't be send");
assert!(!TestTrait::<dyn Send, Context<NonSendSyncStream>>::new().impls_trait(), "Context<NonSendSyncStream> can't be send");
assert!(!TestTrait::<dyn Sync, NonSendSyncStream>::new().impls_trait(), "NonSendSyncStream can't be sync");
assert!(!TestTrait::<dyn Sync, Context<NonSendSyncStream>>::new().impls_trait(), "Context<NonSendSyncStream> can't be sync");

assert!(TestTrait::<dyn Send, SendSyncStream>::new().impls_trait(), "SendSyncStream is send");
assert!(TestTrait::<dyn Send, Context<SendSyncStream>>::new().impls_trait(), "Context<SendSyncStream> is send");
assert!(TestTrait::<dyn Sync, SendSyncStream>::new().impls_trait(), "SendSyncStream is sync");
assert!(TestTrait::<dyn Sync, Context<SendSyncStream>>::new().impls_trait(), "Context<SendSyncStream> is sync");
}

}
Expand Down

0 comments on commit 3d90448

Please sign in to comment.