]> git.lizzy.rs Git - rust.git/blobdiff - tests/run-pass/packed_struct.rs
Stacked Borrows: alignment does not matter
[rust.git] / tests / run-pass / packed_struct.rs
index 7aa53ef568f560c2f0d58a9f5fadfe6cfc3e0a15..5582caaf37ea5fdb9e3a9ef9033d282763384818 100644 (file)
@@ -1,4 +1,4 @@
-#![feature(unsize, coerce_unsized)]
+#![feature(unsize, coerce_unsized, raw_ref_op)]
 
 use std::collections::hash_map::DefaultHasher;
 use std::hash::Hash;
@@ -6,6 +6,7 @@
 fn test_basic() {
     #[repr(packed)]
     struct S {
+        fill: u8,
         a: i32,
         b: i64,
     }
@@ -30,6 +31,7 @@ fn test(t: Test2) {
     }
 
     let mut x = S {
+        fill: 0,
         a: 42,
         b: 99,
     };
@@ -37,9 +39,13 @@ fn test(t: Test2) {
     let b = x.b;
     assert_eq!(a, 42);
     assert_eq!(b, 99);
+    assert_eq!(&x.fill, &0); // `fill` just requirs 1-byte-align, so this is fine
     // can't do `assert_eq!(x.a, 42)`, because `assert_eq!` takes a reference
     assert_eq!({x.a}, 42);
     assert_eq!({x.b}, 99);
+    // but we *can* take a raw pointer!
+    assert_eq!(unsafe { (&raw const x.a).read_unaligned() }, 42);
+    assert_eq!(unsafe { (&raw const x.b).read_unaligned() }, 99);
 
     x.b = 77;
     assert_eq!({x.b}, 77);