secp256k1_sys/
types.rs

1// SPDX-License-Identifier: CC0-1.0
2
3#![allow(non_camel_case_types)]
4
5pub type c_int = i32;
6pub type c_uchar = u8;
7pub type c_uint = u32;
8pub type size_t = usize;
9
10/// This might not match C's `c_char` exactly.
11/// The way we use it makes it fine either way but this type shouldn't be used outside of the library.
12pub type c_char = i8;
13
14pub use core::ffi::c_void;
15
16/// A type that is as aligned as the biggest alignment for fundamental types in C
17/// since C11 that means as aligned as `max_align_t` is.
18/// the exact size/alignment is unspecified.
19// 16 matches is as big as the biggest alignment in any arch that rust currently supports https://github.com/rust-lang/rust/blob/2c31b45ae878b821975c4ebd94cc1e49f6073fd0/library/std/src/sys_common/alloc.rs
20#[repr(align(16))]
21#[derive(Default, Copy, Clone)]
22pub struct AlignedType([u8; 16]);
23
24impl AlignedType {
25    pub fn zeroed() -> Self {
26        AlignedType([0u8; 16])
27    }
28
29    /// A static zeroed out AlignedType for use in static assignments of [AlignedType; _]
30    pub const ZERO: AlignedType = AlignedType([0u8; 16]);
31}
32
33#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
34pub(crate) const ALIGN_TO: usize = core::mem::align_of::<AlignedType>();
35
36#[cfg(test)]
37mod tests {
38    extern crate libc;
39    use std::any::TypeId;
40    use std::mem;
41    use std::os::raw;
42    use crate::{types, AlignedType};
43
44    #[test]
45    fn verify_types() {
46        assert_eq!(TypeId::of::<types::c_int>(), TypeId::of::<raw::c_int>());
47        assert_eq!(TypeId::of::<types::c_uchar>(), TypeId::of::<raw::c_uchar>());
48        assert_eq!(TypeId::of::<types::c_uint>(), TypeId::of::<raw::c_uint>());
49        assert_eq!(TypeId::of::<types::c_char>(), TypeId::of::<raw::c_char>());
50
51        assert!(mem::align_of::<AlignedType>() >= mem::align_of::<self::libc::max_align_t>());
52    }
53}
54
55#[doc(hidden)]
56#[cfg(target_arch = "wasm32")]
57pub fn sanity_checks_for_wasm() {
58    use core::mem::{align_of, size_of};
59    extern "C" {
60        pub static WASM32_INT_SIZE: c_uchar;
61        pub static WASM32_INT_ALIGN: c_uchar;
62
63        pub static WASM32_UNSIGNED_INT_SIZE: c_uchar;
64        pub static WASM32_UNSIGNED_INT_ALIGN: c_uchar;
65
66        pub static WASM32_SIZE_T_SIZE: c_uchar;
67        pub static WASM32_SIZE_T_ALIGN: c_uchar;
68
69        pub static WASM32_UNSIGNED_CHAR_SIZE: c_uchar;
70        pub static WASM32_UNSIGNED_CHAR_ALIGN: c_uchar;
71
72        pub static WASM32_PTR_SIZE: c_uchar;
73        pub static WASM32_PTR_ALIGN: c_uchar;
74    }
75    unsafe {
76        assert_eq!(size_of::<c_int>(), WASM32_INT_SIZE as usize);
77        assert_eq!(align_of::<c_int>(), WASM32_INT_ALIGN as usize);
78
79        assert_eq!(size_of::<c_uint>(), WASM32_UNSIGNED_INT_SIZE as usize);
80        assert_eq!(align_of::<c_uint>(), WASM32_UNSIGNED_INT_ALIGN as usize);
81
82        assert_eq!(size_of::<size_t>(), WASM32_SIZE_T_SIZE as usize);
83        assert_eq!(align_of::<size_t>(), WASM32_SIZE_T_ALIGN as usize);
84
85        assert_eq!(size_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_SIZE as usize);
86        assert_eq!(align_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_ALIGN as usize);
87
88        assert_eq!(size_of::<*const ()>(), WASM32_PTR_SIZE as usize);
89        assert_eq!(align_of::<*const ()>(), WASM32_PTR_ALIGN as usize);
90    }
91}