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

Add try_transmute!, try_transmute_{ref,mut}! #359

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Sep 9, 2023

TODO: Body

@joshlf joshlf requested a review from jswrenn September 9, 2023 10:02
src/lib.rs Outdated
Comment on lines 2038 to 2508
// TODO: What's the correct drop behavior on `None`? Does this just
// behave like `mem::forget` in that case?
let m = $crate::__RealManuallyDrop::new(e);
$crate::TryFromBytes::try_read_from($crate::AsBytes::as_bytes(&m))
Copy link
Member Author

@joshlf joshlf Sep 9, 2023

Choose a reason for hiding this comment

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

@jswrenn Thoughts on this TODO? I could see an argument that the most reasonable behavior would be to drop on failure - you always have something that gets dropped, either now or later (if try_transmute! succeeds and you have a U that gets dropped later).

Copy link
Member Author

Choose a reason for hiding this comment

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

Discussed with @jswrenn; he advocates for, on failure, returning e as Result::Error(e).

@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from df988fb to 6c7af14 Compare September 9, 2023 22:10
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from e22c6d0 to b62e2ec Compare September 9, 2023 22:10
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 6c7af14 to d7c16fa Compare September 10, 2023 00:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch 2 times, most recently from 59e8401 to 4891c4f Compare September 10, 2023 06:34
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from d7c16fa to 7a78746 Compare September 10, 2023 19:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 4891c4f to 0f315c7 Compare September 10, 2023 19:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 7a78746 to d1921e2 Compare September 11, 2023 02:59
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch 2 times, most recently from bfb5bf5 to 1b18c53 Compare September 11, 2023 03:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from d1921e2 to 0adefe6 Compare September 11, 2023 19:39
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 1b18c53 to 87754b2 Compare September 11, 2023 19:39
/// assert_eq!(try_transmute_ref!(&0u32.as_bytes()[1..]), None::<&u16>);
/// ```
#[macro_export]
macro_rules! try_transmute_ref {
Copy link
Member Author

Choose a reason for hiding this comment

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

This can - and thus should - validate size equality at compile time.

@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 0adefe6 to 76b3532 Compare September 11, 2023 22:29
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 87754b2 to 2402ec6 Compare September 11, 2023 22:29
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 76b3532 to 5d4515a Compare September 11, 2023 22:52
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch 2 times, most recently from c26336c to b9b29a0 Compare September 11, 2023 23:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 5d4515a to 06a52df Compare September 11, 2023 23:31
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from b9b29a0 to 34016f9 Compare September 11, 2023 23:31
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 06a52df to 68ab76d Compare September 12, 2023 00:34
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 34016f9 to 1a0b1bb Compare September 12, 2023 00:34
@joshlf joshlf changed the base branch from try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes to try-from-bytes-raw-argument-to-is-bit-valid--derive-cleanup September 12, 2023 00:39
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-cleanup branch from 05e5775 to 77ad3c3 Compare September 12, 2023 01:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 1a0b1bb to b8781ee Compare September 12, 2023 01:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-cleanup branch from 77ad3c3 to 0b55a6d Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from b8781ee to dc8e0fc Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-cleanup branch from 0b55a6d to 1eff6f2 Compare September 12, 2023 02:22
Base automatically changed from try-from-bytes-raw-argument-to-is-bit-valid--derive-cleanup to try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes September 12, 2023 15:41
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 02895f3 to 51e8bcc Compare September 12, 2023 15:41
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 43b24a2 to 94130a9 Compare September 12, 2023 15:44
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 51e8bcc to 528d07d Compare September 12, 2023 15:44
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 94130a9 to c79a650 Compare September 12, 2023 16:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 528d07d to 83e66cc Compare September 12, 2023 16:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from c79a650 to 778533a Compare September 19, 2023 02:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 83e66cc to 91c9b2b Compare September 19, 2023 02:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 778533a to 24f0c25 Compare September 19, 2023 16:32
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 91c9b2b to 7c32984 Compare September 19, 2023 16:32
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 24f0c25 to 51cf9f9 Compare September 19, 2023 16:43
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 7c32984 to dde3280 Compare September 19, 2023 16:43
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 51cf9f9 to eb970d2 Compare September 19, 2023 18:46
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from dde3280 to 91057f7 Compare September 19, 2023 18:46
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from eb970d2 to baa448f Compare September 19, 2023 18:57
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 91057f7 to 3008eb5 Compare September 19, 2023 18:58
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from baa448f to 73961ac Compare September 19, 2023 22:33
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 3008eb5 to 1e309ca Compare September 19, 2023 22:33
joshlf and others added 4 commits October 12, 2023 21:41
`Ptr` is like `NonNull`, but has many restrictions which make it so that
using a `Ptr` in unsafe code requires much simpler soundness proofs. In
particular, in a future commit, we will add support for a
`try_cast_into<U>` method where `U: ?Sized + KnownLayout`, which is a
building block of `TryFromBytes`.

Makes progress on #29
`try_cast_into` attempts to cast a `Ptr<[u8]>` into a `Ptr<U>` where `U:
?Sized + KnownLayout`, and will be built upon in future commits as a
building block of `TryFromBytes`.

Because `try_cast_into` performs a runtime check to validate alignment,
this requires disabling Miri's "symbolic alignment check" feature. While
it would be possible to run both with and without that feature in order
to still test other code using symbolic alignment checking, I don't
think that the benefit is worth a) the complexity of passing the
information necessary for certain tests to not run under symbolic
alignment checking and, b) the extra CI time to run Miri tests twice.

Makes progress on #29
`TryFromBytes` can be implemented for types which are not `FromZeroes`
or `FromBytes`; it supports performing a runtime check to determine
whether a given byte sequence contains a valid instance of `Self`.

This is the first step of #5. Future commits will add support for a
custom derive and for implementing `TryFromBytes` on unsized types.

TODO:
- More thorough tests for non-FromBytes types (bool, char, etc)
- Tests that go through the `TryFromBytes` public methods rather than
  testing `is_bit_valid` directly
- Update safety requirements for `is_bit_valid` now that it takes a
  `Ptr` rather than a `NonNull`
- Update SAFETY comments in various places

Makes progress on #5
TODO: Commit message body

TODO:
- In `try_transmute!`, should the argument be dropped or forgotten (ie,
  `mem::forget`) when the transmute fails? We could also return the
  original value in a `Result::Error`, but that would be inconsistent
  with `TryFrom`. Then again, `TryFrom` provides a custom error type, so
  in theory implementers could do that if they wanted to. Most of the
  types that don't consume Copy types.
- Add UI tests

Makes progress on #5
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch from 1e309ca to 82d9322 Compare October 12, 2023 21:42
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 73961ac to df96950 Compare October 12, 2023 21:42
@jswrenn jswrenn force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 2 times, most recently from b69386f to 806a8d7 Compare November 9, 2023 17:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 806a8d7 to e54c8d6 Compare November 17, 2023 18:53
@joshlf
Copy link
Member Author

joshlf commented Sep 10, 2024

Superseded by #1622.

@joshlf joshlf closed this Sep 10, 2024
@joshlf joshlf deleted the try-from-bytes-raw-argument-to-is-bit-valid-3-try-transmute-macros branch September 10, 2024 13:38
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.

1 participant