1 #![feature(unsize, coerce_unsized)]
3 use std::collections::hash_map::DefaultHasher;
30 let x = *t.other.other;
34 let mut x = S { fill: 0, a: 42, b: 99 };
39 assert_eq!(&x.fill, &0); // `fill` just requirs 1-byte-align, so this is fine
40 // can't do `assert_eq!(x.a, 42)`, because `assert_eq!` takes a reference
41 assert_eq!({ x.a }, 42);
42 assert_eq!({ x.b }, 99);
43 // but we *can* take a raw pointer!
44 assert_eq!(unsafe { ptr::addr_of!(x.a).read_unaligned() }, 42);
45 assert_eq!(unsafe { ptr::addr_of!(x.b).read_unaligned() }, 99);
48 assert_eq!({ x.b }, 77);
50 test(Test2 { x: 0, other: &Test1 { x: 0, other: &42 } });
56 struct UnalignedPtr<'a, T: ?Sized>
63 impl<'a, T, U> std::ops::CoerceUnsized<UnalignedPtr<'a, U>> for UnalignedPtr<'a, T>
65 T: std::marker::Unsize<U> + ?Sized,
71 let arr_unaligned: UnalignedPtr<[i32; 3]> = UnalignedPtr { data: &arr };
72 let arr_unaligned: UnalignedPtr<[i32]> = arr_unaligned;
73 let _unused = &arr_unaligned; // forcing an allocation, which could also yield "unaligned write"-errors
80 // Do an (aligned) load
82 // For the fun of it, test alignment
83 assert_eq!(&self.0 as *const _ as usize % std::mem::align_of::<u32>(), 0);
89 f1: u8, // this should move the second field to something not very aligned
93 let p = Packed { f1: 42, f2: Wrap(23) };
97 fn test_inner_packed() {
98 // Even if just the inner struct is packed, accesses to the outer field can get unaligned.
99 // Make sure that works.
101 #[derive(Clone, Copy)]
104 #[derive(Clone, Copy)]
105 struct Outer(u8, Inner);
107 let o = Outer(0, Inner(42));
119 static FOO: Foo = Foo { i: 42 };
121 assert_eq!({ FOO.i }, 42);
126 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
133 let x = P { a: 1usize, b: 2u8, c: 3usize };
134 let y = P { a: 1usize, b: 2u8, c: 4usize };
136 let _clone = x.clone();
138 assert_eq!(x.partial_cmp(&y).unwrap(), x.cmp(&y));
139 x.hash(&mut DefaultHasher::new());