google / google/zerocopy

Add `Initialize`/`Inhabited` (`TryFromBytes` super-trait)

Open
#2,748 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

A number of desired features require knowledge of the recursive layout of a type down to individual fields. `KnownLayout` is not recursive (e.g. you can derive `KnownLayout` on a sized type whose fields are not `KnownLayout`), and so we cannot use `KnownLayout` without a semver breaking change. Besides, `KnownLayout` is philosophically the wrong place for these features.

These features include:
- In-place initialization (#2749)
- Field projection (#196)
- Zeroing padding (#494)
- Access to a value's initialized prefix (#2668)

Instead, we should add a super-trait to `TryFromBytes` which has a recursive requirement (just like `TryFromBytes`) but which is not as heavy-weight as `TryFromBytes` (which requires deriving an entire validator) and does not expose the ability to _construct_ a type to users outside of a module (which is something that an author may wish to avoid). Naming TBD (`Initialize` and `Inhabited` were both proposed, but other names may be better).

## Open questions

- Could we migrate any functionality from `KnownLayout`, thus unlocking this functionality a) as a dependency of this new trait or, b) for types which `KnownLayout` can't support?

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.