-
Notifications
You must be signed in to change notification settings - Fork 13
Add Transmogrify concept with sus::mog<T>() #289
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
Merged
Merged
Conversation
This file contains hidden or 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
5050196
to
0ae1a74
Compare
9425d0b
to
9739b55
Compare
as_bits<T>() is like into() except it: - does not deduce the type, you must specify it - does not preserve the meaning, it preserves the bits instead - can be lossy, including trucation This provides bitwise conversions between integers for chromium#221.
This gives a cast-like syntax for fallably converting to a type in a value-preserving way for chromium#221.
The conversion is owning to owning (cheap copy), not reference to reference or owning to reference. So it's a 'to' transformation.
/// Casting from a float to an integer will round the float towards zero, except: /// * NaN will return 0. /// * Values larger than the maximum integer value, including INFINITY, will /// saturate to the maximum value of the integer type. /// * Values smaller than the minimum integer value, including NEG_INFINITY, /// will saturate to the minimum value of the integer type.
3e70ce5
to
ef820ce
Compare
Normalize the f32/f64 apis with ints they were missing: - Constructor from f32/f64 - From impl for f32/f64 - Assignment from f32/f64 This ensures you can convert up, as operator== already lets you convert up.
For converting types that may be out of bounds, use try_from(). If you want a lossy conversion we have to_bits() (which is to be renamed).
From always succeeds. Use try_from() or to_bits() instead for values that may not fit, with error handling possible in the first, and value loss in the second.
std::byte is an enum class so remove those specializations
In lack of a better name, we defer to Calvin and Hobbes to show us how to convert things to other types, through the Transmogrifier. The sus::mog<T>(f) operation will convert an object `f` to an object of type T, performing a copy as needed. The mog operation is supported for subspace numeric types both integers and floats, primitive integer and float types, and enums and enum classes. The operation can be applied to other types by providing a specialization of the TransmogrifyImpl<To, From> struct with a To mog_from(const From&) static method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #221
mog<T>()
is likeinto()
except it:It is much like
static_cast<T>()
except it does not introduce Undefined Behaviour when converting to/from floating point values.This provides lossy conversions between integers and floats (and enums) for #221.