Skip to content

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented May 2, 2024

The first return value is unchanged - it is the target of the cast. The second return value replaces the previous split_at: usize, and replaces it with a pointer to the prefix or suffix bytes as a Ptr<[u8]>.

This permits us to replace uses of Ref with direct uses of Ptr in some places.

Copy link
Member Author

@joshlf joshlf left a comment

Choose a reason for hiding this comment

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

Done.

TODO: Make @jswrenn a co-author; this is refactored from his work in #1072.

@joshlf joshlf force-pushed the ptr-split branch 4 times, most recently from 4be4755 to 43761b0 Compare May 2, 2024 02:41
@joshlf joshlf force-pushed the ptr-split branch 8 times, most recently from 18e3b66 to 541396d Compare May 7, 2024 04:31
@joshlf joshlf marked this pull request as ready for review May 7, 2024 04:32
@joshlf joshlf requested a review from jswrenn May 7, 2024 04:32
// `ptr` is non-null.
let ptr = unsafe { NonNull::new_unchecked(ptr) };

// SAFETY: TODO(#896)
Copy link
Member Author

Choose a reason for hiding this comment

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

NOTE: Leaving this undone, but it will block 0.8.

src/lib.rs Outdated
Comment on lines 4914 to 4917
// PANICS: This is guaranteed not to underflow (and thus panic) since
// `remainder` addresses subset of `bytes`, and thus can't be longer
// than `bytes`.
let split_at = bytes.len().checked_sub(remainder.len()).expect("zerocopy internal error: `bytes.len().checked_sub(remainder.len())` should not underflow");
Copy link
Member Author

Choose a reason for hiding this comment

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

Heads up about this .expect(). If we go the "always return an error instead of panicking" route, even for errors we know are unreachable, then it's not clear to me what error to construct here (it was easier when we were returning Options). I could also be convinced to do unchecked_sub.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since it's unreachable, it doesn't matter what error we'd return (though I'd argue that a SizeError is most appropriate). But let's just do an unchecked_sub here!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

Copy link
Collaborator

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

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

Giving this an approval, but there's one suggestion in my review.

src/lib.rs Outdated
Comment on lines 4914 to 4917
// PANICS: This is guaranteed not to underflow (and thus panic) since
// `remainder` addresses subset of `bytes`, and thus can't be longer
// than `bytes`.
let split_at = bytes.len().checked_sub(remainder.len()).expect("zerocopy internal error: `bytes.len().checked_sub(remainder.len())` should not underflow");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since it's unreachable, it doesn't matter what error we'd return (though I'd argue that a SizeError is most appropriate). But let's just do an unchecked_sub here!

@joshlf joshlf enabled auto-merge May 7, 2024 13:48
The first return value is unchanged - it is the target of the cast. The
second return value replaces the previous `split_at: usize`, and
replaces it with a pointer to the prefix or suffix bytes as a
`Ptr<[u8]>`.

This permits us to replace uses of `Ref` with direct uses of `Ptr` in
some places.

Co-authored-by: Jack Wrenn <[email protected]>
@joshlf joshlf added this pull request to the merge queue May 7, 2024
Merged via the queue into main with commit 4ea4c17 May 7, 2024
@joshlf joshlf deleted the ptr-split branch May 7, 2024 14:15
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.

2 participants