@@ -38,7 +38,7 @@ pub mod ecp;
3838pub mod hash;
3939pub mod pk;
4040pub mod rng;
41- pub mod self_test;
41+ pub use mbedtls_selftest as self_test;
4242pub mod ssl;
4343pub mod x509;
4444pub mod alloc;
@@ -158,3 +158,69 @@ cfg_if::cfg_if! {
158158pub 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+ }
0 commit comments