Skip to content

Commit f42d25c

Browse files
bors[bot]Jethro Beekman
and
Jethro Beekman
authored
Merge #180
180: Rearrange Send/Sync impls for MbedtlsList/MbedtlsBox r=AdrianCX a=jethrogb See rust-lang/rust#93367 Co-authored-by: Jethro Beekman <[email protected]>
2 parents 4db611b + cd56cf7 commit f42d25c

File tree

4 files changed

+82
-68
lines changed

4 files changed

+82
-68
lines changed

mbedtls/src/alloc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ impl<T> Drop for Box<T> {
5858
}
5959
}
6060

61+
unsafe impl<T: Send> Send for Box<T> {}
62+
unsafe impl<T: Sync> Sync for Box<T> {}
63+
6164
#[repr(transparent)]
6265
pub struct List<T> {
6366
pub(crate) inner: Option<Box<T>>
6467
}
65-

mbedtls/src/lib.rs

+66
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,69 @@ cfg_if::cfg_if! {
158158
pub unsafe fn set_global_debug_threshold(threshold: i32) {
159159
mbedtls_sys::debug_set_threshold(threshold);
160160
}
161+
162+
#[cfg(test)]
163+
mod tests {
164+
#[allow(dead_code)]
165+
/// Utilities for testing whether types implement certain traits.
166+
///
167+
/// For each trait `Trait` that you want to be able to test, you should
168+
/// implement:
169+
/// ```ignore
170+
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
171+
/// ```
172+
///
173+
/// Then, to test whether a type `Type` implements `Trait`, call:
174+
/// ```ignore
175+
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
176+
/// ```
177+
/// This returns a `bool` indicating whether the trait is implemented.
178+
// This relies on auto-deref to distinguish between types that do and don't
179+
// implement the trait.
180+
mod testtrait {
181+
use core::marker::PhantomData;
182+
183+
pub struct NonImplTrait<T> {
184+
inner: PhantomData<T>
185+
}
186+
187+
pub struct TestTrait<TraitObj: ?Sized, Type> {
188+
non_impl: NonImplTrait<Type>,
189+
phantom: PhantomData<*const TraitObj>,
190+
}
191+
192+
pub trait Testable<T: ?Sized> {}
193+
194+
impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
195+
pub fn new() -> Self {
196+
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
197+
}
198+
}
199+
200+
impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
201+
pub fn impls_trait(&self) -> bool {
202+
true
203+
}
204+
}
205+
206+
impl<T> NonImplTrait<T> {
207+
pub fn impls_trait(&self) -> bool {
208+
false
209+
}
210+
}
211+
212+
impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
213+
type Target = NonImplTrait<Type>;
214+
215+
fn deref(&self) -> &NonImplTrait<Type> {
216+
&self.non_impl
217+
}
218+
}
219+
}
220+
221+
pub use testtrait::{TestTrait, Testable};
222+
223+
impl<T: Send> Testable<dyn Send> for T {}
224+
impl<T: Sync> Testable<dyn Sync> for T {}
225+
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
226+
}

mbedtls/src/wrapper_macros.rs

+1-59
Original file line numberDiff line numberDiff line change
@@ -301,69 +301,11 @@ macro_rules! getter {
301301

302302
#[cfg(test)]
303303
mod tests {
304-
#[allow(dead_code)]
305-
/// Utilities for testing whether types implement certain traits.
306-
///
307-
/// For each trait `Trait` that you want to be able to test, you should
308-
/// implement:
309-
/// ```ignore
310-
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
311-
/// ```
312-
///
313-
/// Then, to test whether a type `Type` implements `Trait`, call:
314-
/// ```ignore
315-
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
316-
/// ```
317-
/// This returns a `bool` indicating whether the trait is implemented.
318-
// This relies on auto-deref to distinguish between types that do and don't
319-
// implement the trait.
320-
mod testtrait {
321-
use core::marker::PhantomData;
322-
323-
pub struct NonImplTrait<T> {
324-
inner: PhantomData<T>
325-
}
326-
327-
pub struct TestTrait<TraitObj: ?Sized, Type> {
328-
non_impl: NonImplTrait<Type>,
329-
phantom: PhantomData<*const TraitObj>,
330-
}
331-
332-
pub trait Testable<T: ?Sized> {}
333-
334-
impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
335-
pub fn new() -> Self {
336-
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
337-
}
338-
}
339-
340-
impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
341-
pub fn impls_trait(&self) -> bool {
342-
true
343-
}
344-
}
345-
346-
impl<T> NonImplTrait<T> {
347-
pub fn impls_trait(&self) -> bool {
348-
false
349-
}
350-
}
351-
352-
impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
353-
type Target = NonImplTrait<Type>;
354-
355-
fn deref(&self) -> &NonImplTrait<Type> {
356-
&self.non_impl
357-
}
358-
}
359-
}
360-
361-
use testtrait::{TestTrait, Testable};
304+
use crate::tests::{TestTrait, Testable};
362305

363306
callback!(RustTest: Fn() -> ());
364307
callback!(NativeTestMut,NativeTest() -> ());
365308

366-
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
367309
impl<T: RustTest> Testable<dyn RustTest> for T {}
368310
impl<T: NativeTest> Testable<dyn NativeTest> for T {}
369311
impl<T: NativeTestMut> Testable<dyn NativeTestMut> for T {}

mbedtls/src/x509/certificate.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ define!(
6767
impl<'a> UnsafeFrom<ptr> {}
6868
);
6969

70+
unsafe impl Sync for Certificate {}
71+
7072
fn x509_buf_to_vec(buf: &x509_buf) -> Vec<u8> {
7173
if buf.p.is_null() || buf.len == 0 {
7274
return vec![];
@@ -494,10 +496,6 @@ impl<'a> Builder<'a> {
494496
// x509write_crt_set_subject_key_identifier
495497
//
496498

497-
unsafe impl Send for MbedtlsBox<Certificate> {}
498-
499-
unsafe impl Sync for MbedtlsBox<Certificate> {}
500-
501499
impl MbedtlsBox<Certificate> {
502500
fn init() -> Result<Self> {
503501
unsafe {
@@ -556,10 +554,6 @@ impl<'a> UnsafeFrom<*mut *mut x509_crt> for &'a mut Option<MbedtlsBox<Certificat
556554
}
557555
}
558556

559-
unsafe impl Send for MbedtlsList<Certificate> {}
560-
561-
unsafe impl Sync for MbedtlsList<Certificate> {}
562-
563557
impl MbedtlsList<Certificate> {
564558
pub fn new() -> Self {
565559
Self { inner: None }
@@ -1439,4 +1433,14 @@ cYp0bH/RcPTC0Z+ZaqSWMtfxRrk63MJQF9EXpDCdvQRcTMD9D85DJrMKn8aumq0M
14391433
let cert : &mut Certificate = unsafe { UnsafeFrom::from(ptr).unwrap() };
14401434
assert_eq!(c1_info, format!("{:?}", cert));
14411435
}
1436+
1437+
#[test]
1438+
fn cert_send_sync() {
1439+
assert!(crate::tests::TestTrait::<dyn Send, Certificate>::new().impls_trait(), "Certificate should be Send");
1440+
assert!(crate::tests::TestTrait::<dyn Send, MbedtlsBox<Certificate>>::new().impls_trait(), "MbedtlsBox<Certificate> should be Send");
1441+
assert!(crate::tests::TestTrait::<dyn Send, MbedtlsList<Certificate>>::new().impls_trait(), "MbedtlsList<Certificate> should be Send");
1442+
assert!(crate::tests::TestTrait::<dyn Sync, Certificate>::new().impls_trait(), "Certificate should be Sync");
1443+
assert!(crate::tests::TestTrait::<dyn Sync, MbedtlsBox<Certificate>>::new().impls_trait(), "MbedtlsBox<Certificate> should be Sync");
1444+
assert!(crate::tests::TestTrait::<dyn Sync, MbedtlsList<Certificate>>::new().impls_trait(), "MbedtlsList<Certificate> should be Sync");
1445+
}
14421446
}

0 commit comments

Comments
 (0)