2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2021-05-13 14:42:25 +00:00
|
|
|
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
2018-09-06 12:45:33 +00:00
|
|
|
#![allow(non_snake_case)]
|
2018-09-06 12:41:12 +00:00
|
|
|
|
2016-08-22 16:56:14 +00:00
|
|
|
use std::mem::{size_of, size_of_val, align_of, align_of_val};
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed)]
|
2018-02-04 11:10:28 +00:00
|
|
|
struct Sp1 {
|
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed(2))]
|
|
|
|
struct Sp2 {
|
2016-08-22 16:56:14 +00:00
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
union U {
|
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed)]
|
2018-02-04 11:10:28 +00:00
|
|
|
union Up1 {
|
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed(2))]
|
|
|
|
union Up2 {
|
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C, packed(4))]
|
|
|
|
union Up4c {
|
2016-08-22 16:56:14 +00:00
|
|
|
a: u16,
|
|
|
|
b: [u8; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
const CS: S = S { a: 0, b: [0, 0, 0] };
|
2018-02-04 11:10:28 +00:00
|
|
|
const CSP1: Sp1 = Sp1 { a: 0, b: [0, 0, 0] };
|
|
|
|
const CSP2: Sp2 = Sp2 { a: 0, b: [0, 0, 0] };
|
2016-08-22 16:56:14 +00:00
|
|
|
const CU: U = U { b: [0, 0, 0] };
|
2018-02-04 11:10:28 +00:00
|
|
|
const CUP1: Up1 = Up1 { b: [0, 0, 0] };
|
|
|
|
const CUP2: Up2 = Up2 { b: [0, 0, 0] };
|
|
|
|
const CUP4C: Up4c = Up4c { b: [0, 0, 0] };
|
2016-08-22 16:56:14 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s = S { a: 0, b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<S>(), 6);
|
|
|
|
assert_eq!(size_of_val(&s), 6);
|
|
|
|
assert_eq!(size_of_val(&CS), 6);
|
|
|
|
assert_eq!(align_of::<S>(), 2);
|
|
|
|
assert_eq!(align_of_val(&s), 2);
|
|
|
|
assert_eq!(align_of_val(&CS), 2);
|
|
|
|
|
2018-02-04 11:10:28 +00:00
|
|
|
let sp1 = Sp1 { a: 0, b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<Sp1>(), 5);
|
|
|
|
assert_eq!(size_of_val(&sp1), 5);
|
|
|
|
assert_eq!(size_of_val(&CSP1), 5);
|
|
|
|
assert_eq!(align_of::<Sp1>(), 1);
|
|
|
|
assert_eq!(align_of_val(&sp1), 1);
|
|
|
|
assert_eq!(align_of_val(&CSP1), 1);
|
|
|
|
|
|
|
|
let sp2 = Sp2 { a: 0, b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<Sp2>(), 6);
|
|
|
|
assert_eq!(size_of_val(&sp2), 6);
|
|
|
|
assert_eq!(size_of_val(&CSP2), 6);
|
|
|
|
assert_eq!(align_of::<Sp2>(), 2);
|
|
|
|
assert_eq!(align_of_val(&sp2), 2);
|
|
|
|
assert_eq!(align_of_val(&CSP2), 2);
|
2016-08-22 16:56:14 +00:00
|
|
|
|
|
|
|
let u = U { b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<U>(), 4);
|
|
|
|
assert_eq!(size_of_val(&u), 4);
|
|
|
|
assert_eq!(size_of_val(&CU), 4);
|
|
|
|
assert_eq!(align_of::<U>(), 2);
|
|
|
|
assert_eq!(align_of_val(&u), 2);
|
|
|
|
assert_eq!(align_of_val(&CU), 2);
|
|
|
|
|
2018-02-04 11:10:28 +00:00
|
|
|
let Up1 = Up1 { b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<Up1>(), 3);
|
|
|
|
assert_eq!(size_of_val(&Up1), 3);
|
|
|
|
assert_eq!(size_of_val(&CUP1), 3);
|
|
|
|
assert_eq!(align_of::<Up1>(), 1);
|
|
|
|
assert_eq!(align_of_val(&Up1), 1);
|
|
|
|
assert_eq!(align_of_val(&CUP1), 1);
|
|
|
|
|
|
|
|
let up2 = Up2 { b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<Up2>(), 4);
|
|
|
|
assert_eq!(size_of_val(&up2), 4);
|
|
|
|
assert_eq!(size_of_val(&CUP2), 4);
|
|
|
|
assert_eq!(align_of::<Up2>(), 2);
|
|
|
|
assert_eq!(align_of_val(&up2), 2);
|
|
|
|
assert_eq!(align_of_val(&CUP2), 2);
|
|
|
|
|
|
|
|
let up4c = Up4c { b: [0, 0, 0] };
|
|
|
|
assert_eq!(size_of::<Up4c>(), 4);
|
|
|
|
assert_eq!(size_of_val(&up4c), 4);
|
|
|
|
assert_eq!(size_of_val(&CUP4C), 4);
|
|
|
|
assert_eq!(align_of::<Up4c>(), 2);
|
|
|
|
assert_eq!(align_of_val(&up4c), 2);
|
|
|
|
assert_eq!(align_of_val(&CUP4C), 2);
|
2016-08-26 16:23:42 +00:00
|
|
|
|
|
|
|
hybrid::check_hybrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
mod hybrid {
|
2018-02-04 11:10:28 +00:00
|
|
|
use std::mem::{size_of, align_of};
|
2016-08-26 16:23:42 +00:00
|
|
|
|
|
|
|
#[repr(packed)]
|
2020-10-04 20:24:14 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2016-08-26 16:23:42 +00:00
|
|
|
struct S1 {
|
|
|
|
a: u16,
|
|
|
|
b: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed)]
|
|
|
|
union U {
|
|
|
|
s: S1,
|
|
|
|
c: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed)]
|
|
|
|
struct S2 {
|
|
|
|
d: u8,
|
|
|
|
u: U,
|
|
|
|
}
|
|
|
|
|
2018-02-04 11:10:28 +00:00
|
|
|
#[repr(C, packed(2))]
|
|
|
|
struct S1C {
|
|
|
|
a: u16,
|
|
|
|
b: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C, packed(2))]
|
|
|
|
union UC {
|
|
|
|
s: S1,
|
|
|
|
c: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C, packed(2))]
|
|
|
|
struct S2C {
|
|
|
|
d: u8,
|
|
|
|
u: UC,
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:23:42 +00:00
|
|
|
pub fn check_hybrid() {
|
2018-02-04 11:10:28 +00:00
|
|
|
assert_eq!(align_of::<S1>(), 1);
|
2016-08-26 16:23:42 +00:00
|
|
|
assert_eq!(size_of::<S1>(), 3);
|
2018-02-04 11:10:28 +00:00
|
|
|
assert_eq!(align_of::<U>(), 1);
|
2016-08-26 16:23:42 +00:00
|
|
|
assert_eq!(size_of::<U>(), 3);
|
2018-02-04 11:10:28 +00:00
|
|
|
assert_eq!(align_of::<S2>(), 1);
|
2016-08-26 16:23:42 +00:00
|
|
|
assert_eq!(size_of::<S2>(), 4);
|
2018-02-04 11:10:28 +00:00
|
|
|
|
|
|
|
assert_eq!(align_of::<S1C>(), 2);
|
|
|
|
assert_eq!(size_of::<S1C>(), 4);
|
|
|
|
assert_eq!(align_of::<UC>(), 2);
|
|
|
|
assert_eq!(size_of::<UC>(), 4);
|
|
|
|
assert_eq!(align_of::<S2C>(), 2);
|
|
|
|
assert_eq!(size_of::<S2C>(), 6);
|
2016-08-26 16:23:42 +00:00
|
|
|
}
|
2016-08-22 16:56:14 +00:00
|
|
|
}
|