]> git.lizzy.rs Git - rust.git/blob - src/libstd/os/raw.rs
Prefer raw::c_char or libc::c_char and fix arm
[rust.git] / src / libstd / os / raw.rs
1 // Copyright 2015 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 //! Raw OS-specific types for the current platform/architecture
12
13 #![stable(feature = "raw_os", since = "1.1.0")]
14
15 #[cfg(all(not(target_os = "ios"), not(target_os = "macos"),
16           any(target_arch = "aarch64", target_arch = "arm",
17               target_os = "android")))]
18 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = u8;
19 #[cfg(any(target_os = "ios", target_os = "macos",
20           not(any(target_arch = "aarch64", target_arch = "arm",
21                   target_os = "android"))))]
22 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = i8;
23 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_schar = i8;
24 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uchar = u8;
25 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_short = i16;
26 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ushort = u16;
27 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_int = i32;
28 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uint = u32;
29 #[cfg(any(target_pointer_width = "32", windows))]
30 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i32;
31 #[cfg(any(target_pointer_width = "32", windows))]
32 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u32;
33 #[cfg(all(target_pointer_width = "64", not(windows)))]
34 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i64;
35 #[cfg(all(target_pointer_width = "64", not(windows)))]
36 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u64;
37 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_longlong = i64;
38 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulonglong = u64;
39 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_float = f32;
40 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_double = f64;
41
42 /// Type used to construct void pointers for use with C.
43 ///
44 /// This type is only useful as a pointer target. Do not use it as a
45 /// return type for FFI functions which have the `void` return type in
46 /// C. Use the unit type `()` or omit the return type instead.
47 // NB: For LLVM to recognize the void pointer type and by extension
48 //     functions like malloc(), we need to have it represented as i8* in
49 //     LLVM bitcode. The enum used here ensures this and prevents misuse
50 //     of the "raw" type by only having private variants.. We need two
51 //     variants, because the compiler complains about the repr attribute
52 //     otherwise.
53 #[repr(u8)]
54 #[stable(feature = "raw_os", since = "1.1.0")]
55 pub enum c_void {
56     #[unstable(feature = "c_void_variant", reason = "should not have to exist",
57                issue = "0")]
58     #[doc(hidden)] __variant1,
59     #[unstable(feature = "c_void_variant", reason = "should not have to exist",
60                issue = "0")]
61     #[doc(hidden)] __variant2,
62 }
63
64 #[cfg(test)]
65 mod tests {
66     use any::TypeId;
67     use libc;
68     use mem;
69
70     macro_rules! ok {
71         ($($t:ident)*) => {$(
72             assert!(TypeId::of::<libc::$t>() == TypeId::of::<raw::$t>(),
73                     "{} is wrong", stringify!($t));
74         )*}
75     }
76
77     macro_rules! ok_size {
78         ($($t:ident)*) => {$(
79             assert!(mem::size_of::<libc::$t>() == mem::size_of::<raw::$t>(),
80                     "{} is wrong", stringify!($t));
81         )*}
82     }
83
84     #[test]
85     fn same() {
86         use os::raw;
87         ok!(c_char c_schar c_uchar c_short c_ushort c_int c_uint c_long c_ulong
88             c_longlong c_ulonglong c_float c_double);
89     }
90
91     #[cfg(unix)]
92     fn unix() {
93         {
94             use os::unix::raw;
95             ok!(uid_t gid_t dev_t ino_t mode_t nlink_t off_t blksize_t blkcnt_t);
96         }
97         {
98             use sys::platform::raw;
99             ok_size!(stat);
100         }
101     }
102
103     #[cfg(windows)]
104     fn windows() {
105         use os::windows::raw;
106     }
107 }