diff --git a/mbedtls/src/alloc.rs b/mbedtls/src/alloc.rs index 12883277e..bd28023e2 100644 --- a/mbedtls/src/alloc.rs +++ b/mbedtls/src/alloc.rs @@ -58,8 +58,10 @@ impl Drop for Box { } } +unsafe impl Send for Box {} +unsafe impl Sync for Box {} + #[repr(transparent)] pub struct List { pub(crate) inner: Option> } - diff --git a/mbedtls/src/lib.rs b/mbedtls/src/lib.rs index a98f6f40b..07b18e9d1 100644 --- a/mbedtls/src/lib.rs +++ b/mbedtls/src/lib.rs @@ -158,3 +158,69 @@ cfg_if::cfg_if! { pub unsafe fn set_global_debug_threshold(threshold: i32) { mbedtls_sys::debug_set_threshold(threshold); } + +#[cfg(test)] +mod tests { + #[allow(dead_code)] + /// Utilities for testing whether types implement certain traits. + /// + /// For each trait `Trait` that you want to be able to test, you should + /// implement: + /// ```ignore + /// impl Testable for T {} + /// ``` + /// + /// Then, to test whether a type `Type` implements `Trait`, call: + /// ```ignore + /// TestTrait::::new().impls_trait() + /// ``` + /// This returns a `bool` indicating whether the trait is implemented. + // This relies on auto-deref to distinguish between types that do and don't + // implement the trait. + mod testtrait { + use core::marker::PhantomData; + + pub struct NonImplTrait { + inner: PhantomData + } + + pub struct TestTrait { + non_impl: NonImplTrait, + phantom: PhantomData<*const TraitObj>, + } + + pub trait Testable {} + + impl TestTrait { + pub fn new() -> Self { + TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData } + } + } + + impl> TestTrait { + pub fn impls_trait(&self) -> bool { + true + } + } + + impl NonImplTrait { + pub fn impls_trait(&self) -> bool { + false + } + } + + impl core::ops::Deref for TestTrait { + type Target = NonImplTrait; + + fn deref(&self) -> &NonImplTrait { + &self.non_impl + } + } + } + + pub use testtrait::{TestTrait, Testable}; + + impl Testable for T {} + impl Testable for T {} + impl Testable for T {} +} diff --git a/mbedtls/src/wrapper_macros.rs b/mbedtls/src/wrapper_macros.rs index 6c2d20156..8a3d916ff 100644 --- a/mbedtls/src/wrapper_macros.rs +++ b/mbedtls/src/wrapper_macros.rs @@ -301,69 +301,11 @@ macro_rules! getter { #[cfg(test)] mod tests { - #[allow(dead_code)] - /// Utilities for testing whether types implement certain traits. - /// - /// For each trait `Trait` that you want to be able to test, you should - /// implement: - /// ```ignore - /// impl Testable for T {} - /// ``` - /// - /// Then, to test whether a type `Type` implements `Trait`, call: - /// ```ignore - /// TestTrait::::new().impls_trait() - /// ``` - /// This returns a `bool` indicating whether the trait is implemented. - // This relies on auto-deref to distinguish between types that do and don't - // implement the trait. - mod testtrait { - use core::marker::PhantomData; - - pub struct NonImplTrait { - inner: PhantomData - } - - pub struct TestTrait { - non_impl: NonImplTrait, - phantom: PhantomData<*const TraitObj>, - } - - pub trait Testable {} - - impl TestTrait { - pub fn new() -> Self { - TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData } - } - } - - impl> TestTrait { - pub fn impls_trait(&self) -> bool { - true - } - } - - impl NonImplTrait { - pub fn impls_trait(&self) -> bool { - false - } - } - - impl core::ops::Deref for TestTrait { - type Target = NonImplTrait; - - fn deref(&self) -> &NonImplTrait { - &self.non_impl - } - } - } - - use testtrait::{TestTrait, Testable}; + use crate::tests::{TestTrait, Testable}; callback!(RustTest: Fn() -> ()); callback!(NativeTestMut,NativeTest() -> ()); - impl Testable for T {} impl Testable for T {} impl Testable for T {} impl Testable for T {} diff --git a/mbedtls/src/x509/certificate.rs b/mbedtls/src/x509/certificate.rs index 56f2c3cb2..6a698e961 100644 --- a/mbedtls/src/x509/certificate.rs +++ b/mbedtls/src/x509/certificate.rs @@ -67,6 +67,8 @@ define!( impl<'a> UnsafeFrom {} ); +unsafe impl Sync for Certificate {} + fn x509_buf_to_vec(buf: &x509_buf) -> Vec { if buf.p.is_null() || buf.len == 0 { return vec![]; @@ -494,10 +496,6 @@ impl<'a> Builder<'a> { // x509write_crt_set_subject_key_identifier // -unsafe impl Send for MbedtlsBox {} - -unsafe impl Sync for MbedtlsBox {} - impl MbedtlsBox { fn init() -> Result { unsafe { @@ -556,10 +554,6 @@ impl<'a> UnsafeFrom<*mut *mut x509_crt> for &'a mut Option {} - -unsafe impl Sync for MbedtlsList {} - impl MbedtlsList { pub fn new() -> Self { Self { inner: None } @@ -1439,4 +1433,14 @@ cYp0bH/RcPTC0Z+ZaqSWMtfxRrk63MJQF9EXpDCdvQRcTMD9D85DJrMKn8aumq0M let cert : &mut Certificate = unsafe { UnsafeFrom::from(ptr).unwrap() }; assert_eq!(c1_info, format!("{:?}", cert)); } + + #[test] + fn cert_send_sync() { + assert!(crate::tests::TestTrait::::new().impls_trait(), "Certificate should be Send"); + assert!(crate::tests::TestTrait::>::new().impls_trait(), "MbedtlsBox should be Send"); + assert!(crate::tests::TestTrait::>::new().impls_trait(), "MbedtlsList should be Send"); + assert!(crate::tests::TestTrait::::new().impls_trait(), "Certificate should be Sync"); + assert!(crate::tests::TestTrait::>::new().impls_trait(), "MbedtlsBox should be Sync"); + assert!(crate::tests::TestTrait::>::new().impls_trait(), "MbedtlsList should be Sync"); + } }