Skip to content
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

Merged
merged 10 commits into from Oct 3, 2018
Merged

Conversation

termoshtt
Copy link
Contributor

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

@termoshtt
Copy link
Contributor Author

Fix 1.15.0 case

Copy link
Member

@cuviper cuviper left a 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 }
Copy link
Member

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> {
Copy link
Member

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>

@termoshtt
Copy link
Contributor Author

Thanks review. I fixed them.

@cuviper
Copy link
Member

cuviper commented Oct 3, 2018

Thanks!

bors r+

bors bot added a commit that referenced this pull request Oct 3, 2018
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>
@bors
Copy link
Contributor

bors bot commented Oct 3, 2018

Build succeeded

@bors bors bot merged commit 330c91e into rust-num:master Oct 3, 2018
@termoshtt
Copy link
Contributor Author

Could you release 0.2.1? or any release schedule?

@cuviper
Copy link
Member

cuviper commented Oct 9, 2018

Could you release 0.2.1?

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants