However, I'm getting just a ton of errors when attempting to manipulate this field. Too many to list here, which indicates that after years of working with Rust, I still don't understand how to make an entity that can have several pointers to it.
What am I doing wrong? To me the above type makes perfect sense:
Optional<>, because it's potentially NULLable.
Rc<>, because it's reference counted.
&'a dyn FooTrait because it's a Trait Object (yes, by intention).
I don't understand what else I could possibly need here? Any elucidation would be very helpful, thanks!
EDIT: I forgot to mention that I've also been searching the web for help on this and trying my best to RTFM, but to no avail. Despite there being a ton of sites that cover things like Rc vs Arc, etc, I'm still not able to find a site that clearly illustrates the simple use-case of having a structure with n pointers to it from different places/scopes.
@jumpnbrownweasel's suggestion is probably what you want. The only thing I'll add is that it's possible you'll get borrow checker errors if your FooTrait implementors are not 'static, in which case you could attempt one of these:
There isn't a day that goes by when Rust doesn't somehow teach me something new. (Or, at least, something that feels new to me.)
I don't normally need to deal with these kinds of types because the default collections are just too convenient. Alas, in this context, I'm writing code that is intended to resemble no_std/alloc-less code, so I'm trying to dig a little deeper than what I normally do. Eventually, I'm hoping I can more easily port this code to an STM32 microcontroller.
I also completely forgot about RefCell as well, which is something else I'll be needing.
Thanks for the replies; I'll try to apply your collective wisdom as soon as my lunch break is over.
It looks like the final type that ended up working for my needs is:
Option<Rc<RefCell<dyn FooTrait>>>
That is a real mouthful; and, simply accessing the field involves more match statements than I'd prefer; but, it finally works, and I'm able to move on with my project.