-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add PyBytes wrapper type to access bytes from python
- Loading branch information
1 parent
6ddc885
commit f4a6d35
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use pyo3::{prelude::*, ffi, PyResult}; | ||
use std::os::raw::c_int; | ||
|
||
use crate::Bytes; | ||
|
||
#[pyclass(name = "Bytes")] | ||
pub struct PyBytes { | ||
bytes: Bytes | ||
} | ||
|
||
#[pymethods] | ||
impl PyBytes { | ||
unsafe fn __getbuffer__( | ||
slf: PyRefMut<Self>, | ||
view: *mut ffi::Py_buffer, | ||
flags: c_int, | ||
) -> PyResult<()> { | ||
let bytes = slf.bytes.as_slice(); | ||
let ret = ffi::PyBuffer_FillInfo( | ||
view, | ||
slf.as_ptr() as *mut _, | ||
bytes.as_ptr() as *mut _, | ||
bytes.len().try_into().unwrap(), | ||
1, // read only | ||
flags, | ||
); | ||
if ret == -1 { | ||
return Err(PyErr::fetch(slf.py())); | ||
} | ||
Ok(()) | ||
} | ||
} |