-
Notifications
You must be signed in to change notification settings - Fork 131
Handle misaligned arrays in as_slice
#525
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
+128
−23
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28401f6
Handle misaligned arrays in `as_slice`
jakelishman 153f622
Change `NotContiguousError` to `AsSliceError`
jakelishman 06ecc68
Revert to opaque error type
jakelishman ff3e22e
Add test coverage of misaligned empty vec
jakelishman 9df8ec6
Correct as_slice_mut test
jakelishman 7c8c975
add version to deprecation
Icxolu 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
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
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.
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.
I think adjusting the error type makes sense. Maybe we could consider adding
#[non_exhaustive]now, so that we can extend this in the future in a non-breaking way if that happens to be necessary.Alternatively we could also think about keeping this as an opaque error (just renamed and a new error message). Not sure if consumers would ever need to act differently depending on the condition, given that there is not much they can do about it. I tend to lean towards this, what do you think?
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 can't think of a situation where there'd be another (detectable) error in converting to a slice - the Rust rules are "aligned, contiguous and initialised", but initialised isn't detectable, and we already implicitly require Python space to behave safely for Rust around uninitialised data (no passing
np.emptyarrays, etc), so I'm not certain ifnon_exhaustivewould be worth it.When I made the enum I was roughly thinking that you can handle unaligned arrays differently to non-contiguous ones; you can cast an unaligned one to a pointer and do unaligned strided pointer reads through it to access the data. But that said, I guess the solution might more reasonably be to just make an infallible version of
to_vecthat does that misalignment and non-contiguity handling itself? In that model, we could revert the error back to an opaque "array is either misaligned or non-contiguous" message without needing to expose the complexity to users.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.
Makes sense.
That sounds like a nice idea. Maybe that could be
to_cow, returning a borrowed slice if possible or an owned copy if not.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.
Ok, that sounds like a plan - let me pivot the error back to a single opaque struct, I'll leave the name as
AsSliceErrorand makeNonContiguousErrora#[deprecated]type alias to it. I can open a new PR forPyArray::to_cow.