-
Notifications
You must be signed in to change notification settings - Fork 12
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
Remove public AsPrimitive trait bounds on scalars #78
Conversation
- Replicate AsPrimitive conversion methods into DataElement - Rewrite `convert_bytes_and_cast_to` as a macro - one for each scalar type - reduced trait bounds - Relax trait bounds on LinearTransform impls - Relax trait bounds on IntoNdArray methods - Relax trait bounds on test functions
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.
Thank you for working on this! I'm a little confused by 2 lines but I think it's a good change overall. The users of nifti-rs will indeed have sexier generic code ;)
fn from_f32(value: f32) -> Self; | ||
|
||
/// Create a single element by converting a scalar value. | ||
fn from_f64(value: f64) -> Self; |
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.
Haha, ok, wow, I see your plan now. We're lucky that there's not 100 POD :)
src/volume/element.rs
Outdated
|
||
fn from_f64(value: f64) -> Self { | ||
value as u8 | ||
} |
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 dont know much about macro, but is it possible to create one that creates several functions? Like generate_from_fns!(u8);
and BAM there's 10 functions with one without as
.. That would greatly help this file!
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.
It is possible to expand a macro into multiple function declarations, as suggested now in commit eee26e2. Making an exception for one of them without as
would be too tricky to even bother. ¯\_(ツ)_/¯
let mut data: Vec<O> = data.into_iter() | ||
.map($converter) | ||
.collect(); | ||
<O as DataElement>::Transform::linear_transform_many_inline( |
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 know that you didn't actually change this, it's the same behavior as before, but I find this part hard to understand. I think it deserves 3 comments explaining what each line does. In fact, I'm not even sure what's going on on 2) because I thought that 1 and 2 would be the same call.
- Cast raw data from file) to the corresponding DataElement
- Cast those element to the requested DataElement O
- Apply slope and inter before creating the final ndarray
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.
Agreed, I have included those comments now.
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.
Oups, there's no opening (
in my first sentence. I'm still not a fan of my own sentence though ^^
Cast raw data buffer (from file) to the DataElement that corresponds to the declared NiftiHeader::datatype.
It feels heavy. Feel free to edit to make it clearer.
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.
element.rs
is much shorter with the macro. Thank you.
let mut data: Vec<O> = data.into_iter() | ||
.map($converter) | ||
.collect(); | ||
<O as DataElement>::Transform::linear_transform_many_inline( |
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.
Oups, there's no opening (
in my first sentence. I'm still not a fan of my own sentence though ^^
Cast raw data buffer (from file) to the DataElement that corresponds to the declared NiftiHeader::datatype.
It feels heavy. Feel free to edit to make it clearer.
Resolves #60.
AsPrimitive
conversion into multiple constructor functions in DataElementconvert_bytes_and_cast_to
as a macroLinearTransform
implsIntoNdArray
methods