2024-05-31 00:52:12 +00:00
|
|
|
//! Tokens
|
|
|
|
//!
|
|
|
|
//! This module contains tokens which keep track of the validity of certain data.
|
|
|
|
//! Currently, there is 1 token:
|
|
|
|
//! - [`ValidityToken`]
|
|
|
|
//!
|
|
|
|
|
2023-11-05 18:44:41 +00:00
|
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
|
|
|
|
/// A token representing if a piece of data is valid.
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct ValidityToken {
|
|
|
|
token: CancellationToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidityToken {
|
2024-05-31 00:52:12 +00:00
|
|
|
/// Creates a new [`ValidityToken`]
|
2023-11-05 18:44:41 +00:00
|
|
|
pub fn new() -> ValidityToken {
|
|
|
|
ValidityToken {
|
|
|
|
token: CancellationToken::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:52:12 +00:00
|
|
|
/// Returns `true` if the data is still valid.
|
2023-11-05 18:44:41 +00:00
|
|
|
pub fn is_data_valid(&self) -> bool {
|
|
|
|
!self.token.is_cancelled()
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:52:12 +00:00
|
|
|
/// Sets the data to invalid.
|
2023-11-05 18:44:41 +00:00
|
|
|
pub fn set_data_invalid(self) {
|
|
|
|
self.token.cancel()
|
|
|
|
}
|
|
|
|
}
|