-
I'm trying to return a PyBytes object with the new Bound interface, but the Rust compiler complains about a lifetime problem: fn to_vec(&self, py: Python) -> PyResult<Bound<PyBytes>> {
match self.instance.statistics.to_vec() {
Ok(v) => {
Ok(
PyBytes::new_bound_with(py, v.len(), |b| {
b.copy_from_slice(&v);
Ok(())
})?
)
}
Err(e) => Err(PyException::new_err(e.to_string())),
}
}
The compiler error is:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
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())),
}
} |
Beta Was this translation helpful? Give feedback.
-
The solution with
because it's otherwise inferred to be the one of @davidhewitt are these lifetime inference problems new with Bound? Do/Should we have a chapter in the guide about it? |
Beta Was this translation helpful? Give feedback.
I've found the solution: