]> git.lizzy.rs Git - rust.git/blob - src/libstd/os/raw/mod.rs
dc33747c05b0670da0c514c6f8dd098efa6b591a
[rust.git] / src / libstd / os / raw / mod.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 //! Platform-specific types, as defined by C.
12 //!
13 //! Code that interacts via FFI will almost certainly be using the
14 //! base types provided by C, which aren't nearly as nicely defined
15 //! as Rust's primitive types. This module provides types which will
16 //! match those defined by C, so that code that interacts with C will
17 //! refer to the correct types.
18
19 #![stable(feature = "raw_os", since = "1.1.0")]
20
21 use fmt;
22
23 #[doc(include = "os/raw/char.md")]
24 #[cfg(any(all(target_os = "linux", any(target_arch = "aarch64",
25                                        target_arch = "arm",
26                                        target_arch = "powerpc",
27                                        target_arch = "powerpc64",
28                                        target_arch = "s390x")),
29           all(target_os = "android", any(target_arch = "aarch64",
30                                          target_arch = "arm")),
31           all(target_os = "l4re", target_arch = "x86_64"),
32           all(target_os = "netbsd", any(target_arch = "aarch64",
33                                         target_arch = "arm",
34                                         target_arch = "powerpc")),
35           all(target_os = "openbsd", target_arch = "aarch64"),
36           all(target_os = "fuchsia", target_arch = "aarch64")))]
37 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = u8;
38 #[doc(include = "os/raw/char.md")]
39 #[cfg(not(any(all(target_os = "linux", any(target_arch = "aarch64",
40                                            target_arch = "arm",
41                                            target_arch = "powerpc",
42                                            target_arch = "powerpc64",
43                                            target_arch = "s390x")),
44               all(target_os = "android", any(target_arch = "aarch64",
45                                              target_arch = "arm")),
46               all(target_os = "l4re", target_arch = "x86_64"),
47               all(target_os = "netbsd", any(target_arch = "aarch64",
48                                             target_arch = "arm",
49                                             target_arch = "powerpc")),
50               all(target_os = "openbsd", target_arch = "aarch64"),
51               all(target_os = "fuchsia", target_arch = "aarch64"))))]
52 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = i8;
53 #[doc(include = "os/raw/schar.md")]
54 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_schar = i8;
55 #[doc(include = "os/raw/uchar.md")]
56 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uchar = u8;
57 #[doc(include = "os/raw/short.md")]
58 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_short = i16;
59 #[doc(include = "os/raw/ushort.md")]
60 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ushort = u16;
61 #[doc(include = "os/raw/int.md")]
62 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_int = i32;
63 #[doc(include = "os/raw/uint.md")]
64 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uint = u32;
65 #[doc(include = "os/raw/long.md")]
66 #[cfg(any(target_pointer_width = "32", windows))]
67 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i32;
68 #[doc(include = "os/raw/ulong.md")]
69 #[cfg(any(target_pointer_width = "32", windows))]
70 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u32;
71 #[doc(include = "os/raw/long.md")]
72 #[cfg(all(target_pointer_width = "64", not(windows)))]
73 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i64;
74 #[doc(include = "os/raw/ulong.md")]
75 #[cfg(all(target_pointer_width = "64", not(windows)))]
76 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u64;
77 #[doc(include = "os/raw/longlong.md")]
78 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_longlong = i64;
79 #[doc(include = "os/raw/ulonglong.md")]
80 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulonglong = u64;
81 #[doc(include = "os/raw/float.md")]
82 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_float = f32;
83 #[doc(include = "os/raw/double.md")]
84 #[stable(feature = "raw_os", since = "1.1.0")] pub type c_double = f64;
85
86 /// Equivalent to C's `void` type when used as a [pointer].
87 ///
88 /// In essence, `*const c_void` is equivalent to C's `const void*`
89 /// and `*mut c_void` is equivalent to C's `void*`. That said, this is
90 /// *not* the same as C's `void` return type, which is Rust's `()` type.
91 ///
92 /// Ideally, this type would be equivalent to [`!`], but currently it may
93 /// be more ideal to use `c_void` for FFI purposes.
94 ///
95 /// [`!`]: ../../primitive.never.html
96 /// [pointer]: ../../primitive.pointer.html
97 // NB: For LLVM to recognize the void pointer type and by extension
98 //     functions like malloc(), we need to have it represented as i8* in
99 //     LLVM bitcode. The enum used here ensures this and prevents misuse
100 //     of the "raw" type by only having private variants.. We need two
101 //     variants, because the compiler complains about the repr attribute
102 //     otherwise.
103 #[repr(u8)]
104 #[stable(feature = "raw_os", since = "1.1.0")]
105 pub enum c_void {
106     #[unstable(feature = "c_void_variant", reason = "should not have to exist",
107                issue = "0")]
108     #[doc(hidden)] __variant1,
109     #[unstable(feature = "c_void_variant", reason = "should not have to exist",
110                issue = "0")]
111     #[doc(hidden)] __variant2,
112 }
113
114 #[stable(feature = "std_debug", since = "1.16.0")]
115 impl fmt::Debug for c_void {
116     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117         f.pad("c_void")
118     }
119 }
120
121 #[cfg(test)]
122 #[allow(unused_imports)]
123 mod tests {
124     use any::TypeId;
125     use libc;
126     use mem;
127
128     macro_rules! ok {
129         ($($t:ident)*) => {$(
130             assert!(TypeId::of::<libc::$t>() == TypeId::of::<raw::$t>(),
131                     "{} is wrong", stringify!($t));
132         )*}
133     }
134
135     #[test]
136     fn same() {
137         use os::raw;
138         ok!(c_char c_schar c_uchar c_short c_ushort c_int c_uint c_long c_ulong
139             c_longlong c_ulonglong c_float c_double);
140     }
141 }