design - What is the most idiomatic way to share multiple functionality in rust? -
i'm trying wrap restful gdax api in rust learn language, , have question on design of library.
i have struct restconnector
handle connection rest api , trait endpoint
wrap 1 endpoint of api, looks :
impl restconnector { fn request(&self, end_point: endpoint) { // send request... } } trait endpoint { fn http_request(&self) -> request; }
i have several struct implement trait endpoint
lot of functionality in common. example, there private endpoints, need credentials, , public doesn't need it.
so tried create 2 other traits :
trait privateendpoint { fn route(&self) -> string; fn api_key(&self) -> apikey; } impl<t: privateendpoint> t endpoint { fn http_request(&self) -> request { // create request... } } trait publicendpoint { fn route(&self) -> string; } impl<t: publicendpoint> t endpoint { fn http_request(&self) { // create request... } }
and had compile error because of implementations conflict understood after reading another question.
so i'm wondering how compose different functionality in case in rust?
the best solution found far trait endpoint
:
trait endpoint { fn route(&self) -> string; fn api_key(&self) -> box<apikey>; // none if not needed fn method(&self) -> method; // get, post, delete ... // ... }
what don't solution trait has take account possible cases (for example if there apikey or not?).
thank in advance help!
Comments
Post a Comment