Skip to content

Commit 9566803

Browse files
committed
Auto merge of #1023 - RalfJung:align_to, r=RalfJung
test align_to example Fixes #873
2 parents ffb1476 + 459aea8 commit 9566803

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/run-pass/align_offset.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,31 @@ fn test_from_utf8() {
7171
println!("{:?}", std::str::from_utf8(content).unwrap());
7272
}
7373

74+
fn test_u64_array() {
75+
#[derive(Default)]
76+
#[repr(align(8))]
77+
struct AlignToU64<T>(T);
78+
79+
const BYTE_LEN: usize = std::mem::size_of::<[u64; 4]>();
80+
type Data = AlignToU64<[u8; BYTE_LEN]>;
81+
82+
fn example(data: &Data) {
83+
let (head, u64_arrays, tail) = unsafe { data.0.align_to::<[u64; 4]>() };
84+
85+
assert!(head.is_empty(), "buffer was not aligned for 64-bit numbers");
86+
assert_eq!(u64_arrays.len(), 1, "buffer was not long enough");
87+
assert!(tail.is_empty(), "buffer was too long");
88+
89+
let u64_array = &u64_arrays[0];
90+
let _val = u64_array[0]; // make sure we can actually load this
91+
}
92+
93+
example(&Data::default());
94+
}
95+
7496
fn main() {
7597
test_align_offset();
7698
test_align_to();
7799
test_from_utf8();
100+
test_u64_array();
78101
}

0 commit comments

Comments
 (0)