Add Builder::with for updating messages

Allow creating a new message based on an existing one, overriding
specific fields while copying the remaining original fields.
This commit is contained in:
2026-05-04 20:11:54 -07:00
parent 05e4c275bb
commit 04ef952a58
5 changed files with 1723 additions and 13 deletions
+17
View File
@@ -95,6 +95,23 @@ fn build_proto(buf: &mut [u8]) -> roto::Result<&[u8]> {
Builder methods consume `self` and return `Result<Self>`, enabling `?`-based chaining.
`finish()` returns `Result<&'b mut [u8]>` — a slice of the portion of the buffer that was written.
### Updating messages
You can read a message, modify specific fields, and use `.with()` to copy the remaining fields from the original binary.
```rust
fn update_proto(data: &[u8], buf: &mut [u8]) -> roto::Result<&[u8]> {
let msg = Message::new(data)?;
let mut builder = MessageBuilder::builder(buf);
if msg.foo()? == "bar" {
builder = builder.foo("foosbar")?;
}
builder.with(&msg)?.finish()
}
```
### Repeated fields
Repeated fields return a `RepeatedFieldIterator<'a>`. Each item yields `Result<(&[u8], WireType)>`.