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

Seprate HeaderSliceWithLength into checked and unchecked variants #108

Merged
merged 6 commits into from
Dec 31, 2024

Conversation

Manishearth
Copy link
Owner

Fixes #107

The breakage is limited to with_mut(), which now returns a HeaderSliceWithLengthChecked which has an immutable header.

Not fully happy with the resultant API, worth cleaning up in the next major release.

@Manishearth
Copy link
Owner Author

r? @steffahn if you have time

Copy link
Contributor

@steffahn steffahn left a comment

Choose a reason for hiding this comment

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

Fixes #107

This addresses only half of #107. Switching out the whole Arcs is still a problem.

The with_mut callback can add a check for this. (Pointer equality, I guess? Actually, I suppose simply unconditionally writing back the new pointer could be okay.)

src/header.rs Outdated Show resolved Hide resolved
src/header.rs Outdated Show resolved Hide resolved
src/thin_arc.rs Outdated
Comment on lines 56 to 59
pub fn with_arc<F, U>(&self, f: F) -> U
where
F: FnOnce(&Arc<HeaderSliceWithLength<H, [T]>>) -> U,
F: FnOnce(&Arc<HeaderSliceWithLengthChecked<H, [T]>>) -> U,
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably only with_arc_mut should change, this is breaking old API.

src/thin_arc.rs Outdated Show resolved Hide resolved
src/thin_arc.rs Outdated
/// Converts a `ThinArc` into an `Arc`. This consumes the `ThinArc`, so the refcount
/// is not modified.
#[inline]
pub fn checked_header_from_thin(a: ThinArc<H, T>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not really happy with the naming, haven’t thought of better alternatives yet.

The problem is that now there’s Arc::from_thin and Arc::checked_header_from_thin, and the latter just sounds more heavy, like there’s additional checking going on, even though the exact opposite is the case (you get something that needs less checking in order to convert back).


Actually, now I have an idea: how about the word becomes “protected”?

E.g. HeaderSliceWithLengthProtected instead of HeaderSliceWithLengthChecked. (And the HeaderSliceWithLengthUnchecked type synonym isn’t needed at all.)

Then we can have Arc::from_thin and Arc::protected_from_thin. That sounds less like additional “checking” is going on.

And we can have ThinArc::with_arc and ThinArc::with_protected_arc (the latter can also serve to be used internally an implementation that replaces the unsafe check-less clone from #76). We could even consider to keep with_arc_mut [with sufficient checks, i.e. under documented threat of abort] and add with_protected_arc_mut next to it, making the naming more consistent.


Or (coming back to naming) another idea is HeaderSliceWithConstLen, and thus Arc::const_len_from_thin, ThinArc::with_const_len_arc and ThinArc::with_const_len_arc_mut.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yeah I'm not happy with the naming either, this is just the naming that's stable. The unchecked variant should be more verbose.

Copy link
Owner Author

@Manishearth Manishearth Dec 25, 2024

Choose a reason for hiding this comment

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

Protected seems fine for now. I don't like the implication of const here with the const naming.

src/thin_arc.rs Outdated Show resolved Hide resolved
@steffahn
Copy link
Contributor

Okay, finally starting another round of review. I’m wondering if with_protected_arc should be made public, I think it might be weird if the “more useful” (with stronger guarantees) protected version is only exposed to users of the mutable API.

@steffahn
Copy link
Contributor

I wonder if HeaderSliceWithLengthProtected should just contain a slice directly, instead of T: ?Sized. I feel the invariant is hard(-ish) to formulate anyways. In the presence of unsizing coercions, a [T; N] in a HeaderSliceWithLengthProtected would also need to have correct length information.

Other types kind-of have no rules… except str is mentioned in the current docs.

Ultimately, HeaderSliceWithLengthProtected is a tool for a sound API of ThinArc and ThinArc doesn't support anything but [T]. Even the str case seems relatively irrelevant, because ThinArc doesn't support it.

@steffahn
Copy link
Contributor

I wonder if HeaderSliceWithLengthProtected should just contain a slice directly, instead of T: ?Sized.

@Manishearth I've tried this out myself, so you don't have to; see #110 ;-)

@steffahn
Copy link
Contributor

Other than the suggestions above, this PR looks good to me.

@Manishearth Manishearth merged commit 6ccaace into master Dec 31, 2024
13 checks passed
@Manishearth Manishearth deleted the headerslice-checked branch December 31, 2024 20:21
@Manishearth
Copy link
Owner Author

Merging this first, I'll get around to your changes soon. The reason I didn't want to make HeaderSliceWithLengthProtected contain a slice directly was that I was worried about the [T; 0] layout changes: AIUI Rust CoerceUnsized guarantees equivalent layout for Foo<[T]> vs Foo<[T; N]> but it does not do that for different types. If your changes can make that part still safe I'm all for it, I don't want the protected type to be overly generic.

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.

ThinArc::with_arc_mut is unsound
2 participants