From bfcb6ea38de9e0a286c7c9b08d6f73fd3b7e2231 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 26 Jul 2019 06:46:23 +0300 Subject: [PATCH] const new --- build.rs | 6 ++++++ src/lib.rs | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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..c95636d 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 {