Skip to content

Commit 967b3a0

Browse files
authored
[derive] Document trivial_is_bit_valid (#1905)
Explain why we only support concrete types so that future authors won't spuriously add support for them.
1 parent d9e48e3 commit 967b3a0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

zerocopy-derive/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,22 @@ fn derive_try_from_bytes_enum(
551551
/// Attempts to generate a `TryFromBytes::is_bit_valid` instance that
552552
/// unconditionally returns true.
553553
///
554+
/// This is possible when the `top_level` trait is `FromBytes` and there are no
555+
/// generic type parameters. In this case, we know that compilation will succeed
556+
/// only if the type is unconditionally `FromBytes`. Type parameters are not
557+
/// supported because a type with type parameters could be `TryFromBytes` but
558+
/// not `FromBytes` depending on its type parameters, and so deriving a trivial
559+
/// `is_bit_valid` would be either unsound or, assuming we add a defensive
560+
/// `Self: FromBytes` bound (as we currently do), overly restrictive. Consider,
561+
/// for example, that `Foo<bool>` ought to be `TryFromBytes` but not `FromBytes`
562+
/// in this example:
563+
///
564+
/// ```rust,ignore
565+
/// #[derive(FromBytes)]
566+
/// #[repr(transparent)]
567+
/// struct Foo<T>(T);
568+
/// ```
569+
///
554570
/// This should be used where possible. Using this impl is faster to codegen,
555571
/// faster to compile, and is friendlier on the optimizer.
556572
fn try_gen_trivial_is_bit_valid(

0 commit comments

Comments
 (0)