-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
From/To/AsPrimitive and NumCast for Complex<T> #33
Conversation
Fix 1.15.0 case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a couple comments.
src/cast.rs
Outdated
($ty:ty, $to:ident) => { | ||
#[inline] | ||
fn $to(&self) -> Option<$ty> { | ||
if self.im == T::zero() { self.re.$to() } else { None } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use self.im.is_zero()
to avoid constructing a new value.
src/cast.rs
Outdated
}; | ||
} // impl_from_primitive | ||
|
||
impl<T: FromPrimitive + Zero> FromPrimitive for Complex<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you require just Zero
here, but full Num
elsewhere?
Most of the crate actually requires T: Clone + Num
, and then you could leverage:
impl<T: Clone + Num> From<T> for Complex<T>
Thanks review. I fixed them. |
Thanks! bors r+ |
33: From/To/AsPrimitive and NumCast for Complex<T> r=cuviper a=termoshtt I added a submodule `cast` which defines four traits in `num-traits::cast`: - `ToPrimitive::to_xxx` returns `None` if `im` is non-zero - `FromPrimitive::from_xxx(a)` returns `Complex { re: T::from_xxx(a), im: T::zero() }` - `AsPrimitive` drops the imaginary part of complex - `NumCast` is same as `ToPrimitive` Co-authored-by: Toshiki Teramura <toshiki.teramura@gmail.com>
Build succeeded |
Could you release 0.2.1? or any release schedule? |
Done! |
I added a submodule
cast
which defines four traits innum-traits::cast
:ToPrimitive::to_xxx
returnsNone
ifim
is non-zeroFromPrimitive::from_xxx(a)
returnsComplex { re: T::from_xxx(a), im: T::zero() }
AsPrimitive
drops the imaginary part of complexNumCast
is same asToPrimitive