Skip to content

Commit

Permalink
feat: allow creating ngx_str_t from a byte slice
Browse files Browse the repository at this point in the history
There's no guarantee we'll be working with a valid UTF-8 data.
  • Loading branch information
bavshin-f5 committed Oct 23, 2024
1 parent 8fac2d5 commit e91556a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ mod bindings {
#[doc(no_inline)]
pub use bindings::*;

/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
///
/// # Safety
///
/// The caller must provide a valid pointer to the memory pool.
pub unsafe fn bytes_to_uchar(pool: *mut ngx_pool_t, data: &[u8]) -> Option<*mut u_char> {
let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _;
if ptr.is_null() {
return None;
}
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
Some(ptr)
}

/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
///
/// # Arguments
Expand All @@ -70,6 +84,7 @@ pub use bindings::*;
/// ```
pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _;
debug_assert!(!ptr.is_null());
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
ptr
}
Expand Down Expand Up @@ -121,6 +136,15 @@ impl ngx_str_t {
std::str::from_utf8(self.as_bytes()).unwrap()
}

/// Create an `ngx_str_t` instance from a byte slice.
///
/// # Safety
///
/// The caller must provide a valid pointer to a memory pool.
pub unsafe fn from_bytes(pool: *mut ngx_pool_t, src: &[u8]) -> Option<Self> {
bytes_to_uchar(pool, src).map(|data| Self { data, len: src.len() })
}

/// Create an `ngx_str_t` instance from a `String`.
///
/// # Arguments
Expand Down

0 comments on commit e91556a

Please sign in to comment.