Skip to main content

set_progress_handler

Function set_progress_handler 

Source
pub fn set_progress_handler(
    handler: impl FnMut(&str, f64) -> ControlFlow<()> + 'static,
) -> ProgressHandlerGuard
Expand description

Installs a Rust closure as the progress handler of the calling thread until the returned guard is dropped.

igraph functions performing long computations (e.g. betweenness and closeness centralities, some layouts, the fast-greedy community detection) periodically call the handler with a short message describing the algorithm and the percentage of the work done (e.g. Graph::betweenness reports "Betweenness centrality: "): the first call has 0.0 and the last one 100.0 (unless an error occurs). Returning ControlFlow::Break aborts the computation, which then fails with ErrorKind::Interrupted. A panic in the closure also aborts the computation, and is resumed when the wrapper returns.

Binds igraph_set_progress_handler.

ยงExamples

use igraph::misc;
use std::{cell::RefCell, ops::ControlFlow, rc::Rc};

let seen = Rc::new(RefCell::new(Vec::new()));
let sink = Rc::clone(&seen);
let guard = misc::set_progress_handler(move |msg, pct| {
    sink.borrow_mut().push((msg.to_owned(), pct));
    ControlFlow::Continue(())
});
misc::progress("Crunching numbers", 50.0)?;
drop(guard);
misc::progress("Nobody listens", 100.0)?; // no handler anymore
assert_eq!(*seen.borrow(), vec![("Crunching numbers".to_owned(), 50.0)]);