Skip to content

Commit

Permalink
const new
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jul 26, 2019
1 parent dc2157f commit bfcb6ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build.rs
Expand Up @@ -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!());
}
12 changes: 11 additions & 1 deletion src/lib.rs
Expand Up @@ -92,13 +92,23 @@ pub struct Complex<T> {
pub type Complex32 = Complex<f32>;
pub type Complex64 = Complex<f64>;

impl<T: Clone + Num> Complex<T> {
impl<T> Complex<T> {
#[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<T: Clone + Num> Complex<T> {
/// Returns imaginary unit
#[inline]
pub fn i() -> Self {
Expand Down

0 comments on commit bfcb6ea

Please sign in to comment.