Skip to content

How to convert python bytes to Rust Vec<u8>? #4748

Answered by Icxolu
rdong8 asked this question in Questions
Discussion options

You must be logged in to vote

The problem here is the signature of your bytes_from_py. To be usable with from_py_with it has to have to following signature: fn(&Bound<'_, PyAny>) -> PyResult<T>. Something like this should work:

fn bytes_from_py(bytes: &Bound<'_, PyAny>) -> PyResult<Vec<u8>> {
    Ok(bytes.downcast::<PyBytes>()?.as_bytes().to_vec())
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@rdong8
Comment options

@Icxolu
Comment options

Icxolu Dec 2, 2024
Collaborator

Answer selected by rdong8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants