blob: 665747fb24f90a39a8dc019d6dee7be3ab2b90cb [file] [log] [blame]
Alex Crichton0df7c102015-09-10 16:35:37 -07001#![allow(bad_style)]
2
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07003extern crate libc;
4extern crate libc_test;
5
Alex Crichtonc8b895c2015-09-10 13:24:15 -07006use std::any::{Any, TypeId};
Alex Crichton16083062015-09-09 22:59:24 -07007use std::mem;
8
Alex Crichtona9adfbf2015-09-09 23:21:27 -07009use libc::*;
10use libc::types::os::common::bsd43::*;
11
Alex Crichton0df7c102015-09-10 16:35:37 -070012trait Pretty {
13 fn pretty(&self) -> String;
14}
15
16impl<T> Pretty for *const T {
17 fn pretty(&self) -> String { format!("{:?}", self) }
18}
19impl<T> Pretty for *mut T {
20 fn pretty(&self) -> String { format!("{:?}", self) }
21}
22macro_rules! p {
23 ($($i:ident)*) => ($(
24 impl Pretty for $i {
25 fn pretty(&self) -> String { format!("{} ({:#x})", self, self) }
26 }
27 )*)
28}
29p! { i8 i16 i32 i64 u8 u16 u32 u64 usize isize }
30
31fn same<T: Eq + Pretty>(rust: T, c: T, attr: &str) {
Alex Crichton3e5155b2015-09-09 23:46:19 -070032 if rust != c {
Alex Crichton0df7c102015-09-10 16:35:37 -070033 panic!("bad {}: rust: {} != c {}", attr, rust.pretty(), c.pretty());
Alex Crichton3e5155b2015-09-09 23:46:19 -070034 }
35}
36
Alex Crichtonc8b895c2015-09-10 13:24:15 -070037fn 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 Crichton3e5155b2015-09-09 23:46:19 -070052macro_rules! offset_of {
53 ($ty:ident, $field:ident) => (
54 (&((*(0 as *const $ty)).$field)) as *const _ as u64
55 )
56}
57
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070058#[cfg(test)]
59include!(concat!(env!("OUT_DIR"), "/all.rs"));