pub fn new_with<F>(py: Python, len: usize, init: F) -> PyResult<&PyByteArray>
    where
        F: FnOnce(&mut [u8]) -> PyResult<()>,
    {
        unsafe {
            let pyptr =
                ffi::PyByteArray_FromStringAndSize(std::ptr::null(), len as ffi::Py_ssize_t);
            // Check for an allocation error and return it
            let pypybytearray: Py<PyByteArray> = Py::from_owned_ptr_or_err(py, pyptr)?;
            let buffer = ffi::PyByteArray_AsString(pyptr) as *mut u8;
            debug_assert!(!buffer.is_null());
            // Zero-initialise the uninitialised bytearray
            std::ptr::write_bytes(buffer, 0u8, len);
            // (Further) Initialise the bytearray in init
            // If init returns an Err, pypybytearray will automatically deallocate the buffer
            init(std::slice::from_raw_parts_mut(buffer, len)).map(|_| pypybytearray.into_ref(py))
        }
    }
/// Creates a new Python `bytearray` object with an `init` closure to write its contents.
/// Before calling `init` the bytearray is zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return
///   it inside `Err`.
/// * If `init` returns `Err(e)`, `new_with` will return `Err(e)`.
/// * If `init` returns `Ok(())`, `new_with` will return `Ok(&PyByteArray)`.
///
/// # Examples
///
/// ```
/// use pyo3::{prelude::*, types::PyByteArray};
///
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| -> PyResult<()> {
///     let py_bytearray = PyByteArray::new_with(py, 10, |bytes: &mut [u8]| {
///         bytes.copy_from_slice(b"Hello Rust");
///         Ok(())
///     })?;
///     let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() };
///     assert_eq!(bytearray, b"Hello Rust");
///     Ok(())
/// })
/// # }
/// ```
Creates a new Python `bytearray` object with an `init` closure to write its contents.
Before calling `init` the bytearray is zero-initialised.
If Python raises a MemoryError on the allocation, `new_with` will return
it inside `Err`.
Creates a new Python ` bytearray ` object with an ` init ` closure to write its contents . Before calling ` init ` the bytearray is zero - initialised . If Python raises a MemoryError on the allocation ` new_with ` will return it inside ` Err ` .
pub fn new_with<F>(py: Python, len: usize, init: F) -> PyResult<&PyByteArray>
    where
        F: FnOnce(&mut [u8]) -> PyResult<()>,
    {
        unsafe {
            let pyptr =
                ffi::PyByteArray_FromStringAndSize(std::ptr::null(), len as ffi::Py_ssize_t);
            // Check for an allocation error and return it
            let pypybytearray: Py<PyByteArray> = Py::from_owned_ptr_or_err(py, pyptr)?;
            let buffer = ffi::PyByteArray_AsString(pyptr) as *mut u8;
            debug_assert!(!buffer.is_null());
            // Zero-initialise the uninitialised bytearray
            std::ptr::write_bytes(buffer, 0u8, len);
            // (Further) Initialise the bytearray in init
            // If init returns an Err, pypybytearray will automatically deallocate the buffer
            init(std::slice::from_raw_parts_mut(buffer, len)).map(|_| pypybytearray.into_ref(py))
        }
    }
pub fn new_with < F > ( py : Python , len : usize , init : F ) -> PyResult < & PyByteArray > where F : FnOnce ( & mut [ u8 ] ) -> PyResult < ( ) > , { unsafe { let pyptr = ffi :: PyByteArray_FromStringAndSize ( std :: ptr :: null ( ) , len as ffi :: Py_ssize_t ) ; // Check for an allocation error and return it let pypybytearray : Py < PyByteArray > = Py :: from_owned_ptr_or_err ( py , pyptr ) ? ; let buffer = ffi :: PyByteArray_AsString ( pyptr ) as * mut u8 ; debug_assert ! ( buffer is_null ( ) ) ; // Zero-initialise the uninitialised bytearray std :: ptr :: write_bytes ( buffer , 0u8 , len ) ; // (Further) Initialise the bytearray in init // If init returns an Err, pypybytearray will automatically deallocate the buffer init ( std :: slice :: from_raw_parts_mut ( buffer , len ) ) . map ( | _ | pypybytearray . into_ref ( py ) ) } }
Creates a new Python `bytearray` object with an `init` closure to write its contents.
Creates a new Python ` bytearray ` object with an ` init ` closure to write its contents .
py
Python
len
usize
init
F
examples
```
use pyo3::{prelude::*, types::PyByteArray}.
` ` ` use pyo3 :: { prelude :: * types :: PyByteArray } .
fn main() -> pyresult<()> {
:with_gil(|py| -> PyResult<()> {
let py_bytearray = PyByteArray::new_with(py, 10, |bytes: &mut [u8]| {
bytes.copy_from_slice(b"Hello Rust");
Ok(())
})?;
let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() };
assert_eq!(bytearray, b"Hello Rust");
Ok(())
})
: with_gil ( |py| - > PyResult< () > { let py_bytearray = PyByteArray :: new_with ( py 10 |bytes : &mut [ u8 ] | { bytes . copy_from_slice ( b " Hello Rust " ) ; Ok (( )) } ) ? ; let bytearray : & [ u8 ] = unsafe { py_bytearray . as_bytes () } ; assert_eq! ( bytearray b " Hello Rust " ) ; Ok (( )) } )
}
None