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] Initial commit #1929

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Oct 17, 2024

Makes progress on #1931


This PR is on branch unsafe-fields.

@codecov-commenter
Copy link

codecov-commenter commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.46%. Comparing base (35d9d4f) to head (e5f23a3).

Files with missing lines Patch % Lines
testutil/src/lib.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1929   +/-   ##
=======================================
  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 If0e198c377137dd941ebd5dc68787766a593e1eb branch from 3ef0d34 to a4d4368 Compare October 17, 2024 19:28
@joshlf joshlf force-pushed the If0e198c377137dd941ebd5dc68787766a593e1eb branch 6 times, most recently from 18fe347 to fff281b Compare October 21, 2024 01:06
@joshlf joshlf requested a review from jswrenn October 21, 2024 01:06
@joshlf joshlf force-pushed the If0e198c377137dd941ebd5dc68787766a593e1eb branch 2 times, most recently from ecfc8ec to 27bf796 Compare October 21, 2024 01:13
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.

A few minor nits.

Comment on lines 183 to 285
use core::mem::ManuallyDrop;

let slf = ManuallyDrop::new(self);

#[repr(C)]
union Transmute<O: ?Sized, F, const NAME_HASH: u128> {
src: ManuallyDrop<Unsafe<O, F, { NAME_HASH }>>,
dst: ManuallyDrop<F>,
}

// SAFETY: `ManuallyDrop<Unsafe<_, F, _>>` has the same size and bit
// validity as `Unsafe<_, F, _>`. [1] `Unsafe<_, F, _>` is
// `#[repr(transparent)]` and has no other fields, and so it has the
// same size and bit validity as `F`.
//
// [1] Per https://doc.rust-lang.org/1.81.0/core/mem/struct.ManuallyDrop.html:
//
// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
// validity as `T`
let dst = unsafe { Transmute { src: slf }.dst };

// SAFETY (satisfaction of `Unsafe`'s field invariant): This method is
// unsafe to call.
ManuallyDrop::into_inner(dst)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you just add a comment explaining why we need to go through all of this ceremony?

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.

Comment on lines 187 to 264
#[repr(C)]
union Transmute<O: ?Sized, F, const NAME_HASH: u128> {
src: ManuallyDrop<Unsafe<O, F, { NAME_HASH }>>,
dst: ManuallyDrop<F>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unless there's good reason to do otherwise, I'd prefer to see this written as:

Suggested change
#[repr(C)]
union Transmute<O: ?Sized, F, const NAME_HASH: u128> {
src: ManuallyDrop<Unsafe<O, F, { NAME_HASH }>>,
dst: ManuallyDrop<F>,
}
#[repr(C)]
union Transmute<Src, Dst> {
src: ManuallyDrop<Src>,
dst: ManuallyDrop<Dst>,
}

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.

Comment on lines +137 to +221
/// Gets a reference to the inner value.
///
/// # Safety
///
/// The caller is responsible for upholding any safety invariants associated
/// with this field.
#[inline(always)]
pub const unsafe fn as_ref(&self) -> &F {
// SAFETY: This method is unsafe to call.
&self.field
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

With Immutable, we could provide as_ref safely. Perhaps — in the interest of leaving that name open for the safe accessor — this should be called something like as_ref_unchecked?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO we should keep this totally separate from zerocopy. We can also easily release breaking changes since this will never be in anyone's API, so I don't think we need to be too concerned about predicting future API changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't zerocopy be an option dependency of this?

// the user is still able to provide a manual impl, so this does not
// fundamentally restrict what behavior can be supported.
impl<O: ?Sized, F: Copy, const NAME_HASH: u128> Copy for Unsafe<O, F, { NAME_HASH }> {}
impl<O: ?Sized, F: Copy, const NAME_HASH: u128> Clone for 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.

Food for thought: With Immutable, I think we could provide basically all of the standard trait impls. No need to take action on this in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack.

@joshlf joshlf force-pushed the If0e198c377137dd941ebd5dc68787766a593e1eb branch from 27bf796 to cf569b2 Compare October 21, 2024 01:51
@joshlf joshlf force-pushed the If0e198c377137dd941ebd5dc68787766a593e1eb branch 3 times, most recently from b57a14b to 667d5db Compare October 21, 2024 02:11
Makes progress on #1931

gherrit-pr-id: If0e198c377137dd941ebd5dc68787766a593e1eb
@joshlf joshlf force-pushed the If0e198c377137dd941ebd5dc68787766a593e1eb branch from 667d5db to e5f23a3 Compare October 21, 2024 04:34
@joshlf joshlf added this pull request to the merge queue Oct 21, 2024
Merged via the queue into main with commit 986e3c5 Oct 21, 2024
66 checks passed
@joshlf joshlf deleted the If0e198c377137dd941ebd5dc68787766a593e1eb branch October 21, 2024 16:34
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