Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. use std::pin::Pin;
  2.  
  3. trait TimTrait {
  4. fn hello(&mut self) -> u32;
  5. }
  6.  
  7. struct Tim {
  8. member: u32,
  9. }
  10.  
  11. impl TimTrait for Tim {
  12. fn hello(&mut self) -> u32 {
  13. self.member += 1;
  14. println!("Hello {}", self.member);
  15. self.member
  16. }
  17. }
  18.  
  19. struct Timer<TIM> {
  20. tim: TIM
  21. }
  22.  
  23. trait MyFuture {
  24. fn poll(self: Pin<&mut Self>) -> u32;
  25. }
  26.  
  27. struct TimerFuture<'a, TIM> {
  28. tim: &'a mut TIM,
  29. }
  30.  
  31. trait TimerTrait<'a> {
  32. type FutureType: 'a;
  33.  
  34. fn magic(&'a mut self) -> Self::FutureType;
  35. }
  36.  
  37. impl<'a, TIM> TimerTrait<'a> for Timer<TIM> where TIM: TimTrait, TIM: 'a {
  38. type FutureType = TimerFuture<'a, TIM>;
  39.  
  40. fn magic(&'a mut self) -> Self::FutureType {
  41. TimerFuture {
  42. tim: &mut self.tim
  43. }
  44. }
  45. }
  46.  
  47. impl<'a, TIM> MyFuture for TimerFuture<'a, TIM> where TIM: TimTrait {
  48. fn poll(mut self: Pin<&mut Self>) -> u32 {
  49. self.tim.hello()
  50. }
  51. }
  52.  
  53. fn main() {
  54. let tim = Tim { member: 24 };
  55. let mut timer = Timer {
  56. tim
  57. };
  58.  
  59. let mut foo = timer.magic();
  60. let res1 = Pin::new(&mut foo).poll();
  61.  
  62. let mut foo2 = timer.magic();
  63. let res2 = Pin::new(&mut foo2).poll();
  64.  
  65. println!("{} {}", res1, res2);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement