INNER CODE UNIT · Rust
mask_bytes
RyanCodrai/turbovec · turbovec-python/src/lib.rs:109
fn mask_bytes(name: &str, arr: &PyReadonlyArray1<'_, bool>) -> PyResult<Vec<bool>> {
// Same rejection (and message) the previous `as_slice()` produced.
if !arr.is_c_contiguous() {
return Err(not_contiguous_err(name));
}
let n = arr.len();
if n == 0 {
// Skip the pointer entirely: an empty array need not carry a
// dereferenceable one.
return Ok(Vec::new());
}
// SAFETY: `arr` is a live, C-contiguous, 1-D numpy array of `n`
// `bool_` elements, and numpy's `bool_` is one byte wide, so `n`
// bytes from the data pointer lie inside the array's own allocation.
// The GIL is held and the readonly borrow is registered for the
// whole read, so nothing can resize or free the buffer underneath
// it. `*mut bool -> *const u8` casts between two one-byte types
// with the same alignment, and no `bool` reference is ever formed —