tokio-rs / tokio-service

The core `Service` trait in Tokio and support
Apache License 2.0
82 stars 19 forks source link

Impl a service if &mut self is needed #24

Closed flosse closed 7 years ago

flosse commented 7 years ago

Is there a reason for why the Service trait does not use a mutable reference to self? https://github.com/tokio-rs/tokio-service/blob/c56afde938abbbccb6db257a49adddf5ad174374/src/lib.rs#L157

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 commented 7 years ago

https://github.com/tokio-rs/tokio-service/issues/9#issuecomment-269105545

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 commented 7 years ago

Thank you for that clarification!