Borrowing as Any non-'static

⚓ Rust    📅 2025-07-08    👤 surdeus    👁️ 2      

surdeus

I am doing something quite straightforward. Just trying to get a ref to a struct after it was passed around as a trait in a Box. The usual help on as_any does not apply, because the lifetimes are not static. According to the searches I have made, one should first convert to Any, with some borrowing and then downcast. This almost works, but Rust gets insistent about 'static-ness.

One solution would be the following function. But how do I write it? Rust error suggestions are circular, as often is the case. (The lifetime on the reference, 'a, is perhaps not needed, but everything is annotated for debugging.)

pub fn z_to_a<'a, 'b>(input: &'a mut (dyn SomeTrait + 'b)) -> &'a mut (dyn Any + 'b)
where
   'a: 'b,
{
   // Implementation here.
}

3 posts - 3 participants

Read full topic

🏷️ rust_feed