google / google/zerocopy

Clarify when `TryFromBytes` and `IntoBytes` aren't symmetrical

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

Description

The docs for `TryFromBytes` say

> Code should not generally assume that values produced by `IntoBytes` will necessarily be accepted as valid by `TryFromBytes`.

It would be helpful to explain cases where these traits are asymmetrical, so that people could use `TryFromBytes` with confidence. As written, it seems like even trivial code is of unknown correctness, e.g.

```rust
use zerocopy::{IntoBytes, TryFromBytes, Immutable, KnownLayout};

#[derive(Debug, PartialEq, TryFromBytes, IntoBytes, Immutable, KnownLayout)]
struct Foo {
hello: u32,
world: u32,
}

fn main() {
let f = Foo {
hello: 0x1234,
world: 0x5678,
};
let bytes = f.as_bytes();
let f_ = Foo::try_ref_from_bytes(bytes).expect("is this okay to do?");
assert_eq!(&f, f_);
}
```

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.