google / google/zerocopy

Support a copying read from `Unalign<T>` where `T: !Copy` but is `FromBytes + IntoBytes`

Open
#2,004 1 comment 0 reactions 0 assignees View on GitHub
customer-request
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

When working with generic code where we don't want to require `Copy` for large structs but is still constructed from unaligned bytes, the [`Ref::read`](https://docs.rs/zerocopy/latest/zerocopy/struct.Ref.html#method.read) method comes in handy. However, this doesn't work when you have a `Ref<&[u8], [Unalign]>`, since there's no builtin way to index a `Ref<_, [T]>`.

It'd be very nice if I could take some `&Unalign` and copy out a `T` where `T: FromBytes + IntoBytes`. This would be infallible shorthand for `T::read_from_bytes(unaligned_val.as_bytes()).unwrap()`. I'm not aware of an infallible way to copy out of an `Unalign where T: !Copy`.

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.