Struct Task

Source
pub struct Task { /* private fields */ }
Expand description

A task is a specific tracked operation. It has:

  • A Name
  • A unique ID
  • A creation time
  • The current progress of the task (in “elements”), the ‘counter’.
  • An (optional) maximum number of elements.
  • A time the task was “started”
  • The estimated amount of time the task has remaining
  • A time the task has “ended”
  • A list of any “Child Tasks” that this task can spawn.

Implementations§

Source§

impl Task

Source

pub fn new(id: u64, name: String, max_elements: u64) -> Task

Creates a new finite, named task with the specified ID.

Examples found in repository?
examples/progress.rs (line 12)
8pub fn main() -> Result<(), std::io::Error> {
9    let elements = 1000;
10
11    let prog = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
12    let task = Task::new(0, "Test Task".to_string(), u64::MAX);
13    prog.track_task_progress(&task);
14    task.mark_started();
15    for i in 0..elements {
16        task.mark_one_completed();
17
18        let status = format!("Phase {}", i / 100);
19        task.set_current_status(Some(status));
20
21        std::thread::sleep(std::time::Duration::from_millis(5));
22    }
23
24    Ok(())
25}
Source

pub fn new_infinite(id: u64, name: String) -> Task

Creates a new infinite, named task with a specific ID.

Source

pub fn new_infinite_named(name: String) -> Task

Creates a new infinite, named task with a random ID

Examples found in repository?
examples/progress_random_write.rs (line 19)
15pub fn main() -> Result<(), Error> {
16    let mut rand = Random::default();
17
18    let cons = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
19    let task = Task::new_infinite_named("Writer".to_string());
20    cons.track_task_progress(&task);
21    task.mark_started();
22
23    let out = OpenOptions::new()
24        .write(true)
25        .truncate(true)
26        .create(true)
27        .open("test")?;
28    let out = BufWriter::new(out);
29    let mut out = WriterTask::new(out, task);
30
31    let gb = 1_000_000_000 / 8;
32    for _i in 0..gb {
33        out.write_be_u64(rand.next_u64())?;
34    }
35
36    Ok(())
37}
Source

pub fn new_named(name: String, max_elements: u64) -> Task

Creates a new finite, named task with a random ID.

Source

pub fn current_progress_count(&self) -> u64

Returns the number of elements completed in the range 0..=max_elements

Source

pub fn set_current_progress_count(&self, current_progress: u64)

Updates the current progress counter to be the specified value

Source

pub fn max_elements(&self) -> u64

Returns the maximum number of elements of this task

Source

pub fn set_max_elements(&self, max_elements: u64)

Source

pub fn current_progress_frac(&self) -> f64

Returns the current progress as a fraction in the range 0..=1

Source

pub fn get_id(&self) -> u64

Returns the ID of this task.

Source

pub fn get_name(&self) -> &str

Returns the name of this task

Source

pub fn get_created(&self) -> UnixTimestamp

Returns the time this task was created

Source

pub fn get_started(&self) -> Option<&UnixTimestamp>

Returns the time at which this task started, or None if the task hasn’t started yet.

Source

pub fn mark_one_completed(&self)

Increments the ‘completed’ counter.

Examples found in repository?
examples/progress.rs (line 16)
8pub fn main() -> Result<(), std::io::Error> {
9    let elements = 1000;
10
11    let prog = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
12    let task = Task::new(0, "Test Task".to_string(), u64::MAX);
13    prog.track_task_progress(&task);
14    task.mark_started();
15    for i in 0..elements {
16        task.mark_one_completed();
17
18        let status = format!("Phase {}", i / 100);
19        task.set_current_status(Some(status));
20
21        std::thread::sleep(std::time::Duration::from_millis(5));
22    }
23
24    Ok(())
25}
Source

pub fn mark_all_completed(&self)

Mark this task complete. Does not affect sub-tasks.

Source

pub fn mark_some_completed(&self, completed: u64)

Mark some some portion of this task as completed.

Source

pub fn get_remaining_time(&self) -> Duration

Source

pub fn mark_started(&self)

Marks this task as started. If the task has already started, does nothing.

Examples found in repository?
examples/progress.rs (line 14)
8pub fn main() -> Result<(), std::io::Error> {
9    let elements = 1000;
10
11    let prog = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
12    let task = Task::new(0, "Test Task".to_string(), u64::MAX);
13    prog.track_task_progress(&task);
14    task.mark_started();
15    for i in 0..elements {
16        task.mark_one_completed();
17
18        let status = format!("Phase {}", i / 100);
19        task.set_current_status(Some(status));
20
21        std::thread::sleep(std::time::Duration::from_millis(5));
22    }
23
24    Ok(())
25}
More examples
Hide additional examples
examples/progress_random_write.rs (line 21)
15pub fn main() -> Result<(), Error> {
16    let mut rand = Random::default();
17
18    let cons = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
19    let task = Task::new_infinite_named("Writer".to_string());
20    cons.track_task_progress(&task);
21    task.mark_started();
22
23    let out = OpenOptions::new()
24        .write(true)
25        .truncate(true)
26        .create(true)
27        .open("test")?;
28    let out = BufWriter::new(out);
29    let mut out = WriterTask::new(out, task);
30
31    let gb = 1_000_000_000 / 8;
32    for _i in 0..gb {
33        out.write_be_u64(rand.next_u64())?;
34    }
35
36    Ok(())
37}
Source

pub fn get_ended(&self) -> Option<&UnixTimestamp>

Returns the time at which this task ended, or None if the task hasn’t ended yet.

Source

pub fn mark_ended(&self)

Marks this task as ended. If this task has already ended, does nothing.

Source

pub fn num_children(&self) -> usize

Returns the number of child tasks this task has

Source

pub fn each_child<F: FnMut(&Task)>(&self, func: F)

Iterates over each child task, providing a reference of the child task to the input function

Source

pub fn clean_completed_children(&self) -> Vec<Task>

Source

pub fn new_child_task(&self, id: u64, name: String, max_elements: u64) -> Task

Creates a new child task of this task

Source

pub fn push_new_child_task(&self, task: Task)

Appends this task as a tracked child task.

Source

pub fn is_complete(&self) -> bool

Returns true if this task is complete.

Source

pub fn cancel(&self)

Marks this task as “Cancelled”. Users of this task may opt to ignore this flag, it’s really more like a suggestion.

Source

pub fn is_cancelled(&self) -> bool

Returns true if this task has been marked ‘cancelled’. Cancelling a task is a one-way operation.

Source

pub fn current_status(&self) -> Option<Arc<String>>

Gets a copy of the current status

Source

pub fn set_current_status<T: AsRef<str>>(&self, status: Option<T>)

Sets the optional current status of this task

Examples found in repository?
examples/progress.rs (line 19)
8pub fn main() -> Result<(), std::io::Error> {
9    let elements = 1000;
10
11    let prog = ConsoleProgressPrinter::new_update_rate(Duration::from_millis(100));
12    let task = Task::new(0, "Test Task".to_string(), u64::MAX);
13    prog.track_task_progress(&task);
14    task.mark_started();
15    for i in 0..elements {
16        task.mark_one_completed();
17
18        let status = format!("Phase {}", i / 100);
19        task.set_current_status(Some(status));
20
21        std::thread::sleep(std::time::Duration::from_millis(5));
22    }
23
24    Ok(())
25}

Trait Implementations§

Source§

impl Clone for Task

Source§

fn clone(&self) -> Task

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Task

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Task

§

impl RefUnwindSafe for Task

§

impl Send for Task

§

impl Sync for Task

§

impl Unpin for Task

§

impl UnwindSafe for Task

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> MaybeInto<U> for T
where U: MaybeFrom<T>,

Source§

fn maybe_into(self) -> Option<U>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.