diff --git a/build.rs b/build.rs index 15590bb..1614ab4 100644 --- a/build.rs +++ b/build.rs @@ -4,11 +4,17 @@ use std::env; fn main() { let ac = autocfg::new(); + if ac.probe_type("i128") { println!("cargo:rustc-cfg=has_i128"); } else if env::var_os("CARGO_FEATURE_I128").is_some() { panic!("i128 support was not detected!"); } + // autocfg doesn't have a direct way to probe for `const fn` yet. + if ac.probe_rustc_version(1, 31) { + autocfg::emit("has_const_fn"); + } + autocfg::rerun_path(file!()); } diff --git a/src/lib.rs b/src/lib.rs index 52cfe02..46dc2e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,13 +92,23 @@ pub struct Complex { pub type Complex32 = Complex; pub type Complex64 = Complex; -impl Complex { +impl Complex { + #[cfg(has_const_fn)] + /// Create a new Complex + #[inline] + pub const fn new(re: T, im: T) -> Self { + Complex { re: re, im: im } + } + + #[cfg(not(has_const_fn))] /// Create a new Complex #[inline] pub fn new(re: T, im: T) -> Self { Complex { re: re, im: im } } +} +impl Complex { /// Returns imaginary unit #[inline] pub fn i() -> Self { @@ -2639,4 +2649,15 @@ mod test { c.set_one(); assert!(c.is_one()); } + + #[cfg(has_const_fn)] + #[test] + fn test_const() { + const R: f64 = 12.3; + const I: f64 = -4.5; + const C: Complex64 = Complex::new(R, I); + + assert_eq!(C.re, 12.3); + assert_eq!(C.im, -4.5); + } }