pub fn new_with<F>(py: Python, len: usize, init: F) -> &PyBytes
    where
        F: FnOnce(&mut [u8]),
    {
        unsafe {
            let length = len as ffi::Py_ssize_t;
            let pyptr = ffi::PyBytes_FromStringAndSize(std::ptr::null(), length);
            // Iff pyptr is null, py.from_owned_ptr(pyptr) will panic
            let pybytes = py.from_owned_ptr(pyptr);
            let buffer = ffi::PyBytes_AsString(pyptr) as *mut u8;
            debug_assert!(!buffer.is_null());
            // Zero-initialise the uninitialised bytestring
            std::ptr::write_bytes(buffer, 0u8, len);
            // (Further) Initialise the bytestring in init
            init(std::slice::from_raw_parts_mut(buffer, len));
            pybytes
        }
    }
/// Creates a new Python `bytes` object with an `init` closure to write its contents.
/// Before calling `init` the bytes' contents are zero-initialised.
///
/// Panics if out of memory.
///
/// # Example
/// ```
/// use pyo3::{prelude::*, types::PyBytes};
/// Python::with_gil(|py| -> PyResult<()> {
///     let py_bytes = PyBytes::new_with(py, 10, |bytes: &mut [u8]| {
///         bytes.copy_from_slice(b"Hello Rust");
///     });
///     let bytes: &[u8] = FromPyObject::extract(py_bytes)?;
///     assert_eq!(bytes, b"Hello Rust");
///     Ok(())
/// });
/// ```
Creates a new Python `bytes` object with an `init` closure to write its contents.
Before calling `init` the bytes' contents are zero-initialised.

Panics if out of memory.
Creates a new Python ` bytes ` object with an ` init ` closure to write its contents . Before calling ` init ` the bytes ' contents are zero - initialised . Panics if out of memory .
pub fn new_with<F>(py: Python, len: usize, init: F) -> &PyBytes
    where
        F: FnOnce(&mut [u8]),
    {
        unsafe {
            let length = len as ffi::Py_ssize_t;
            let pyptr = ffi::PyBytes_FromStringAndSize(std::ptr::null(), length);
            // Iff pyptr is null, py.from_owned_ptr(pyptr) will panic
            let pybytes = py.from_owned_ptr(pyptr);
            let buffer = ffi::PyBytes_AsString(pyptr) as *mut u8;
            debug_assert!(!buffer.is_null());
            // Zero-initialise the uninitialised bytestring
            std::ptr::write_bytes(buffer, 0u8, len);
            // (Further) Initialise the bytestring in init
            init(std::slice::from_raw_parts_mut(buffer, len));
            pybytes
        }
    }
pub fn new_with < F > ( py : Python , len : usize , init : F ) -> & PyBytes where F : FnOnce ( & mut [ u8 ] ) , { unsafe { let length = len as ffi :: Py_ssize_t ; let pyptr = ffi :: PyBytes_FromStringAndSize ( std :: ptr :: null ( ) , length ) ; // Iff pyptr is null, py.from_owned_ptr(pyptr) will panic let pybytes = py . from_owned_ptr ( pyptr ) ; let buffer = ffi :: PyBytes_AsString ( pyptr ) as * mut u8 ; debug_assert ! ( buffer is_null ( ) ) ; // Zero-initialise the uninitialised bytestring std :: ptr :: write_bytes ( buffer , 0u8 , len ) ; // (Further) Initialise the bytestring in init init ( std :: slice :: from_raw_parts_mut ( buffer , len ) ) ; pybytes } }
Creates a new Python `bytes` object with an `init` closure to write its contents.
Creates a new Python ` bytes ` object with an ` init ` closure to write its contents .
py
Python
len
usize
init
F
py
Python
len
usize
init
F
example
```
use pyo3::{prelude::*, types::PyBytes};
Python::with_gil(|py| -> PyResult<()> {
let py_bytes = PyBytes::new_with(py, 10, |bytes: &mut [u8]| {
bytes.copy_from_slice(b"Hello Rust");
});
let bytes: &[u8] = FromPyObject::extract(py_bytes)?;
assert_eq!(bytes, b"Hello Rust");
Ok(())
});
```
` ` ` use pyo3 :: { prelude :: * types :: PyBytes } ; Python :: with_gil ( |py| - > PyResult< () > { let py_bytes = PyBytes :: new_with ( py 10 |bytes : &mut [ u8 ] | { bytes . copy_from_slice ( b " Hello Rust " ) ; } ) ; let bytes : & [ u8 ] = FromPyObject :: extract ( py_bytes ) ? ; assert_eq! ( bytes b " Hello Rust " ) ; Ok (( )) } ) ; ` ` `