Skip to content

Commit 612900a

Browse files
authored
Merge pull request #125 from cuviper/consts
Add const ZERO/ONE/I and impl ConstZero/ConstOne
2 parents f66164b + 2045f2d commit 612900a

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ optional = true
2424
version = "1"
2525

2626
[dependencies.num-traits]
27-
version = "0.2.11"
27+
version = "0.2.18"
2828
default-features = false
2929
features = ["i128"]
3030

src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::str::FromStr;
3030
#[cfg(feature = "std")]
3131
use std::error::Error;
3232

33-
use num_traits::{Inv, MulAdd, Num, One, Pow, Signed, Zero};
33+
use num_traits::{ConstOne, ConstZero, Inv, MulAdd, Num, One, Pow, Signed, Zero};
3434

3535
use num_traits::float::FloatCore;
3636
#[cfg(any(feature = "std", feature = "libm"))]
@@ -104,7 +104,9 @@ impl<T> Complex<T> {
104104
}
105105

106106
impl<T: Clone + Num> Complex<T> {
107-
/// Returns imaginary unit
107+
/// Returns the imaginary unit.
108+
///
109+
/// See also [`Complex::I`].
108110
#[inline]
109111
pub fn i() -> Self {
110112
Self::new(T::zero(), T::one())
@@ -1146,6 +1148,15 @@ impl<T: Clone + Num> Rem<T> for Complex<T> {
11461148
real_arithmetic!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64);
11471149

11481150
// constants
1151+
impl<T: ConstZero> Complex<T> {
1152+
/// A constant `Complex` 0.
1153+
pub const ZERO: Self = Self::new(T::ZERO, T::ZERO);
1154+
}
1155+
1156+
impl<T: Clone + Num + ConstZero> ConstZero for Complex<T> {
1157+
const ZERO: Self = Self::ZERO;
1158+
}
1159+
11491160
impl<T: Clone + Num> Zero for Complex<T> {
11501161
#[inline]
11511162
fn zero() -> Self {
@@ -1164,6 +1175,18 @@ impl<T: Clone + Num> Zero for Complex<T> {
11641175
}
11651176
}
11661177

1178+
impl<T: ConstOne + ConstZero> Complex<T> {
1179+
/// A constant `Complex` 1.
1180+
pub const ONE: Self = Self::new(T::ONE, T::ZERO);
1181+
1182+
/// A constant `Complex` _i_, the imaginary unit.
1183+
pub const I: Self = Self::new(T::ZERO, T::ONE);
1184+
}
1185+
1186+
impl<T: Clone + Num + ConstOne + ConstZero> ConstOne for Complex<T> {
1187+
const ONE: Self = Self::ONE;
1188+
}
1189+
11671190
impl<T: Clone + Num> One for Complex<T> {
11681191
#[inline]
11691192
fn one() -> Self {

0 commit comments

Comments
 (0)