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 TryFromBytes trait #353

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Sep 8, 2023

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.

Makes progress on #5

@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from b91a49a to c2b946e Compare September 8, 2023 06:17
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 547c468 to f72320b Compare September 8, 2023 07:38
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from c2b946e to 7dec672 Compare September 8, 2023 07:38
src/lib.rs Outdated Show resolved Hide resolved
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 5 times, most recently from 320bc2c to 879638f Compare September 8, 2023 08:32
@jswrenn jswrenn force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from f72320b to da9e996 Compare September 8, 2023 20:21
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from da9e996 to d8b1ce6 Compare September 8, 2023 22:20
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 879638f to 002bb4a Compare September 8, 2023 22:20
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from d8b1ce6 to 87cd2a8 Compare September 8, 2023 22:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 002bb4a to d43fadf Compare September 8, 2023 22:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 87cd2a8 to 7adbc94 Compare September 8, 2023 22:50
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from d43fadf to 26f08fd Compare September 8, 2023 22:50
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 7adbc94 to af95760 Compare September 8, 2023 23:15
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 26f08fd to 074c632 Compare September 8, 2023 23:15
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from af95760 to 9742ace Compare September 8, 2023 23:28
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 074c632 to 5181ae1 Compare September 8, 2023 23:28
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 9742ace to ed49647 Compare September 8, 2023 23:53
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from 5181ae1 to f30994d Compare September 8, 2023 23:53
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from ed49647 to 90bee71 Compare September 9, 2023 00:06
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 2 times, most recently from 4a6a127 to df988fb Compare September 9, 2023 00:12
@joshlf joshlf mentioned this pull request Sep 9, 2023
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 90bee71 to 0ba3552 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 2 times, most recently from 6c7af14 to d7c16fa Compare September 10, 2023 00:40
src/lib.rs Outdated Show resolved Hide resolved
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 3 times, most recently from 68ab76d to a3235fc Compare September 12, 2023 01:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 80c12cf to 2c9e7f1 Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from a3235fc to 2d0bef3 Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align branch from 2c9e7f1 to bbf9603 Compare September 12, 2023 02:22
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 3 times, most recently from 9c77d7e to f2e9656 Compare September 12, 2023 03:44
Comment on lines +22 to +33
//! - [`TryFromBytes`] supports non-`FromBytes` types by providing the ability
//! to check the validity of a conversion at runtime
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love this wording. @jswrenn any suggestions?

@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch from f2e9656 to 1244aba Compare September 12, 2023 06:16
Comment on lines +1048 to +1608
/// # Panics
///
/// `is_bit_valid` may panic. Callers are responsible for ensuring that any
/// `unsafe` code remains sound even in the face of `is_bit_valid`
/// panicking. (We support user-defined validation routines; so long as
/// these routines are not required to be `unsafe`, there is no way to
/// ensure that these do not generate panics.)
Copy link
Member Author

Choose a reason for hiding this comment

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

cc @jswrenn @djkoloski Calling this out; I've updated all the safety comments in this PR to address panics.

Base automatically changed from try-from-bytes-raw-argument-to-is-bit-valid-2-validate-size-align to main September 12, 2023 12:08
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid-3-try-from-bytes branch 4 times, most recently 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-from-bytes branch 5 times, most recently 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-from-bytes branch from 73961ac to df96950 Compare October 12, 2023 21:42
Comment on lines +1723 to +2148
/// TODO(https://github.com/rust-lang/reference/pull/1401): Use `&u32`
/// instead once it's guaranteed that `align_of::<u32>() ==
/// align_of::<char>()`.
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 has landed; update this PR.

@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
`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
@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 joshlf closed this Feb 12, 2024
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