rust/src/test/run-pass/enum-alignment.rs

32 lines
840 B
Rust
Raw Normal View History

// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::cast;
use std::mem;
fn addr_of<T>(ptr: &T) -> uint {
2014-02-14 17:42:01 -06:00
ptr as *T as uint
}
fn is_aligned<T>(ptr: &T) -> bool {
2013-04-26 16:04:39 -05:00
unsafe {
let addr: uint = cast::transmute(ptr);
(addr % mem::min_align_of::<T>()) == 0
2013-04-26 16:04:39 -05:00
}
}
pub fn main() {
let x = Some(0u64);
match x {
None => fail!(),
2013-03-28 20:39:09 -05:00
Some(ref y) => assert!(is_aligned(y))
}
}