wasmer_compiler/
progress.rs1use crate::lib::std::{
4 borrow::Cow,
5 sync::{
6 Arc,
7 atomic::{AtomicU64, Ordering},
8 },
9};
10use wasmer_types::{CompilationProgress, CompilationProgressCallback, CompileError};
11
12#[derive(Clone)]
16pub struct ProgressContext {
17 callback: CompilationProgressCallback,
18 counter: Arc<AtomicU64>,
19 total: u64,
20 phase_name: &'static str,
21}
22
23impl ProgressContext {
24 pub fn new(
26 callback: CompilationProgressCallback,
27 total: u64,
28 phase_name: &'static str,
29 ) -> Self {
30 Self {
31 callback,
32 counter: Arc::new(AtomicU64::new(0)),
33 total,
34 phase_name,
35 }
36 }
37
38 pub fn notify(&self) -> Result<(), CompileError> {
40 let step = self.counter.fetch_add(1, Ordering::SeqCst) + 1;
41 self.callback
42 .notify(CompilationProgress::new(
43 Some(Cow::Borrowed(self.phase_name)),
44 Some(self.total),
45 Some(step),
46 ))
47 .map_err(CompileError::from)
48 }
49}