Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/core/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::slice;
use core::{ptr, slice};

use crate::ffi::*;

Expand Down Expand Up @@ -67,6 +67,33 @@ pub trait MutableBuffer: Buffer {
let buf = self.as_ngx_buf_mut();
unsafe { slice::from_raw_parts_mut((*buf).pos, self.len()) }
}

/// Returns how many bytes can still be appended.
///
/// A buffer from [`crate::core::Pool::create_buffer`] starts empty, with
/// its whole allocation spare.
fn spare_capacity(&self) -> usize {
let buf = self.as_ngx_buf();
unsafe { usize::wrapping_sub((*buf).end as _, (*buf).last as _) }
}

/// Appends `bytes` to the buffer contents.
///
/// Returns `None` and writes nothing if they do not fit, so a caller that
/// ignores the result cannot end up with a truncated value.
fn append(&mut self, bytes: &[u8]) -> Option<()> {
if bytes.len() > self.spare_capacity() {
return None;
}

let buf = self.as_ngx_buf_mut();
unsafe {
ptr::copy_nonoverlapping(bytes.as_ptr(), (*buf).last, bytes.len());
(*buf).last = (*buf).last.add(bytes.len());
}

Some(())
}
}

/// Wrapper struct for a temporary buffer, providing methods for working with an `ngx_buf_t`.
Expand Down Expand Up @@ -127,3 +154,50 @@ impl Buffer for MemoryBuffer {
self.0
}
}

#[cfg(test)]
mod tests {
use super::*;

/// Builds the shape `ngx_create_temp_buf` produces: `pos == last == start`.
fn temp_buf(storage: &mut [u8], raw: &mut ngx_buf_t) -> TemporaryBuffer {
raw.start = storage.as_mut_ptr();
raw.pos = raw.start;
raw.last = raw.start;
raw.end = unsafe { raw.start.add(storage.len()) };
TemporaryBuffer::from_ngx_buf(raw)
}

#[test]
fn append_fills_a_freshly_created_buffer() {
let mut storage = [0u8; 64];
let mut raw: ngx_buf_t = unsafe { core::mem::zeroed() };
let mut buf = temp_buf(&mut storage, &mut raw);

// A new buffer holds nothing, and all of its allocation is spare.
assert_eq!(buf.len(), 0);
assert_eq!(buf.spare_capacity(), 64);
assert!(buf.as_bytes_mut().is_empty());

assert!(buf.append(b"hello ").is_some());
assert!(buf.append(b"world").is_some());

assert_eq!(buf.as_bytes(), b"hello world");
assert_eq!(buf.len(), 11);
assert_eq!(buf.spare_capacity(), 53);
}

#[test]
fn append_writes_nothing_when_it_does_not_fit() {
let mut storage = [0u8; 4];
let mut raw: ngx_buf_t = unsafe { core::mem::zeroed() };
let mut buf = temp_buf(&mut storage, &mut raw);

assert!(buf.append(b"too long").is_none());
assert_eq!(buf.len(), 0);

// A partial append would have left the buffer holding "too ".
assert!(buf.append(b"ok").is_some());
assert_eq!(buf.as_bytes(), b"ok");
}
}