|
| 1 | +// Check that trait-objects without a principal codegen properly. |
| 2 | + |
| 3 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 4 | +use std::mem; |
| 5 | + |
| 6 | +// Array is to make sure the size is not exactly pointer-size, so |
| 7 | +// we can be sure we are measuring the right size in the |
| 8 | +// `size_of_val` test. |
| 9 | +struct SetOnDrop<'a>(&'a AtomicUsize, [u8; 64]); |
| 10 | +impl<'a> Drop for SetOnDrop<'a> { |
| 11 | + fn drop(&mut self) { |
| 12 | + self.0.store(self.0.load(Ordering::Relaxed)+1, Ordering::Relaxed); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +trait TypeEq<V: ?Sized> {} |
| 17 | +impl<T: ?Sized> TypeEq<T> for T {} |
| 18 | +fn assert_types_eq<U: ?Sized, V: ?Sized>() where U: TypeEq<V> {} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + // Check that different ways of writing the same type are equal. |
| 22 | + assert_types_eq::<dyn Sync, dyn Sync + Sync>(); |
| 23 | + assert_types_eq::<dyn Sync + Send, dyn Send + Sync>(); |
| 24 | + assert_types_eq::<dyn Sync + Send + Sync, dyn Send + Sync>(); |
| 25 | + |
| 26 | + // Check that codegen works. |
| 27 | + // |
| 28 | + // Using `AtomicUsize` here because `Cell<u32>` is not `Sync`, and |
| 29 | + // so can't be made into a `Box<dyn Sync>`. |
| 30 | + let c = AtomicUsize::new(0); |
| 31 | + { |
| 32 | + let d: Box<dyn Sync> = Box::new(SetOnDrop(&c, [0; 64])); |
| 33 | + |
| 34 | + assert_eq!(mem::size_of_val(&*d), |
| 35 | + mem::size_of::<SetOnDrop>()); |
| 36 | + assert_eq!(mem::align_of_val(&*d), |
| 37 | + mem::align_of::<SetOnDrop>()); |
| 38 | + assert_eq!(c.load(Ordering::Relaxed), 0); |
| 39 | + } |
| 40 | + assert_eq!(c.load(Ordering::Relaxed), 1); |
| 41 | +} |
0 commit comments