Skip to content

Commit 481db9f

Browse files
committed
Add test for aligned references from slice iterators
1 parent 65a9cb6 commit 481db9f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(repr_align, attr_literals, core_intrinsics)]
11+
12+
use std::mem::align_of;
13+
use std::intrinsics::type_name;
14+
15+
fn has_aligned_refs<'a, I, T: 'a>(iterable: I)
16+
where
17+
I: Iterator<Item = &'a T>
18+
{
19+
for elt in iterable {
20+
unsafe {
21+
assert_eq!((elt as *const T as usize) % align_of::<T>(), 0,
22+
"Assertion failed for type {}", type_name::<T>());
23+
}
24+
}
25+
}
26+
27+
fn main() {
28+
#[derive(Copy, Clone)]
29+
struct Zst;
30+
31+
#[derive(Copy, Clone)]
32+
#[repr(align(64))]
33+
struct Aligned;
34+
35+
has_aligned_refs([Zst; 8].iter());
36+
has_aligned_refs([[0f64; 0]; 8].iter());
37+
has_aligned_refs([Aligned; 8].iter());
38+
has_aligned_refs([Zst; 8].iter_mut().map(|t| &*t));
39+
has_aligned_refs([[0f64; 0]; 8].iter_mut().map(|t| &*t));
40+
has_aligned_refs([Aligned; 8].iter_mut().map(|t| &*t));
41+
}

0 commit comments

Comments
 (0)