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

[unsafe-fields] Add as_ref_checked #1960

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Oct 21, 2024

Release 0.2.0.

Makes progress on #1931


This PR is on branch unsafe-fields-zerocopy.

@joshlf joshlf requested a review from jswrenn October 21, 2024 20:42
@joshlf joshlf enabled auto-merge October 21, 2024 20:42
@joshlf joshlf force-pushed the I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d branch from 9092745 to 4289ead Compare October 21, 2024 20:42
#[inline(always)]
#[cfg(feature = "zerocopy_0_8")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "zerocopy_0_8")))]
pub const fn as_ref_checked(&self) -> &F
Copy link
Collaborator

Choose a reason for hiding this comment

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

My preference would be as_ref_unchecked for the unsafe method, and make the safe thing the shortest to type (.as_ref). Most types are Immutable, so I additionally expect the safe version will be the common case!

Copy link
Member Author

Choose a reason for hiding this comment

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

Feels weird to have most of the API suffixed with _unchecked, but I agree that that's philosophically the right thing to do.

Done.

@joshlf joshlf force-pushed the I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d branch 3 times, most recently from 12189bf to f1391d2 Compare October 21, 2024 20:57
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.

I don't think we necessarily need to name everything _unchecked. The _unchecked prefix in Rust is conventionally used in Rust when there's a safe option that the method needs to be distinguished from.

When there's no safe alternative, the standard library omits the _unchecked suffix (e.g., contrast get and get_unchecked with slice::align_to).

@@ -253,7 +277,7 @@ impl<O: ?Sized, F, const NAME_HASH: u128> Unsafe<O, F, { NAME_HASH }> {
/// The caller is responsible for upholding any safety invariants associated
/// with this field.
#[inline(always)]
pub const unsafe fn into(self) -> F {
pub const unsafe fn into_unchecked(self) -> F {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be safe, right? It consumes self, so F cannot possibly have any safety invariants with respect to self.

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.

///
/// # Safety
///
/// The caller is responsible for upholding any safety invariants associated
/// with this field.
#[inline(always)]
pub const unsafe fn new(field: F) -> Unsafe<O, F, { NAME_HASH }> {
pub const unsafe fn new_unchecked(field: F) -> Unsafe<O, F, { NAME_HASH }> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm fine just calling this new, since we couldn't possibly offer a safe variant that we'd need to distinguish it from. All cases of initialization are unsafe, without exception.

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.

/// Gets a mutable reference to the inner value.
///
/// # Safety
///
/// The caller is responsible for upholding any safety invariants associated
/// with this field.
#[inline(always)]
pub unsafe fn as_mut(&mut self) -> &mut F {
pub unsafe fn as_mut_unchecked(&mut self) -> &mut F {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same concern as new. I don't think there's a safe version we could possibly offer here that we'd want to distinguish the unsafe version from.

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.

@codecov-commenter
Copy link

codecov-commenter commented Oct 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.46%. Comparing base (f51d666) to head (1f83c62).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1960   +/-   ##
=======================================
  Coverage   89.46%   89.46%           
=======================================
  Files          16       16           
  Lines        5838     5838           
=======================================
  Hits         5223     5223           
  Misses        615      615           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshlf joshlf force-pushed the I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d branch 3 times, most recently from 7835610 to d459581 Compare October 21, 2024 21:15
Release 0.2.0.

Makes progress on #1931

gherrit-pr-id: I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d
@joshlf joshlf force-pushed the I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d branch from d459581 to 1f83c62 Compare October 21, 2024 21:21
@joshlf joshlf requested a review from jswrenn October 21, 2024 21:21
@joshlf joshlf added this pull request to the merge queue Oct 21, 2024
Merged via the queue into main with commit 34f4e6e Oct 21, 2024
69 checks passed
@joshlf joshlf deleted the I7ce0c981ed1f1bc1f4ff85dffef2a74114c6e76d branch October 21, 2024 22:23
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