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:
@@ -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)>`.
|
||||
|
||||
Reference in New Issue
Block a user