Skip to main content

bytes_or_return

Macro bytes_or_return 

Source
macro_rules! bytes_or_return {
    ($ptr:expr, $len:expr, $name:expr, $err_val:expr) => { ... };
}
Expand description

Validate and convert raw C byte array to safe slice, returning early on error.

This macro combines null pointer checking with bounds validation using safe_slice_from_raw_parts. It’s specifically for byte arrays (*const c_uchar) passed from C with an associated length.

§Examples

#[no_mangle]
pub unsafe extern "C" fn process_data(
    data: *const c_uchar,
    len: usize
) -> *mut Result {
    let bytes = bytes_or_return_null!(data, len, "data");
    // bytes is now a safe &[u8]
    let result = process(bytes);
    box_tracked!(result)
}