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 _manual_slice constructors #5312

Merged
merged 6 commits into from
Jul 30, 2024
Merged

Conversation

robertbastian
Copy link
Member

We added these because range slicing &foo[x..y] is not available in const. However, it can be worked around with slice::split_at/slice::split_at_checked/slice::split_at_unchecked. Removing this simplifies our API surface as well as the implementation.

Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have done this with unsafe originally, but I think we did it with _manual_slice in order to avoid unsafe. I'll defer to @Manishearth

In other words, I agree with the motivation for this change, but if it involves adding unsafe, I'm not convinced that the tradeoff is worth it

@sffc sffc requested a review from Manishearth July 26, 2024 19:11
Comment on lines +48 to +54
pub const fn split_out_range(slice: &[u8], start: usize, end: usize) -> &[u8] {
assert!(start <= slice.len());
assert!(end <= slice.len());
assert!(start <= end);
// SAFETY: assertions and align = size = 1.
unsafe { core::slice::from_raw_parts(slice.as_ptr().add(start), end - start) }
}
Copy link
Member

@sffc sffc Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split_at_checked is new (and const) in 1.80: rust-lang/rust#85122

I think I prefer holding this until we can use that in MSRV so we don't introduce unsafe code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did this because clients didn't have a safe way to do this at all. I'm assuming the majority of our clients will be on stable, so now the majority of our clients have a safe way to do this. One line more or less unsafe code to make this work isn't the end of the world. Also note that I have a follow-up PR that simplifies the unsafety, but it needs to be benched.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, if this is just about getting rid of _manual_slice in public APIs, then just remove those functions without adding unsafe code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser calls the _manual_slice functions, because it needs to const-slice the input. I'm doing this in one location in the parser, and this is not the only unsafe code in this crate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add at least a comment here saying something like

// TODO(MSRV): Use slice_at_checked in Rust 1.80

@robertbastian robertbastian requested a review from sffc July 26, 2024 21:18
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with doing this if we commit to using the safe API later. It's unsafe code being added to a crate (unfortunate), but it's rather straightforward, and const, and only one crate, so I'm not too bothered.

I would like us to move to split_at_checked when we can, though.

(Do we have an issue to keep track of all the rolling "let's fix this with the next MSRV" changes we keep deciding on? I think I saw one before but I don't know if we still have one)

Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK since @Manishearth approved, but let's record somewhere that we can target this unsafe block after MSRV moves

Comment on lines +48 to +54
pub const fn split_out_range(slice: &[u8], start: usize, end: usize) -> &[u8] {
assert!(start <= slice.len());
assert!(end <= slice.len());
assert!(start <= end);
// SAFETY: assertions and align = size = 1.
unsafe { core::slice::from_raw_parts(slice.as_ptr().add(start), end - start) }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add at least a comment here saying something like

// TODO(MSRV): Use slice_at_checked in Rust 1.80

@robertbastian robertbastian merged commit aff4632 into unicode-org:main Jul 30, 2024
28 checks passed
@robertbastian robertbastian deleted the manual branch July 30, 2024 14:04
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.

3 participants