pub trait AsyncTask: 'static + Send {
    type Output: 'static + Send;
    type Affinity: Affinity;

    // Required method
    fn run<'frame, 'life0, 'async_trait>(
        &'life0 mut self,
        frame: AsyncGcFrame<'frame>
    ) -> Pin<Box<dyn Future<Output = JlrsResult<Self::Output>> + 'async_trait>>
       where Self: 'async_trait,
             'frame: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn register<'frame, 'async_trait>(
        _frame: AsyncGcFrame<'frame>
    ) -> Pin<Box<dyn Future<Output = JlrsResult<()>> + 'async_trait>>
       where 'frame: 'async_trait { ... }
}
Expand description

A task that returns once.

In order to schedule the task you must use AsyncJulia::task.

Example:

use jlrs::prelude::*;

struct AdditionTask {
    a: u64,
    b: u32,
}

// Only the runtime thread can call the Julia C API, so the async trait
// methods of `AsyncTask` must not return a future that implements `Send`
// or `Sync`.
#[async_trait(?Send)]
impl AsyncTask for AdditionTask {
    // The type of the result of this task if it succeeds.
    type Output = u64;

    // The thread-affinity of this task. This lets you control whether this kind of task is
    // always dispatched to the main thread, to a worker thread if they're used, or to either.
    // This task can be dispatched to either the main thread or a worker thread.
    type Affinity = DispatchAny;

    async fn run<'base>(&mut self, mut frame: AsyncGcFrame<'base>) -> JlrsResult<Self::Output> {
        let a = Value::new(&mut frame, self.a);
        let b = Value::new(&mut frame, self.b);

        let func = Module::base(&frame).function(&mut frame, "+")?;
        unsafe { func.call_async(&mut frame, [a, b]) }
            .await
            .into_jlrs_result()?
            .unbox::<u64>()
    }
}

Required Associated Types§

source

type Output: 'static + Send

The type of the result which is returned if run completes successfully.

source

type Affinity: Affinity

The thread-affinity of this task. Can be set to DispatchAny, DispatchMain, or DispatchWorker

Required Methods§

source

fn run<'frame, 'life0, 'async_trait>( &'life0 mut self, frame: AsyncGcFrame<'frame> ) -> Pin<Box<dyn Future<Output = JlrsResult<Self::Output>> + 'async_trait>>where Self: 'async_trait, 'frame: 'async_trait, 'life0: 'async_trait,

Run this task.

See the trait docs for an example implementation.

Provided Methods§

source

fn register<'frame, 'async_trait>( _frame: AsyncGcFrame<'frame> ) -> Pin<Box<dyn Future<Output = JlrsResult<()>> + 'async_trait>>where 'frame: 'async_trait,

Register the task.

Note that this method is not called automatically, but only if AsyncJulia::register_task is used. This method can be implemented to take care of everything required to execute the task successfully, like loading packages.

Implementors§