-
I thought this would work: use pyo3::prelude::*;
use pyo3::types::PyBytes;
fn bytes_from_py(bytes: &Bound<'_, PyBytes>) -> Vec<u8> {
bytes.as_bytes().to_vec()
}
#[pyfunction]
fn f(#[pyo3(from_py_with = "bytes_from_py")] bytes: Vec<u8>) {} However, I end up with a non-primitve cast error:
|
Beta Was this translation helpful? Give feedback.
Answered by
Icxolu
Dec 2, 2024
Replies: 1 comment 2 replies
-
The problem here is the signature of your fn bytes_from_py(bytes: &Bound<'_, PyAny>) -> PyResult<Vec<u8>> {
Ok(bytes.downcast::<PyBytes>()?.as_bytes().to_vec())
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
rdong8
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem here is the signature of your
bytes_from_py
. To be usable withfrom_py_with
it has to have to following signature:fn(&Bound<'_, PyAny>) -> PyResult<T>
. Something like this should work: