impl<'a> FromPyObject<'a> for Message<'_> {
fn extract(obj: &PyAny) -> PyResult<Self> {
let messsge = obj
.downcast_ref::<PyBytes>()
.expect("Expected param to be an array of bytes");
Ok(Message(messsge.as_bytes()))
}
}
Lifetimes don't do anything. They are only annotations that the compiler can use to prove the code is memory safe.
The suggested solution ties the lifetimes of all references together, which informs the compiler that the reference in the return value is dependent upon 'a defined in the struct. The original code makes the lifetimes independent, allowing the compiler to choose the smallest possible lifetime for obj (which is in opposition to the lifetime required by the function signature).
Ahh I think I understand.
So adding that 'a to the extract function tells the compiler that obj must survive for the 'a lifetime, and since my return object contains a reference to obj it inherits the lifetime, and thus, the compiler now knows the lifetime of the return type, and the error goes away?
That is basically correct. The return value doesn't inherit a lifetime implicitly; the function signature explicitly says which lifetime it must be valid for. Which when desugared is PyResult<Message<'a>>