[][src]Trait gwasm_api::ProgressUpdate

pub trait ProgressUpdate {
    fn update(&self, progress: f64);

    fn start(&self) { ... }
fn stop(&self) { ... } }

Trait specifying the required interface for an object tracking the computation's progress

Note that progress is tracked via active polling thus it might be prudent to store the value of current progress in the struct implementing the trait and update it only when the new reported progress value has actually risen (see Example: ProgressBar).

Example: simple tracker

use gwasm_api::ProgressUpdate;

struct SimpleTracker;

impl ProgressUpdate for SimpleTracker {
    fn update(&self, progress: f64) {
        println!("Current progress = {}", progress);
    }
}

Example: ProgressBar

use std::cell::Cell;
use gwasm_api::ProgressUpdate;
use indicatif::ProgressBar;

struct ProgressBarTracker {
    bar: ProgressBar,
    progress: Cell<f64>,
}

impl ProgressBarTracker {
    fn new(num_subtasks: u64) -> Self {
        Self {
            bar: ProgressBar::new(num_subtasks),
            progress: Cell::new(0.0),
        }
    }
}

impl ProgressUpdate for ProgressBarTracker {
    fn update(&self, progress: f64) {
        if progress > self.progress.get() {
            self.progress.set(progress);
            self.bar.inc(1);
        }
    }

    fn start(&self) {
        self.bar.inc(0);
    }

    fn stop(&self) {
        self.bar.finish_and_clear()
    }
}

Required methods

fn update(&self, progress: f64)

Called when progress value was polled from Golem

Loading content...

Provided methods

fn start(&self)

Called when progress updates started

fn stop(&self)

Called when progress updates finished

Loading content...

Implementors

Loading content...