Skip to content

feat: let a created buffer be filled through the safe API - #329

Open
u5surf wants to merge 1 commit into
nginx:mainfrom
u5surf:feat/buffer-append
Open

u5surf wants to merge 1 commit into
nginx:mainfrom
u5surf:feat/buffer-append

Conversation

@u5surf

@u5surf u5surf commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Closes #327.

Adds two methods to MutableBuffer:

fn spare_capacity(&self) -> usize;
fn append(&mut self, bytes: &[u8]) -> Option<()>;

So a buffer from create_buffer can be filled without leaving the safe API:

let mut buf = request.pool().create_buffer(body.len())?;
buf.append(body)?;
buf.set_last_buf(true);

append writes nothing and returns None when the bytes do not fit, rather
than filling what it can. A caller that ignores the result then gets an empty
buffer instead of a truncated value.

Nothing existing changes behaviour; both methods are new defaults on the trait.
Tests cover a fresh buffer, two appends, and an overflow.

Pool::create_buffer(n) allocates n bytes, but ngx_create_temp_buf leaves
pos == last, so Buffer::len() is 0 and MutableBuffer::as_bytes_mut()
returns an empty slice. There was no safe way to put anything in: neither
trait exposed the capacity or a way to set the length, so filling the
buffer meant dropping to as_ngx_buf_mut() and advancing last by hand.

Add spare_capacity() and append(). Appending more than fits writes
nothing and returns None, rather than truncating, so a caller that
ignores the result cannot produce a partial value.

Fixes nginx#327

Signed-off-by: Y.Horie <u5.horie@gmail.com>
@u5surf
u5surf marked this pull request as ready for review September 15, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pool::create_buffer cannot be filled through the safe API

1 participant