google / google/zerocopy

Support validation-only mode in derives

Open
#1,673 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

Some users want to know whether one of their public types satisfies the layout properties required by `FromBytes`, `IntoBytes`, etc, but do *not* want to expose a trait impl to their users. Currently, the best way to do this is pretty ugly:

```rust
#[derive(FromBytes)]
struct FooInner { ... }

#[repr(transparent)]
pub struct Foo(FooInner);

// Somewhere in `unsafe` code...

// SAFETY: Since `Foo` is a `#[repr(transparent)]` wrapper around `FooInner`, and
// since `FooInner: FromBytes`, we know that...
```

It would be great if we could instead support a validate-only mode for our derives. This would look something like:

```rust
#[derive(FromBytes)]
#[zerocopy(validate-only)]
pub struct Foo { ... }

// Somewhere in `unsafe` code...

// SAFETY: Since `Foo` is validated to be `FromBytes`...
```

Note that this would only be sound for non-generic types. For generic types, our derives don't say in the general case whether a type satisfies the trait's requirements, but instead generate an impl with appropriate bounds. Thus, just because a derive on a generic type compiles successfully doesn't mean that all instantiations of that type satisfy that trait's requirements.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.