I have an object of type
Arc<RwLock<SessionData>>
And I have a method that is supposed to take some kind of reference to SessionData
fn some_method(session: ...)
I'm using Rocket (a web-framework for Rust), and I can't directly invoke the method, because it is invoked by Rocket. However, I can provide it with an implementation that creates an object that will be passed to the handler. It looks a bit like this:
impl<'a, 'r> request::FromRequest<'a, 'r> for SomeType {
type Error = ();
fn from_request(request: &'a request::Request<'r>) -> request::Outcome<Self, Self::Error> {
// return object here
}
}
I want to avoid returning an RwLock directly, because I want the handler to have an already-locked object passed to it. However, I can't return a reference or a RwLockReadGuard, because both of them depend on the RwLock, which would go out of scope.
Instead, I am trying to create some kind of self-sufficient type that would contain an Arc<RwLock<SessionData>>, contain the lock guard to this lock, and deref to a SessionData object.
So far, I have tried some combinations of the following:
- A
Sessionobject that contains anArc<RwLock<SessionData>>and aRwLockReadGuard<SessionData> - An object that contains an
Arc<RwLock<SessionData>>and aRwLockReadGuardRef<SessionData>from the owning-ref library. - An object that would use the
OwnedHandletype from the owning-ref library.
However, I haven't been able to do what I want to do, running into various lifetime borrowing issues and whatnot.
Is it at all possible to create a sort of a self-contained 'Handle'-like object that would contain both the lock and the lock guard to the object that it points to?
This is a similar, but slightly different situation than described in How to return reference to a sub-value of a value that is under a mutex?. In there, the MutexGuardRef internally depends on Mutex, and cannot exist if the Mutex (or MyStruct) goes out of scope. In order to achieve similar behaviour, I'd have to pass a struct that contains my RwLock and then do the locking inside the method. This is fine, but I'm wondering if I can go another step further, and pass a struct that is both independent and serves as a RwLockGuard, avoiding the need to lock manually.
Basically, I want to move the locking of the RwLock from the client to the provider of the value.