From 953fe86b9abb510c0438985be26581f79dbe3dcb Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 27 Feb 2019 21:50:39 -0800 Subject: [PATCH] Add ERR_PACK Also make error functions const when targeting a new enough rustc --- openssl-sys/Cargo.toml | 3 ++- openssl-sys/build/main.rs | 11 +++++++++++ openssl-sys/src/err.rs | 28 ++++++++++++++++++---------- openssl-sys/src/macros.rs | 18 ++++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/openssl-sys/Cargo.toml b/openssl-sys/Cargo.toml index e09bac562..a1c57e6e5 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 4c8b33e76..a1f163d71 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 0804dbe73..ecaec3ed0 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 77bf005d5..84320b726 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 + )* + } +} -- GitLab