]> git.lizzy.rs Git - rust.git/commitdiff
test align_to example
authorRalf Jung <post@ralfj.de>
Fri, 25 Oct 2019 18:26:01 +0000 (20:26 +0200)
committerRalf Jung <post@ralfj.de>
Fri, 25 Oct 2019 18:26:01 +0000 (20:26 +0200)
tests/run-pass/align_offset.rs

index 736889174839f9f458623f671a12cf51b2187dc3..1202fd00be157a8b6176f766533ce7e3a8b71ac7 100644 (file)
@@ -71,8 +71,31 @@ fn test_from_utf8() {
     println!("{:?}", std::str::from_utf8(content).unwrap());
 }
 
+fn test_u64_array() {
+    #[derive(Default)]
+    #[repr(align(8))]
+    struct AlignToU64<T>(T);
+
+    const BYTE_LEN: usize = std::mem::size_of::<[u64; 4]>();
+    type Data = AlignToU64<[u8; BYTE_LEN]>;
+
+    fn example(data: &Data) {
+        let (head, u64_arrays, tail) = unsafe { data.0.align_to::<[u64; 4]>() };
+
+        assert!(head.is_empty(), "buffer was not aligned for 64-bit numbers");
+        assert_eq!(u64_arrays.len(), 1, "buffer was not long enough");
+        assert!(tail.is_empty(), "buffer was too long");
+
+        let u64_array = &u64_arrays[0];
+        let _val = u64_array[0]; // make sure we can actually load this
+    }
+
+    example(&Data::default());
+}
+
 fn main() {
     test_align_offset();
     test_align_to();
     test_from_utf8();
+    test_u64_array();
 }