Skip to content

How to transfer 'Py<PyAny>' to 'Bytes'? #3878

Closed Answered by davidhewitt
k82cn asked this question in Questions
Discussion options

You must be logged in to vote

At the moment we don't have first-party support for Bytes (as per #1992), but that doesn't stop you from doing this conversion yourself via Bytes constructors.

Also, in general Py<T> is best for storing data and its counterpart &T or Bound<T> (depending on pre/post 0.21) is easiest to work with for manipulating objects.

I would recommend the following:

let any: &PyAny = dump_fn.call(res, None)?; 
let py_bytes: &PyBytes = any.downcast()?;
let bytes: Bytes = Bytes::copy_from_slice(py_bytes.as_bytes());

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@k82cn
Comment options

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