Skip to content

From/To/AsPrimitive and NumCast for Complex<T>#33

Merged
bors[bot] merged 10 commits intorust-num:masterfrom
termoshtt:cast
Oct 3, 2018
Merged

From/To/AsPrimitive and NumCast for Complex<T>#33
bors[bot] merged 10 commits intorust-num:masterfrom
termoshtt:cast

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.

2 participants