google / google/zerocopy

Add a way to construct Size Errors, etc

Open
#3,666 0 comments 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

In a private project, I have functions that return things like `Result>` from using `map_src`. I think the `zerocopy` errors are excellent enough that I want to reuse and forward them. These functions can fail partway through initializing the buffer for which `::try_read_from_bytes` is called on. I have to go through this round about way of generating a size error if it fails and doesn't get enough size:
```
// generate a size error using this trick because alignment errors
// come first
let mut buf: [O; 0] = [];
let Err(e) = O::mut_from_bytes(buf.as_mut_bytes()).map_err(|e| e.map_src(drop)) else {
unreachable!();
};
return Err(e);
```
Which is unnecessarily risky and may have weird edge cases in the future. I would rather be able to generate the size error directly.

Contributor guide

Open the contributing guide

Research direction

Start by reading the TryReadError and zerocopy error APIs involved in map_src, TryFromBytes::try_read_from_bytes, and O::mut_from_bytes. Define what direct construction of size and related errors should expose, then verify callers can replace the zero-length-buffer workaround with an explicit size error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.