-
Notifications
You must be signed in to change notification settings - Fork 70
Num traits #218
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
Num traits #218
Changes from 13 commits
00ffb78
629c7c1
164ae46
60fc478
57cab5c
a13ddd0
43e079f
fcfe281
cae3a29
fd074b9
629356f
cc62784
2d24864
dadbee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,13 @@ use crate::Zero; | |
| use core::fmt; | ||
| use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| use num_traits::{Num}; | ||
| #[cfg(feature = "num-traits")] | ||
| use core::ops::{Div, Rem, Mul, Add, Sub}; | ||
| #[cfg(feature = "num-traits")] | ||
| use crate::{Integer, NonZero}; | ||
|
|
||
| #[cfg(feature = "rand_core")] | ||
| use {crate::Random, rand_core::CryptoRngCore}; | ||
|
|
||
|
|
@@ -63,6 +70,68 @@ impl<T: ConstantTimeEq> ConstantTimeEq for Wrapping<T> { | |
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| impl<T: Zero> num_traits::Zero for Wrapping<T> | ||
| where Wrapping<T>: | ||
| Add<Wrapping<T>, Output = Wrapping<T>> | ||
| + PartialEq<Wrapping<T>> | ||
| { | ||
| fn zero() -> Self { | ||
| Self::ZERO | ||
| } | ||
|
|
||
| fn is_zero(&self) -> bool { self == &Self::ZERO } | ||
| } | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| impl<T: Integer> num_traits::One for Wrapping<T> | ||
| where Wrapping<T>: | ||
| Add<Wrapping<T>, Output = Wrapping<T>> | ||
| + Mul<Wrapping<T>, Output = Wrapping<T>> | ||
| { | ||
| fn one() -> Self { | ||
| Wrapping(T::ONE) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| impl<T: Zero> Div<Wrapping<T>> for Wrapping<T> | ||
| where Wrapping<T>: Div<NonZero<T>, Output = Wrapping<T>> | ||
| { | ||
| type Output = Wrapping<T>; | ||
|
|
||
| fn div(self, rhs: Wrapping<T>) -> Self::Output { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and |
||
| self / NonZero::new(rhs.0).unwrap() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| impl<T: Zero> Rem<Wrapping<T>> for Wrapping<T> | ||
| where Wrapping<T>: Rem<NonZero<T>, Output = Wrapping<T>> | ||
| { | ||
| type Output = Wrapping<T>; | ||
|
|
||
| fn rem(self, rhs: Wrapping<T>) -> Self::Output { | ||
| self % NonZero::new(rhs.0).unwrap() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "num-traits")] | ||
| impl<T: Integer + Zero> Num for Wrapping<T> | ||
| where Wrapping<T>: | ||
| Add<Wrapping<T>, Output = Wrapping<T>> | ||
| + Sub<Wrapping<T>, Output = Wrapping<T>> | ||
| + Mul<Wrapping<T>, Output = Wrapping<T>> | ||
| + Div<NonZero<T>, Output = Wrapping<T>> | ||
| + Rem<NonZero<T>, Output = Wrapping<T>> | ||
| { | ||
| type FromStrRadixErr = (); | ||
|
|
||
| fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we want to handle this? Obviously this is unimplemented logic for this crate, and I'm not sure whether we want to support this (or, for example, return an error for any radix other than 2, 10 or 16.)
My thoughts are to actually to use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| todo!() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "rand_core")] | ||
| impl<T: Random> Random for Wrapping<T> { | ||
| fn random(rng: &mut impl CryptoRngCore) -> Self { | ||
|
|
@@ -90,6 +159,24 @@ impl<T: Serialize> Serialize for Wrapping<T> { | |
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "num-traits"))] | ||
| mod tests { | ||
| use num_traits::{Num, NumOps, Zero, One}; | ||
| use core::ops::{Div, Rem, Mul, Add, Sub}; | ||
| use crate::{Integer, NonZero}; | ||
|
|
||
| use crate::{Wrapping, U64}; | ||
|
|
||
| fn assures_num<T: Num>(x: T) {} | ||
|
|
||
| #[test] | ||
| fn num_traits() { | ||
| const TEST: Wrapping<U64> = Wrapping(U64::from_u64(0x0011223344556677)); | ||
|
|
||
| assures_num(TEST) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My logic here is: I'm not sure if there's a better way of checking this. |
||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "serde"))] | ||
| mod tests { | ||
| use crate::{Wrapping, U64}; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.