Lets say I have a factory of objects:
struct Object;
struct Factory;
impl Object {
pub fn new() -> Object { Object{} }
}
impl Factory {
pub fn new() -> Factory { Factory{} }
pub fn create() -> Object { Object::new() }
}
How can you ensure that objects created by the factory do no outlive the factory? In other words, how to make code like this not compile:
let factory = Factory::new();
let object = factory.create();
drop(factory); // Some kind of lifetime compile error
Feel free to use any wrappers/containers to keep track of everything, as long as the compiler knows that something's up. I have tried returning a lot of different things myself, including &RefCell<Object>, but I always run into a wall and end up starting over.