diff --git a/openssl-sys/Cargo.toml b/openssl-sys/Cargo.toml index e09bac56265c3575cb6175767ef6f35726e3cae3..a1c57e6e57cf4376cf9f1e909c7c7f84e5ae6506 100644 --- a/openssl-sys/Cargo.toml +++ b/openssl-sys/Cargo.toml @@ -18,9 +18,10 @@ vendored = ['openssl-src'] libc = "0.2" [build-dependencies] -pkg-config = "0.3.9" cc = "1.0" openssl-src = { version = "111.0.1", optional = true } +pkg-config = "0.3.9" +rustc_version = "0.2" [target.'cfg(target_env = "msvc")'.build-dependencies] vcpkg = "0.2" diff --git a/openssl-sys/build/main.rs b/openssl-sys/build/main.rs index 4c8b33e76161fc3127abb4e86486a3d7f9d2b2b1..a1f163d71cf36354963d0c2957052e3e86396d0c 100644 --- a/openssl-sys/build/main.rs +++ b/openssl-sys/build/main.rs @@ -2,6 +2,7 @@ extern crate cc; #[cfg(feature = "vendored")] extern crate openssl_src; extern crate pkg_config; +extern crate rustc_version; #[cfg(target_env = "msvc")] extern crate vcpkg; @@ -41,6 +42,8 @@ fn env(name: &str) -> Option { } fn main() { + check_rustc_versions(); + let target = env::var("TARGET").unwrap(); let (lib_dir, include_dir) = find::get_openssl(&target); @@ -90,6 +93,14 @@ fn main() { } } +fn check_rustc_versions() { + let version = rustc_version::version().unwrap(); + + if version >= rustc_version::Version::new(1, 31, 0) { + println!("cargo:rustc-cfg=const_fn"); + } +} + /// Validates the header files found in `include_dir` and then returns the /// version string of OpenSSL. fn validate_headers(include_dirs: &[PathBuf]) -> Version { diff --git a/openssl-sys/src/err.rs b/openssl-sys/src/err.rs index 0804dbe736b36771eb8209dc70f81f177dc8c387..ecaec3ed0645007b1374acb824d6aafc61f21afe 100644 --- a/openssl-sys/src/err.rs +++ b/openssl-sys/src/err.rs @@ -5,16 +5,24 @@ pub const ERR_TXT_STRING: c_int = 0x02; pub const ERR_LIB_PEM: c_int = 9; -pub fn ERR_GET_LIB(l: c_ulong) -> c_int { - ((l >> 24) & 0x0FF) as c_int -} - -pub fn ERR_GET_FUNC(l: c_ulong) -> c_int { - ((l >> 12) & 0xFFF) as c_int -} - -pub fn ERR_GET_REASON(l: c_ulong) -> c_int { - (l & 0xFFF) as c_int +const_fn! { + pub const fn ERR_PACK(l: c_int, f: c_int, r: c_int) -> c_ulong { + ((l as c_ulong & 0x0FF) << 24) | + ((f as c_ulong & 0xFFF) << 12) | + ((r as c_ulong & 0xFFF)) + } + + pub const fn ERR_GET_LIB(l: c_ulong) -> c_int { + ((l >> 24) & 0x0FF) as c_int + } + + pub const fn ERR_GET_FUNC(l: c_ulong) -> c_int { + ((l >> 12) & 0xFFF) as c_int + } + + pub const fn ERR_GET_REASON(l: c_ulong) -> c_int { + (l & 0xFFF) as c_int + } } #[repr(C)] diff --git a/openssl-sys/src/macros.rs b/openssl-sys/src/macros.rs index 77bf005d5df01ad6bdfe5db14f924782198257e5..84320b7265e22eb7ea32e7a65dfcf9c561734dd4 100644 --- a/openssl-sys/src/macros.rs +++ b/openssl-sys/src/macros.rs @@ -67,3 +67,21 @@ macro_rules! stack { } }; } + +#[cfg(const_fn)] +macro_rules! const_fn { + ($(pub const fn $name:ident($($arg:ident: $t:ty),*) -> $ret:ty $b:block)*) => { + $( + pub const fn $name($($arg: $t),*) -> $ret $b + )* + } +} + +#[cfg(not(const_fn))] +macro_rules! const_fn { + ($(pub const fn $name:ident($($arg:ident: $t:ty),*) -> $ret:ty $b:block)*) => { + $( + pub fn $name($($arg: $t),*) -> $ret $b + )* + } +}