INNER CODE UNIT · Rust
extract_bytes
RyanCodrai/turbovec · turbovec-python/src/lib.rs:247
fn extract_bytes(name: &str, obj: &Bound<'_, PyAny>) -> PyResult<Vec<u8>> {
if let Ok(b) = obj.cast::<PyBytes>() {
return Ok(b.as_bytes().to_vec());
}
if let Ok(b) = obj.cast::<pyo3::types::PyByteArray>() {
return Ok(b.to_vec());
}
Err(pyo3::exceptions::PyTypeError::new_err(format!(
"{name} must be a bytes-like object (bytes or bytearray), got {}",
type_name(obj),
)))
}
/// Map an `io::Error` from `from_bytes` to Python. Unlike [`load_err`]
/// there is no path to blame, and the input is an in-memory value —
/// so corrupt bytes raise `ValueError` (like other malformed-value
/// arguments) rather than `OSError`.
fn from_bytes_err(e: std::io::Error) -> PyErr {