google / google/zerocopy

`ProbablyPortable`: A potential protector against platform peculiarities

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

Description

I propose a safe, derivable `ProbablyPortable` trait, which provides a (very good) best-effort analysis and marker of probable portability. If a type is `ProbablyPortable` it probably:
- has consistent layout across compilations, platforms and toolchains
- does not have an endian-sensitive representation

We will provide base implementations of `ProbablyPortable` for `()`, `bool`, `i8`, `u8`, and zerocopy's endian-aware types. An implementation is also provided for arrays of `ProbablyPortable` elements.

`derive(ProbablyPortable)` can be applied to:
- structs and unions marked `repr(C)` or `repr(transparent)`, whose members are `ProbablyPortable`
- enums with an explicit integer repr, whose members are `ProbablyPortable`

The documentation of `ProbablyPortable` will instruct users that it should *not* be derived on types whose APIs are endian aware in non-portable ways. However, it is not, strictly speaking, a violation of `ProbablyPortable`'s contract to do so — the proposed trait is called `ProbablyPortable`, not `Portable`, because a `derive` cannot guarantee this.

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.