test some more things around packed structs
This commit is contained in:
parent
bfda0a0a90
commit
98a5b24ef7
2 changed files with 41 additions and 2 deletions
|
|
@ -65,9 +65,9 @@ impl ToBar for Bar {
|
|||
|
||||
pub fn main() {
|
||||
// With a vec of ints.
|
||||
let f1 = Fat { f1: 5, f2: "some str", ptr: [1, 2, 3] };
|
||||
let f1 : Fat<[isize; 3]> = Fat { f1: 5, f2: "some str", ptr: [1, 2, 3] };
|
||||
foo(&f1);
|
||||
let f2 = &f1;
|
||||
let f2 : &Fat<[isize; 3]> = &f1;
|
||||
foo(f2);
|
||||
let f3: &Fat<[isize]> = f2;
|
||||
foo(f3);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,43 @@ fn test_unsizing() {
|
|||
let _unused = &arr_unaligned; // forcing an allocation, which could also yield "unaligned write"-errors
|
||||
}
|
||||
|
||||
fn test_drop() {
|
||||
struct Wrap(u32);
|
||||
impl Drop for Wrap {
|
||||
fn drop(&mut self) {
|
||||
// Do an (aligned) load
|
||||
let _test = self.0;
|
||||
// For the fun of it, test alignment
|
||||
assert_eq!(&self.0 as *const _ as usize % std::mem::align_of::<u32>(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(packed,C)]
|
||||
struct Packed<T> {
|
||||
f1: u8, // this should move the second field to something not very aligned
|
||||
f2: T,
|
||||
}
|
||||
|
||||
let p = Packed { f1: 42, f2: Wrap(23) };
|
||||
drop(p);
|
||||
}
|
||||
|
||||
fn test_inner_packed() {
|
||||
// Even if just the inner struct is packed, accesses to the outer field can get unaligned.
|
||||
// Make sure that works.
|
||||
#[repr(packed)]
|
||||
#[derive(Clone,Copy)]
|
||||
struct Inner(u32);
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
struct Outer(u8, Inner);
|
||||
|
||||
let o = Outer(0, Inner(42));
|
||||
let _x = o.1;
|
||||
let _y = (o.1).0;
|
||||
let _o2 = o.clone();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut x = S {
|
||||
a: 42,
|
||||
|
|
@ -64,4 +101,6 @@ fn main() {
|
|||
test(Test2 { x: 0, other: &Test1 { x: 0, other: &42 }});
|
||||
|
||||
test_unsizing();
|
||||
test_drop();
|
||||
test_inner_packed();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue