google / google/zerocopy

`write_to_prefix` generates panic path

Open
#200 7 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

The `write_to_prefix` function is generating a panic path in our code, and I am pretty sure it is the `bytes[..size].copy_from_slice(self.as_bytes());` line. The compiler doesn't have enough information to elide the slice comparison check.

If the code was written like the following, the compiler should not generate a panic path

```rust
fn write_to_prefix(&self, mut bytes: B) -> Option<()> {
let source = self.as_bytes();
if bytes.len() < source.len() {
return None;
}

bytes[..size].copy_from_slice(source);
Some(())
}

```

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.