blob: 15ad2cff1d4cd0d72632ba2579f8147aaded8af0 [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::*;
Alex Crichton13a6f2d2015-09-10 18:10:58 -070010#[cfg(not(windows))]
Alex Crichtona9adfbf2015-09-09 23:21:27 -070011use libc::types::os::common::bsd43::*;
12
Alex Crichton0df7c102015-09-10 16:35:37 -070013trait Pretty {
14 fn pretty(&self) -> String;
15}
16
17impl<T> Pretty for *const T {
18 fn pretty(&self) -> String { format!("{:?}", self) }
19}
20impl<T> Pretty for *mut T {
21 fn pretty(&self) -> String { format!("{:?}", self) }
22}
23macro_rules! p {
24 ($($i:ident)*) => ($(
25 impl Pretty for $i {
26 fn pretty(&self) -> String { format!("{} ({:#x})", self, self) }
27 }
28 )*)
29}
30p! { i8 i16 i32 i64 u8 u16 u32 u64 usize isize }
31
32fn same<T: Eq + Pretty>(rust: T, c: T, attr: &str) {
Alex Crichton3e5155b2015-09-09 23:46:19 -070033 if rust != c {
Alex Crichton0df7c102015-09-10 16:35:37 -070034 panic!("bad {}: rust: {} != c {}", attr, rust.pretty(), c.pretty());
Alex Crichton3e5155b2015-09-09 23:46:19 -070035 }
36}
37
Alex Crichtonc8b895c2015-09-10 13:24:15 -070038fn align<T: Any>() -> u64 {
39 // TODO: apparently these three types have less alignment in Rust on x86
40 // than they do in C this difference should.. probably be reconciled.
41 //
42 // Perhaps #27195?
43 if cfg!(target_pointer_width = "32") {
44 if TypeId::of::<T>() == TypeId::of::<f64>() ||
45 TypeId::of::<T>() == TypeId::of::<i64>() ||
46 TypeId::of::<T>() == TypeId::of::<u64>() {
47 return 8
48 }
49 }
50 mem::align_of::<T>() as u64
51}
52
Alex Crichton3e5155b2015-09-09 23:46:19 -070053macro_rules! offset_of {
54 ($ty:ident, $field:ident) => (
55 (&((*(0 as *const $ty)).$field)) as *const _ as u64
56 )
57}
58
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070059#[cfg(test)]
60include!(concat!(env!("OUT_DIR"), "/all.rs"));