]>
Commit | Line | Data |
---|---|---|
ff7c6d11 XL |
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 | ||
11 | use std::cell::Cell; | |
12 | use std::mem; | |
13 | ||
14 | struct Aligned<'a> { | |
15 | drop_count: &'a Cell<usize> | |
16 | } | |
17 | ||
18 | #[inline(never)] | |
19 | fn check_align(ptr: *const Aligned) { | |
20 | assert_eq!(ptr as usize % mem::align_of::<Aligned>(), | |
21 | 0); | |
22 | } | |
23 | ||
24 | impl<'a> Drop for Aligned<'a> { | |
25 | fn drop(&mut self) { | |
26 | check_align(self); | |
27 | self.drop_count.set(self.drop_count.get() + 1); | |
28 | } | |
29 | } | |
30 | ||
31 | #[repr(packed)] | |
32 | struct Packed<'a>(u8, Aligned<'a>); | |
33 | ||
34 | fn main() { | |
35 | let drop_count = &Cell::new(0); | |
36 | { | |
37 | let mut p = Packed(0, Aligned { drop_count }); | |
38 | p.1 = Aligned { drop_count }; | |
39 | assert_eq!(drop_count.get(), 1); | |
40 | } | |
41 | assert_eq!(drop_count.get(), 2); | |
42 | } |