Skip to content

Howto return a PyBytes with the new Bound interface of pyo3 #4059

Closed Answered by brmmm3
brmmm3 asked this question in Questions
Discussion options

You must be logged in to vote

I've found the solution:

fn to_vec(&self, py: Python) -> PyResult<Py<PyBytes>> {
        match self.instance.statistics.to_vec() {
            Ok(v) => {
                Ok(
                    PyBytes::new_bound_with(py, v.len(), |b| {
                        b.copy_from_slice(&v);
                        Ok(())
                    })?.into()
                )
            }
            Err(e) => Err(PyException::new_err(e.to_string())),
        }
    }

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by brmmm3
Comment options

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

@adamreichold
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants