Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame^] | 1 | #![allow(bad_style)] |
| 2 | |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 3 | extern crate libc; |
| 4 | extern crate libc_test; |
| 5 | |
Alex Crichton | c8b895c | 2015-09-10 13:24:15 -0700 | [diff] [blame] | 6 | use std::any::{Any, TypeId}; |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 7 | use std::mem; |
| 8 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 9 | use libc::*; |
| 10 | use libc::types::os::common::bsd43::*; |
| 11 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame^] | 12 | trait Pretty { |
| 13 | fn pretty(&self) -> String; |
| 14 | } |
| 15 | |
| 16 | impl<T> Pretty for *const T { |
| 17 | fn pretty(&self) -> String { format!("{:?}", self) } |
| 18 | } |
| 19 | impl<T> Pretty for *mut T { |
| 20 | fn pretty(&self) -> String { format!("{:?}", self) } |
| 21 | } |
| 22 | macro_rules! p { |
| 23 | ($($i:ident)*) => ($( |
| 24 | impl Pretty for $i { |
| 25 | fn pretty(&self) -> String { format!("{} ({:#x})", self, self) } |
| 26 | } |
| 27 | )*) |
| 28 | } |
| 29 | p! { i8 i16 i32 i64 u8 u16 u32 u64 usize isize } |
| 30 | |
| 31 | fn same<T: Eq + Pretty>(rust: T, c: T, attr: &str) { |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 32 | if rust != c { |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame^] | 33 | panic!("bad {}: rust: {} != c {}", attr, rust.pretty(), c.pretty()); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Alex Crichton | c8b895c | 2015-09-10 13:24:15 -0700 | [diff] [blame] | 37 | fn align<T: Any>() -> u64 { |
| 38 | // TODO: apparently these three types have less alignment in Rust on x86 |
| 39 | // than they do in C this difference should.. probably be reconciled. |
| 40 | // |
| 41 | // Perhaps #27195? |
| 42 | if cfg!(target_pointer_width = "32") { |
| 43 | if TypeId::of::<T>() == TypeId::of::<f64>() || |
| 44 | TypeId::of::<T>() == TypeId::of::<i64>() || |
| 45 | TypeId::of::<T>() == TypeId::of::<u64>() { |
| 46 | return 8 |
| 47 | } |
| 48 | } |
| 49 | mem::align_of::<T>() as u64 |
| 50 | } |
| 51 | |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 52 | macro_rules! offset_of { |
| 53 | ($ty:ident, $field:ident) => ( |
| 54 | (&((*(0 as *const $ty)).$field)) as *const _ as u64 |
| 55 | ) |
| 56 | } |
| 57 | |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 58 | #[cfg(test)] |
| 59 | include!(concat!(env!("OUT_DIR"), "/all.rs")); |