-
Notifications
You must be signed in to change notification settings - Fork 26
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
Implement derives for generic wrapper types #37
Open
oli-cosmian
wants to merge
2
commits into
rust-num:master
Choose a base branch
from
oli-cosmian:derive_generic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
extern crate num as num_renamed; | ||
#[macro_use] | ||
extern crate num_derive; | ||
|
||
use crate::num_renamed::{Float, FromPrimitive, Num, NumCast, One, ToPrimitive, Zero}; | ||
use std::ops::Neg; | ||
|
||
#[derive( | ||
Debug, | ||
Clone, | ||
Copy, | ||
PartialEq, | ||
PartialOrd, | ||
ToPrimitive, | ||
FromPrimitive, | ||
NumOps, | ||
NumCast, | ||
One, | ||
Zero, | ||
Num, | ||
Float, | ||
)] | ||
struct MyThing<T: Cake>(T) | ||
where | ||
T: Lie; | ||
|
||
trait Cake {} | ||
trait Lie {} | ||
|
||
impl Cake for f32 {} | ||
impl Lie for f32 {} | ||
|
||
impl<T: Neg<Output = T> + Cake + Lie> Neg for MyThing<T> { | ||
type Output = Self; | ||
fn neg(self) -> Self { | ||
MyThing(self.0.neg()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_from_primitive() { | ||
assert_eq!(MyThing::from_u32(25), Some(MyThing(25.0))); | ||
} | ||
|
||
#[test] | ||
fn test_from_primitive_128() { | ||
assert_eq!( | ||
MyThing::from_i128(std::i128::MIN), | ||
Some(MyThing((-2.0).powi(127))) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_to_primitive() { | ||
assert_eq!(MyThing(25.0).to_u32(), Some(25)); | ||
} | ||
|
||
#[test] | ||
fn test_to_primitive_128() { | ||
let f: MyThing<f32> = MyThing::from_f32(std::f32::MAX).unwrap(); | ||
assert_eq!(f.to_i128(), None); | ||
assert_eq!(f.to_u128(), Some(0xffff_ff00_0000_0000_0000_0000_0000_0000)); | ||
} | ||
|
||
#[test] | ||
fn test_num_ops() { | ||
assert_eq!(MyThing(25.0) + MyThing(10.0), MyThing(35.0)); | ||
assert_eq!(MyThing(25.0) - MyThing(10.0), MyThing(15.0)); | ||
assert_eq!(MyThing(25.0) * MyThing(2.0), MyThing(50.0)); | ||
assert_eq!(MyThing(25.0) / MyThing(10.0), MyThing(2.5)); | ||
assert_eq!(MyThing(25.0) % MyThing(10.0), MyThing(5.0)); | ||
} | ||
|
||
#[test] | ||
fn test_num_cast() { | ||
assert_eq!(<MyThing<f32> as NumCast>::from(25u8), Some(MyThing(25.0))); | ||
} | ||
|
||
#[test] | ||
fn test_zero() { | ||
assert_eq!(MyThing::zero(), MyThing(0.0)); | ||
} | ||
|
||
#[test] | ||
fn test_one() { | ||
assert_eq!(MyThing::one(), MyThing(1.0)); | ||
} | ||
|
||
#[test] | ||
fn test_num() { | ||
assert_eq!(MyThing::from_str_radix("25", 10).ok(), Some(MyThing(25.0))); | ||
} | ||
|
||
#[test] | ||
fn test_float() { | ||
assert_eq!(MyThing(4.0).log(MyThing(2.0)), MyThing(2.0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
extern crate num as num_renamed; | ||
#[macro_use] | ||
extern crate num_derive; | ||
|
||
use crate::num_renamed::{FromPrimitive}; | ||
use std::ops::Neg; | ||
use std::num::Wrapping; | ||
|
||
#[derive( | ||
Debug, | ||
Clone, | ||
Copy, | ||
PartialOrd, | ||
ToPrimitive, | ||
FromPrimitive, | ||
NumOps, | ||
NumCast, | ||
One, | ||
Zero, | ||
Num, | ||
)] | ||
struct MyThing<T>(Wrapping<T>); | ||
|
||
impl<T> PartialEq for MyThing<T> where Wrapping<T>: PartialEq { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.0.eq(&other.0) | ||
} | ||
} | ||
|
||
impl<T: Neg<Output = T>> Neg for MyThing<T> where Wrapping<T>: Neg<Output = Wrapping<T>> { | ||
type Output = Self; | ||
fn neg(self) -> Self { | ||
MyThing(self.0.neg()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_num_ops() { | ||
assert_eq!(MyThing::<u32>::from_i128(25).unwrap() + MyThing::<u32>::from_i128(10).unwrap(), MyThing::<u32>::from_i128(35).unwrap()); | ||
assert_eq!(MyThing::<u32>::from_i128(25).unwrap() - MyThing::<u32>::from_i128(10).unwrap(), MyThing::<u32>::from_i128(15).unwrap()); | ||
assert_eq!(MyThing::<u32>::from_i128(25).unwrap() * MyThing::<u32>::from_i128(2).unwrap(), MyThing::<u32>::from_i128(50).unwrap()); | ||
assert_eq!(MyThing::<u32>::from_i128(25).unwrap() / MyThing::<u32>::from_i128(10).unwrap(), MyThing::<u32>::from_i128(2).unwrap()); | ||
assert_eq!(MyThing::<u32>::from_i128(25).unwrap() % MyThing::<u32>::from_i128(10).unwrap(), MyThing::<u32>::from_i128(5).unwrap()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since
Num
requiresPartialEq
, and the libstd derive causesPartialEq
to get aT: PartialEq
bound instead of aWrapping<T>: PartialEq
bound, this workaround is required to use the new num derives from this PR with such types.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.
Is there a meaningful difference? The
std
implementation looks like this:You can't
impl PartialEq for Wrapping<LocalType>
, so it seem likeT: PartialEq
andWrapping<T>: PartialEq
should be equivalent.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.
I'm not sure why there's a difference, but there definitely is one. I'll look into it