How to store data inside server received from method POST?

The issue is the closure in call borrows a reference to self because it’s trying to assign to the batch field. The closure, however, needs to be 'static (ie has no references to anything other than 'static references); that’s because type Future = Box<Future<Item=Self::Response, Error=Self::Error>>; is actually type Future = Box<Future<Item=Self::Response, Error=Self::Error> + 'static>;

There’s a second problem here, which the compiler didn’t get to yet: call() takes a &self (not &mut self). This will prevent you from modifying the batch field as well.

To solve the &self issue, you’ll need to employ interior mutability. Put the batch into a RefCell: RefCell<Option<Batch>> and use borrow_mut() to get a mutable borrow.

To solve the lifetime issue, further encapsulate the batch into an Rc: Rc<RefCell<Option<Batch>>>. Then move a clone of the Rc into the closure. The code would then look roughly like:

let batch_clone = self.batch.clone();
...
let send = req.body().concat2().map(move |b| {
...
*batch_clone.borrow_mut() = Some(batch.clone());
...
}
1 Like