Skip to content
This repository has been archived by the owner on Sep 13, 2018. It is now read-only.

Impl a service if &mut self is needed #24

Closed
flosse opened this issue Aug 25, 2017 · 2 comments
Closed

Impl a service if &mut self is needed #24

flosse opened this issue Aug 25, 2017 · 2 comments

Comments

@flosse
Copy link

flosse commented Aug 25, 2017

Is there a reason for why the Service trait does not use a mutable reference to self?

fn call(&self, req: Self::Request) -> Self::Future;

Let's say I'd like to modify a client struct:

struct Client {
    transaction_id : u16
}

impl Service for Client {
    // type Request = ...
    // ...
    fn call(&self, req: Self::Request) -> Self::Future {
        self.transaction_id += 1; // <---- this does not work of course :(
        // ...
    }
}
@carllerche
Copy link
Member

#9 (comment)

Services are intended to be pure functions of request -> future of a response. If you want to track some state, you should use some sort of cell managing internal mutability. Service values are also intended to be Sync... (or at least will work better if they are).

@flosse
Copy link
Author

flosse commented Sep 1, 2017

Thank you for that clarification!

@flosse flosse closed this as completed Sep 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants