use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum AsyncRequestState<T> {
Idle,
Pending { generation: u64 },
Ready { generation: u64, value: T },
Interrupted,
}
impl<T> Default for AsyncRequestState<T> {
fn default() -> Self {
Self::Idle
}
}
impl<T> AsyncRequestState<T> {
pub fn is_pending(&self) -> bool {
matches!(self, Self::Pending { .. })
}
pub fn is_ready(&self) -> bool {
matches!(self, Self::Ready { .. })
}
pub fn generation(&self) -> Option<u64> {
match self {
Self::Idle | Self::Interrupted => None,
Self::Pending { generation } | Self::Ready { generation, .. } => Some(*generation),
}
}
pub fn arm_pending(&mut self, generation: u64) -> Self
where
T: Clone,
{
let prior = self.clone();
*self = Self::Pending { generation };
prior
}
pub fn resolve(&mut self, generation: u64, value: T) -> bool {
match self {
Self::Pending {
generation: pending,
} if *pending == generation => {
*self = Self::Ready { generation, value };
true
},
_ => false,
}
}
pub fn interrupt(&mut self) {
*self = Self::Interrupted;
}
pub fn clear(&mut self) {
*self = Self::Idle;
}
pub fn take(&mut self) -> Option<T> {
if matches!(self, Self::Ready { .. }) {
match std::mem::replace(self, Self::Idle) {
Self::Ready { value, .. } => Some(value),
_ => unreachable!(),
}
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_idle() {
let state: AsyncRequestState<u32> = Default::default();
assert!(matches!(state, AsyncRequestState::Idle));
}
#[test]
fn arm_pending_transitions_from_idle_and_reports_prior() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
let prior = state.arm_pending(7);
assert!(matches!(prior, AsyncRequestState::Idle));
assert!(state.is_pending());
assert_eq!(state.generation(), Some(7));
}
#[test]
fn arm_pending_bumps_generation_and_reports_prior_pending() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(7);
let prior = state.arm_pending(8);
assert!(matches!(
prior,
AsyncRequestState::Pending { generation: 7 }
));
assert_eq!(state.generation(), Some(8));
}
#[test]
fn resolve_accepts_matching_generation() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(3);
let accepted = state.resolve(3, 42);
assert!(accepted);
assert!(state.is_ready());
assert_eq!(state.take(), Some(42));
assert!(matches!(state, AsyncRequestState::Idle));
}
#[test]
fn resolve_rejects_stale_generation() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(1);
state.arm_pending(2);
let accepted = state.resolve(1, 42);
assert!(!accepted);
assert!(state.is_pending());
assert_eq!(state.generation(), Some(2));
}
#[test]
fn resolve_rejects_on_idle() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
let accepted = state.resolve(1, 42);
assert!(!accepted);
assert!(matches!(state, AsyncRequestState::Idle));
}
#[test]
fn interrupt_moves_pending_to_interrupted() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(5);
state.interrupt();
assert!(matches!(state, AsyncRequestState::Interrupted));
assert_eq!(state.generation(), None);
}
#[test]
fn clear_from_any_state_returns_to_idle() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(1);
state.resolve(1, 42);
state.clear();
assert!(matches!(state, AsyncRequestState::Idle));
}
#[test]
fn take_without_ready_leaves_state_unchanged() {
let mut state: AsyncRequestState<u32> = AsyncRequestState::Idle;
state.arm_pending(1);
assert_eq!(state.take(), None);
assert!(state.is_pending());
}
#[test]
fn serde_json_round_trips_each_variant() {
let variants: Vec<AsyncRequestState<String>> = vec![
AsyncRequestState::Idle,
AsyncRequestState::Pending { generation: 42 },
AsyncRequestState::Ready {
generation: 42,
value: "result".to_string(),
},
AsyncRequestState::Interrupted,
];
for state in variants {
let encoded = serde_json::to_string(&state).expect("serialize");
let decoded: AsyncRequestState<String> =
serde_json::from_str(&encoded).expect("deserialize");
assert_eq!(decoded, state);
}
}
#[test]
fn stale_late_result_example_flow() {
let mut state: AsyncRequestState<String> = AsyncRequestState::Idle;
state.arm_pending(10);
assert_eq!(state.generation(), Some(10));
state.arm_pending(11);
let accepted_stale = state.resolve(10, "old".to_string());
assert!(!accepted_stale);
assert_eq!(state.generation(), Some(11));
let accepted_fresh = state.resolve(11, "new".to_string());
assert!(accepted_fresh);
assert_eq!(state.take().as_deref(), Some("new"));
}
}