I have got a struct (Adapter
for reference purposes).
struct Adapter {
field: i32
}
It's methods issue async requests using hyper client. Sometimes, I need to update internal fields of the Adapter depending on responses received for the issued by itself requests. It means I need to access self
in one of the closures on futures::then()
. This is not allowed as self may not live long enough, unless the Adapter instance is static. I do not have it static, because it is created after some validation and interaction on start.
I have found another option is to do something like the following:
struct AdapterImpl {
field: i32
}
struct Adapter {
internal: Rc<RefCell<AdapterImpl>>
}
This way I can obtained Rc pointer to the implementation of self
before constructing future chain and move it to the closure, where I do borrow
or borrow_mut
on the RefCell self
object to access or mutate it in async code. I heard that RefCell is a sign of code smell, but I can not think of another way of solving the problem.
Is the approach, I am taking, the best practice? Or am I doing something wrong?