天天看點

003Rust異步程式設計,Future trait介紹

Future介紹

Future是Rust異步程式設計的核心,Rust異步程式設計基本都是圍繞Future來展開。那麼,什麼是Future呢? 首先,我們來看下簡化版的Future,如下:

trait SimpleFuture {
    type Output;
    fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
}

enum Poll<T> {
    Ready(T),
    Pending,
}      
  • executor Future的執行者,Future是具有的惰性的,并不會自己的執行,是以需要有一個執行者(executor)來執行Future。
  • Poll枚舉類型

表示Future的兩個狀态:Ready為完成狀态,Pengding為未完成狀态。

  • poll函數

executor通過調用poll函數可以推進Future的狀态。調用poll時,如果完成,傳回Ready狀态;如果未完成則傳回pengding狀态。當wake函數被調用後,會通知executor再次調用poll函數。

  • wake函數

當Future變成Ready狀态後,wake函數會被調用,executor就知道哪個Future已經準備好了,然後調用poll函數。

使用SimpleFuture

use std::thread;
use std::time::Duration;

enum Poll<T> {
    Ready(T),
    Pending,
}

trait SimpleFuture {
    type Output;
    //fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
    fn poll(&mut self, wake: u32) -> Poll<Self::Output>;
}

static mut FINISHED: bool = false;

struct MySleeper {
    polls: u64,
    wake: u32,
}

impl MySleeper {
    fn new() -> Self {
        MySleeper {
            polls: 0,
            wake: 0,
        }
    }
}

impl SimpleFuture for MySleeper {
    type Output = ();
    fn poll(&mut self, wake: u32) -> Poll<()> {
        unsafe {
        if FINISHED {
            Poll::Ready(())
        } else {
            self.wake = wake;
            self.polls += 1;
            println!("not ready yet --> {}", self.polls);
            Poll::Pending
        }
        }
    }
}
struct MyReactor {
    wake: u32,
    handle: Option<thread::JoinHandle<()>>,
}

impl MyReactor {
    fn new() -> MyReactor {
        MyReactor {
            wake: 0,
            handle: None,
        }
    }

    fn add_wake(&mut self, wake: u32) {
        self.wake = wake;
    }

    fn check_status(&mut self) {
        if self.handle.is_none() {
            let _wake = self.wake;
            let handle = thread::spawn(|| loop {
                thread::sleep(Duration::from_secs(5));
                {//模拟執行wake函數
                    unsafe {
                        FINISHED = true;
                    }
                }
            });

            self.handle = Some(handle);
        }
    }
}

struct MyExecutor;

impl MyExecutor {
    fn block_on<F: SimpleFuture>(mut my_future: F, wake: u32) {
        loop {
            match my_future.poll(wake) {
                Poll::Ready(_) => { 
                    println!("my future execute ok!");
                    break; 
                },
                Poll::Pending => {
                    unsafe {
                        while !FINISHED {//FINISHED為true表示為喚醒
                            thread::sleep(Duration::from_secs(1));
                        }
                    }
                }
            }
        }
    }
}


fn main() {
    let mut reactor = MyReactor::new();
    let sleeper = MySleeper::new();
    let wake = sleeper.wake;
    reactor.add_wake(wake);
    reactor.check_status();
    MyExecutor::block_on(sleeper, wake);
}      
  • SimpleFuture

簡化版的Future,實際上是一個狀态機。

  • MyExecutor

用來執行SimpleFuture的相關動作。

  • MyReactor

用來檢測條件是否就位,進而通知MyExecutor執行SimpleFuture。

真正的Future trait

真正的Future的定義如下:

trait Future {
    type Output;
    fn poll(
        // Note the change from `&mut self` to `Pin<&mut Self>`:
        self: Pin<&mut Self>,
        // and the change from `wake: fn()` to `cx: &mut Context<'_>`:
        cx: &mut Context<'_>,
    ) -> Poll<Self::Output>;
}      

真正的Future中,poll函數中self的類型變為了Pin<&mut Self>,第二個參數wake: fn()變為 &mut Context<'_>。

第一個改變是因為,通過Pin可以建立不可移動的 Future。不可移動的對象可以在它們的字段之間存儲指針,例如:

struct MyFut { a: i32, ptr_to_a: *const i32 }