-
Notifications
You must be signed in to change notification settings - Fork 102
Use generic trait parameters to minimize associated types #493
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
Open
calebzulawski
wants to merge
1
commit into
master
Choose a base branch
from
traits
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
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 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
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
Oops, something went wrong.
Oops, something went wrong.
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.
imo
Nshould be an associated#[type_const]but last I knew that's not done being implemented yet.something like:
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.
that would allow your example function's bounds to just be:
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.
sadly, it seems that hits the bug where specifying bounds hides information that could be deduced so the bound ends up having to be:
Simd<T, 4>: SimdFloat<Element = T, LEN = 4>https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=c84993a34513e1f45a01b986533be269
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 got it to work with just
Simd<T, 4>: SimdFloatby removingSimdFloat: SimdBaseand instead havingwhere Self: SimdBaseon all items in the trait.https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=c13634f70837e4b761b6a19592d069d9
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.
further reduced feature gates: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=b57a420a75142e3eda48993dc2f981f1
Uh oh!
There was an error while loading. Please reload this page.
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.
@programmerjake Those where more there at least for what I am using that for to generically do truncate or extend with operations while allowing me to specify constraints on the N, since const equality is also not stable either, not sure such operation would be needed for SIMD though. I more of just posted what I had to show that types can be used to replace a [type_const] associated const. A first class language construct is definitely nicer than a bunch type machinery boilerplate definitely agree!
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.
imo
Simd<T, N>ideally shouldn't need more or less trait bounds for lengths than[T; N], so if you're trying to be generic over length havingfn f<const N: usize>(v: Simd<f32, N>)seems good to me. imo you shouldn't need to mention known or generic lengths anywhere other than theNinSimd<T, N>(orMask), so I don't like having to haveSimd<T, N>: SomeTrait<N> + OtherTrait<N>where you have to repeat theNin all the trait bounds.Uh oh!
There was an error while loading. Please reload this page.
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.
Yea but an associated
#[type_const]would allow such a function to just befn f<T: Simd<f32>>(v: T).--Edit
@programmerjake Also if you need constraint the length you could do something like:
fn f<T: Simd<f32, N = 4>>(v: T)ect..That number trait I have allows something similar, I guess the main thing I don't like about it is that it makes my arrays opaque to just what trait constraint I put on it. Although the commented out ArrayLike trait allowed me to get around most the issues of that.
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.
Yes (if you used an actual trait rather than the
Simdtype). That said, I think using theSimd<f32, N>type is waay better, similar to how it's standard practice to writefn f<const N: usize>(v: [f32; N])instead offn f<T: ArrayOf<f32>>(v: T).if you already know both the length and element type then there's only one
Simdtype that matches, using a generic is just extra complexity for no reason, just useSimd<f32, 4>or thef32x4type alias. this is similar to how you'd write[f32; 4]instead ofT: Array<f32, 4>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 guess that depends on what your doing/preference, I am just saying #[type_const] can allow for less verbose signatures.