SHARE
TWEET

Untitled

a guest Oct 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::marker::PhantomData;
  2.  
  3. pub struct Endpoint<R, M> {
  4.     pub rep: R,
  5.     _back: PhantomData<M>,
  6. }
  7.  
  8. impl<R, M> Endpoint<R, M> {
  9.     pub fn new(rep: R) -> Self {
  10.         Endpoint {
  11.             rep,
  12.             _back: PhantomData,
  13.         }
  14.     }
  15. }
  16.  
  17. trait IntoEndpoint<R> {
  18.     fn into_endpoint<M: ?Sized>(self) -> Endpoint<R, *const M>;
  19. }
  20.  
  21. trait Service<M> where M: Mounter + ?Sized {
  22.     fn mount_service(self, mounter: &mut M);
  23. }
  24.  
  25. impl<R, M> Service<M> for Endpoint<R, *const M> where R: std::fmt::Display, M: Mounter + ?Sized{
  26.     fn mount_service(self, mounter: &mut M) {
  27.         unimplemented!()
  28.     }
  29. }
  30.  
  31. trait Mounter where Self::Back: BackendTrait {
  32.     type Back;
  33.     fn mount_service<S: Service<Self>>(&mut self, service: S) where Self: Sized {
  34.         service.mount_service(self);
  35.     }
  36.    
  37.     fn mount_repository<Repo: Repository>(&mut self, _rep: &Repo) {
  38.         Repo::mount::<Self>(self);
  39.     }
  40. }
  41.  
  42. trait Repository {
  43.     fn mount<M: Mounter + ?Sized>(mounter: &mut M);
  44. }
  45.  
  46. pub trait BackendTrait {}
  47.  
  48. pub struct RocketRetriever;
  49. pub struct RocketMounter;
  50.  
  51. impl BackendTrait for RocketRetriever {}
  52.  
  53. impl Mounter for RocketMounter {
  54.     type Back = RocketRetriever;
  55. }
  56.  
  57. pub struct MyOwnEndpoint { pub val: u32 }
  58. impl IntoEndpoint<u32> for MyOwnEndpoint {
  59.     fn into_endpoint<M: ?Sized>(self) -> Endpoint<u32, *const M> {
  60.        Endpoint::new( self.val )
  61.     }
  62. }
  63.  
  64. pub struct TestRepo;
  65.  
  66. impl Repository for TestRepo {
  67.     fn mount<M: Mounter + ?Sized>(mounter: &mut M) {
  68.         let my_own_endpoint = MyOwnEndpoint { val: 5 };
  69.         let endpoint = my_own_endpoint.into_endpoint::<M>();
  70.         endpoint.mount_service(mounter);
  71.     }
  72. }
  73.  
  74. fn main() {
  75.     let mut rocket_mounter = RocketMounter { };
  76.     let test_repo = TestRepo { };
  77.     rocket_mounter.mount_repository(&test_repo);
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top