btpc_core/create/
progress.rs1use crate::{Error, Result};
2
3#[derive(Clone, Debug, Default)]
5pub struct CancellationToken {
6 cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
7}
8
9impl CancellationToken {
10 #[must_use]
12 pub fn new() -> Self {
13 Self::default()
14 }
15
16 pub fn cancel(&self) {
18 self.cancelled
19 .store(true, std::sync::atomic::Ordering::Release);
20 }
21
22 #[must_use]
24 pub fn is_cancelled(&self) -> bool {
25 self.cancelled.load(std::sync::atomic::Ordering::Acquire)
26 }
27
28 pub(crate) fn check(&self) -> Result<()> {
29 if self.is_cancelled() {
30 Err(Error::cancelled())
31 } else {
32 Ok(())
33 }
34 }
35}
36
37#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub struct HashProgress {
40 pub(super) bytes_hashed: u64,
41 pub(super) total_bytes: u64,
42 pub(super) pieces_hashed: u64,
43}
44
45impl HashProgress {
46 #[must_use]
48 pub const fn new(bytes_hashed: u64, total_bytes: u64, pieces_hashed: u64) -> Self {
49 Self {
50 bytes_hashed,
51 total_bytes,
52 pieces_hashed,
53 }
54 }
55
56 #[must_use]
58 pub const fn bytes_hashed(self) -> u64 {
59 self.bytes_hashed
60 }
61
62 #[must_use]
64 pub const fn total_bytes(self) -> u64 {
65 self.total_bytes
66 }
67
68 #[must_use]
70 pub const fn pieces_hashed(self) -> u64 {
71 self.pieces_hashed
72 }
73}
74
75pub trait ProgressSink: Sync {
77 fn on_progress(&self, progress: HashProgress);
79}
80
81#[derive(Clone, Copy, Debug, Default)]
83pub struct NoProgress;
84
85impl ProgressSink for NoProgress {
86 fn on_progress(&self, _progress: HashProgress) {}
87}