wsi_rs/core/
read_control.rs1use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::Arc;
3
4use crate::WsiError;
5
6#[derive(Debug, Clone, Default)]
8pub struct ReadCancellationToken {
9 cancelled: Arc<AtomicBool>,
10}
11
12impl ReadCancellationToken {
13 #[must_use]
14 pub fn new() -> Self {
15 Self::default()
16 }
17
18 pub fn cancel(&self) {
19 self.cancelled.store(true, Ordering::Release);
20 }
21
22 #[must_use]
23 pub fn is_cancelled(&self) -> bool {
24 self.cancelled.load(Ordering::Acquire)
25 }
26}
27
28#[derive(Debug, Clone, Default)]
30pub struct ReadControl {
31 cancellation: ReadCancellationToken,
32}
33
34impl ReadControl {
35 #[must_use]
36 pub const fn new(cancellation: ReadCancellationToken) -> Self {
37 Self { cancellation }
38 }
39
40 #[must_use]
41 pub fn cancellation(&self) -> &ReadCancellationToken {
42 &self.cancellation
43 }
44
45 pub(crate) fn check_cancelled(&self) -> Result<(), WsiError> {
46 if self.cancellation.is_cancelled() {
47 Err(WsiError::Cancelled)
48 } else {
49 Ok(())
50 }
51 }
52}