Merge pull request #1071 from sfackler/const-macros

Add ERR_PACK
This commit is contained in:
Steven Fackler 2019-02-27 22:10:39 -08:00 committed by GitHub
commit a99f75fccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 9 deletions

View File

@ -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"

View File

@ -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<OsString> {
}
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 {

View File

@ -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 {
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 fn ERR_GET_FUNC(l: c_ulong) -> c_int {
pub const 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 {
pub const fn ERR_GET_REASON(l: c_ulong) -> c_int {
(l & 0xFFF) as c_int
}
}
#[repr(C)]

View File

@ -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
)*
}
}