Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::pin::Pin;
- trait TimTrait {
- fn hello(&mut self) -> u32;
- }
- struct Tim {
- member: u32,
- }
- impl TimTrait for Tim {
- fn hello(&mut self) -> u32 {
- self.member += 1;
- println!("Hello {}", self.member);
- self.member
- }
- }
- struct Timer<TIM> {
- tim: TIM
- }
- trait MyFuture {
- fn poll(self: Pin<&mut Self>) -> u32;
- }
- struct TimerFuture<'a, TIM> {
- tim: &'a mut TIM,
- }
- trait TimerTrait<'a> {
- type FutureType: 'a;
- fn magic(&'a mut self) -> Self::FutureType;
- }
- impl<'a, TIM> TimerTrait<'a> for Timer<TIM> where TIM: TimTrait, TIM: 'a {
- type FutureType = TimerFuture<'a, TIM>;
- fn magic(&'a mut self) -> Self::FutureType {
- TimerFuture {
- tim: &mut self.tim
- }
- }
- }
- impl<'a, TIM> MyFuture for TimerFuture<'a, TIM> where TIM: TimTrait {
- fn poll(mut self: Pin<&mut Self>) -> u32 {
- self.tim.hello()
- }
- }
- fn main() {
- let tim = Tim { member: 24 };
- let mut timer = Timer {
- tim
- };
- let mut foo = timer.magic();
- let res1 = Pin::new(&mut foo).poll();
- let mut foo2 = timer.magic();
- let res2 = Pin::new(&mut foo2).poll();
- println!("{} {}", res1, res2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement