Skip to content

Shouldn't Num be also implemented for std::num::Wrapping<T> ? #278

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

Closed
yoanlcq opened this issue Apr 20, 2017 · 3 comments
Closed

Shouldn't Num be also implemented for std::num::Wrapping<T> ? #278

yoanlcq opened this issue Apr 20, 2017 · 3 comments

Comments

@yoanlcq
Copy link
Contributor

yoanlcq commented Apr 20, 2017

That would be convenient - I'd like to be able to pass a std::num::Wrapping<T> to anything that accepts a Num type.

Part of the discussion on #175 went over this, so I'd like to share my current use case for such a thing :
I've experimented with a custom "marker" trait named Deterministic which is implemented on types on which basic operations are guaranteed to be consistent across platforms, build settings, etc.

  • std::num::Wrapping<T> implements Deterministic;
  • fixed-point numbers are good candidates;
  • primitive integer types aren't, because they might cause panics when they overflow;
  • floating-point number types certainly aren't.

With this, I'd like to write functions which only accept types that are both Num and Deterministic (for some kind of reproducible game state data), and I can't pass Wrapping<T>s on them - the only workarounds are either to redefine my own Wrapping type and have it implement Num, or define my own Num trait and... well, in short, nothing very appealing.
This "problem" is easy to solve from within this crate, but it seems more like a matter of agreeing on what the Num trait actually means. As far as I'm concerned, this crate should make Wrapping<T> implement Num.
Thoughts ?

@cuviper
Copy link
Member

cuviper commented Apr 21, 2017

It still feels a little weird to me for Num to permit silent overflow behavior, but I guess it's up to the user of they want that. Plus it's not something we can forbid anyway, as one can always write their own wrapping type like you mentioned. So, OK. Care to send a PR?

@yoanlcq
Copy link
Contributor Author

yoanlcq commented Apr 21, 2017

Yup! See #279.
To add to the discussion, Num does already permit silent overflow behaviour to some extent, because (if I'm not mistaken) primitive integer types are allowed to overflow on release builds. My 2 cents.

homu added a commit that referenced this issue Apr 21, 2017
Implemented Zero, One and Num for std::num::Wrapping<T>

Attempts to fix issue #278
@cuviper
Copy link
Member

cuviper commented Apr 21, 2017

Fixed by #279.

@cuviper cuviper closed this as completed Apr 21, 2017
remexre pushed a commit to remexre/num that referenced this issue Jun 1, 2017
Extend the Error API with line, column, category
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

No branches or pull requests

2 participants