Skip to content
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

Merged
merged 6 commits into from
Jan 10, 2021

Conversation

Enet4
Copy link
Owner

@Enet4 Enet4 commented Jan 8, 2021

Resolves #60.

  • Replicate AsPrimitive conversion into multiple constructor functions in 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 generic test functions

- 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
Copy link
Collaborator

@nilgoyette nilgoyette left a 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;
Copy link
Collaborator

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 :)


fn from_f64(value: f64) -> Self {
value as u8
}
Copy link
Collaborator

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!

Copy link
Owner Author

@Enet4 Enet4 Jan 9, 2021

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(
Copy link
Collaborator

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.

  1. Cast raw data from file) to the corresponding DataElement
  2. Cast those element to the requested DataElement O
  3. Apply slope and inter before creating the final ndarray

Copy link
Owner Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@nilgoyette nilgoyette left a 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(
Copy link
Collaborator

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.

@Enet4 Enet4 merged commit 16b7cbb into master Jan 10, 2021
@Enet4 Enet4 deleted the imp/no-infectious-asprimitive branch January 10, 2021 17:47
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

Successfully merging this pull request may close these issues.

Shortcut for the 10 AsPrimitive<T>
2 participants