Skip to content

Commit

Permalink
[derive] Document trivial_is_bit_valid (#1905)
Browse files Browse the repository at this point in the history
Explain why we only support concrete types so that future authors won't
spuriously add support for them.
  • Loading branch information
joshlf authored Oct 14, 2024
1 parent d9e48e3 commit 967b3a0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ fn derive_try_from_bytes_enum(
/// Attempts to generate a `TryFromBytes::is_bit_valid` instance that
/// unconditionally returns true.
///
/// This is possible when the `top_level` trait is `FromBytes` and there are no
/// generic type parameters. In this case, we know that compilation will succeed
/// only if the type is unconditionally `FromBytes`. Type parameters are not
/// supported because a type with type parameters could be `TryFromBytes` but
/// not `FromBytes` depending on its type parameters, and so deriving a trivial
/// `is_bit_valid` would be either unsound or, assuming we add a defensive
/// `Self: FromBytes` bound (as we currently do), overly restrictive. Consider,
/// for example, that `Foo<bool>` ought to be `TryFromBytes` but not `FromBytes`
/// in this example:
///
/// ```rust,ignore
/// #[derive(FromBytes)]
/// #[repr(transparent)]
/// struct Foo<T>(T);
/// ```
///
/// This should be used where possible. Using this impl is faster to codegen,
/// faster to compile, and is friendlier on the optimizer.
fn try_gen_trivial_is_bit_valid(
Expand Down

0 comments on commit 967b3a0

Please sign in to comment.