Struct SBState

Source
pub struct SBState { /* private fields */ }

Implementations§

Source§

impl SBState

Source

pub fn new(config: SBStateConfig) -> Self

Examples found in repository?
examples/minimal.rs (lines 6-9)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // When this handle drops the task completes
12    let _handle = state.add_task(format!("Some super basic task"), Status::Started);
13
14    std::thread::sleep(Duration::from_secs(10));
15}
More examples
Hide additional examples
examples/demo.rs (lines 6-9)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}
Source

pub fn error<S: ToString>(&self, display_name: S)

Source

pub fn info<S: ToString>(&self, display_name: S)

Examples found in repository?
examples/demo.rs (line 57)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}
Source

pub fn add_task<S: ToString>(&self, display_name: S, status: Status) -> TaskId

Examples found in repository?
examples/minimal.rs (line 12)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // When this handle drops the task completes
12    let _handle = state.add_task(format!("Some super basic task"), Status::Started);
13
14    std::thread::sleep(Duration::from_secs(10));
15}
More examples
Hide additional examples
examples/demo.rs (line 15)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}
Source

pub fn set_task_display_name(&self, task_id: &TaskId, display_name: String)

Source

pub fn delete_task(&self, task_id: &TaskId)

Source

pub fn update_task(&self, task_id: &TaskId, new_status: Status)

Examples found in repository?
examples/demo.rs (line 41)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}
Source

pub fn add_subtask(&self, task_id: &TaskId, status: Status) -> TaskId

Examples found in repository?
examples/demo.rs (line 16)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}
Source

pub fn update_subtask( &self, task_id: &TaskId, sub_task_id: &TaskId, status: Status, )

Examples found in repository?
examples/demo.rs (line 38)
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}

Trait Implementations§

Source§

impl Clone for SBState

Source§

fn clone(&self) -> SBState

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SBState

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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.