代码之家  ›  专栏  ›  技术社区  ›  cogle

当这个闭包似乎丢弃了一个参数时,它是如何满足特征边界的

  •  0
  • cogle  · 技术社区  · 9 月前

    嗨,我正在查看的文档 asnyc_task 请注意,在示例代码中:

    use async_task::Runnable;
    use flume::{Receiver, Sender};
    use std::rc::Rc;
    
    thread_local! {
        // A queue that holds scheduled tasks.
        static QUEUE: (Sender<Runnable>, Receiver<Runnable>) = flume::unbounded();
    }
    
    // Make a non-Send future.
    let msg: Rc<str> = "Hello, world!".into();
    let future = async move {
        println!("{}", msg);
    };
    
    // A function that schedules the task when it gets woken up.
    let s = QUEUE.with(|(s, _)| s.clone());
    let schedule = move |runnable| s.send(runnable).unwrap();
    
    // Create a task with the future and the schedule function.
    let (runnable, task) = async_task::spawn_local(future, schedule);
    

    当涉及到的函数签名时 ::spawn_local(...) 有一个特点 Schedule

    pub fn spawn_local<F, S>(future: F, schedule: S) -> (Runnable, Task<F::Output>)
    where
        F: Future + 'static,
        F::Output: 'static,
        S: Schedule + Send + Sync + 'static,
    

    哪里 日程 具有以下特征:

    pub trait Schedule<M = ()>: Sealed<M> {
        // Required method
        fn schedule(&self, runnable: Runnable<M>, info: ScheduleInfo);
    }
    

    在我看来,以下代码丢失了 info: ScheduleInfo 作为一个论点,所以我想知道这种特质界限是如何得到满足的。

    let schedule = move |runnable| s.send(runnable).unwrap();
    
    1 回复  |  直到 9 月前
        1
  •  1
  •   kmdreko    9 月前

    an implementation 对于 Schedule 为所有人 Fn(Runnable) 看起来是这样的:

    impl<M, F> Schedule<M> for F
    where
        F: Fn(Runnable<M>),
    {
        fn schedule(&self, runnable: Runnable<M>, _: ScheduleInfo) {
            self(runnable)
        }
    }