Stream: general

Topic: why does Any need `: 'static`?


Jake Goulding (Oct 16 2019 at 00:58, on Zulip):

I know this is a restriction, but I've never actually thought about why. Is it because Concrete<'a> and Concrete<'static> become the same trait object, and if we allowed downcasting Concrete<'a> through TraitObject + 'a to Concrete<'static> we'd have memory unsafety?

nagisa (Oct 16 2019 at 01:40, on Zulip):

Yes, any does not have any lifetimes for data contained behind that trait object, so this data must be 'static for any conversion to Any to be well-formed.

nagisa (Oct 16 2019 at 01:41, on Zulip):

I find it easier to reason about this with closures that get passed to threads. They do similar kind of type erasure and it is easier to reason about why the requirement like that ends up being present (as threads may outlive the parent scope)

centril (Oct 16 2019 at 07:34, on Zulip):

this question gets raised every now and then so I think it would be good to add some docs about it to Any

csmoe (Oct 16 2019 at 12:55, on Zulip):

this question gets raised every now and then so I think it would be good to add some docs about it to Any

@centril and downcast stuff with Box<dyn Any> https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object , it behaves unexpected for new comers.

Last update: Nov 20 2019 at 11:30UTC