-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Define fn [_]::try_split_at(&self, usize) -> Option<(&Self, &Self)>
#54887
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
src/libcore/slice/mod.rs
Outdated
#[inline] | ||
pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { | ||
if mid > self.len() { None } else { | ||
Some(unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }) |
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.
Add a comment explaining why this is safe.
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.
More importantly, please justify using unsafe at all. While it's possible that something if mid > self.len() { None } else { (&self[..mid], &self[mid..]) }
may end up with redundant bounds checks, in principle they can be optimized out and we should only use unsafe code if that optimization fails to fire.
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 just checked: it indeed elides the bounds checks and emits the same LLVM IR in either case.
src/libcore/slice/mod.rs
Outdated
let ptr = self.as_mut_ptr(); | ||
|
||
if mid > self.len() { None } else { | ||
Some(unsafe { (from_raw_parts_mut(ptr, mid), |
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.
Add a comment explaining why this is safe.
I redefined these functions sans |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
For my edification, can you elaborate why these are better than the caller just checking things? Should there also be |
I just think it's easier to read and harder to misuse in certain use cases — in my experience, for example: parsing certain binary formats, i want to return notice of invalid data if the given slice is too short, but if it is written in terms of I seldom find myself checking bounds before calling |
I personally agree with @scottmcm where I don't think we should add this to the standard library at this time. We don't want to have a dual of all the functionality on slices, both fallible and infallible. Instead we, long ago, decided that we'd only have either infallible or fallible and for slices it's already all related to indexing, which is always infallible, so slices inherited that behavior. Considering to reverse this decision by adding I agree that a local optimum can often be achieved with one extra method, but such a method can always be defined locally in a crate or as a library on crates.io. |
Ping from triage! Thank for your PR, @strake. Based on @alexcrichton's and @scottmcm's comments I'm going to close this PR as blocked on an RFC. Thanks for your contribution! |
I know that this issue is long-closed, however I'd like to add that this would allow the removal of panicking code paths from critical code which may not panic, so I suggest a reconsideration. Without this, one has to rely on unstable optimizations to remove panicking code. |
No description provided.