2023-10-26 22:46:27 +00:00
|
|
|
#![doc(hidden)]
|
|
|
|
|
2021-08-02 09:42:48 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
pub enum Suspendable<T> {
|
|
|
|
Active(T),
|
|
|
|
Suspended,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Suspendable<T> {
|
|
|
|
pub fn new(t: T) -> Self {
|
|
|
|
Suspendable::Active(t)
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:07:01 +00:00
|
|
|
#[inline(always)]
|
2021-08-02 09:42:48 +00:00
|
|
|
pub fn suspend(&mut self) -> Self {
|
|
|
|
match self {
|
|
|
|
Suspendable::Active(_) => std::mem::replace(self, Suspendable::Suspended),
|
2023-07-21 16:15:30 +00:00
|
|
|
Suspendable::Suspended => panic!("Attempt to suspend suspended Suspendable"),
|
2021-08-02 09:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:07:01 +00:00
|
|
|
#[inline(always)]
|
2021-08-02 09:42:48 +00:00
|
|
|
pub fn resume(&mut self, other: Self) {
|
|
|
|
match self {
|
2023-07-21 16:15:30 +00:00
|
|
|
Suspendable::Suspended => match other {
|
|
|
|
Suspendable::Active(_) => *self = other,
|
|
|
|
Suspendable::Suspended => panic!("Attempt to resume from suspended Suspendable"),
|
|
|
|
},
|
|
|
|
Suspendable::Active(_) => panic!("Attempt to resume non-suspended Suspendable"),
|
2021-08-02 09:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:07:01 +00:00
|
|
|
#[inline(always)]
|
2021-08-02 09:42:48 +00:00
|
|
|
pub fn take(self) -> T {
|
|
|
|
match self {
|
|
|
|
Suspendable::Active(t) => t,
|
2023-07-21 16:15:30 +00:00
|
|
|
Suspendable::Suspended => panic!("Attempt to take from suspended Suspendable"),
|
2021-08-02 09:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Suspendable<T> {
|
|
|
|
type Target = T;
|
2021-09-30 11:07:01 +00:00
|
|
|
#[inline(always)]
|
2021-08-02 09:42:48 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match self {
|
|
|
|
Suspendable::Suspended => panic!("Suspended Suspendable at deref"),
|
2023-07-21 16:15:30 +00:00
|
|
|
Suspendable::Active(t) => t,
|
2021-08-02 09:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DerefMut for Suspendable<T> {
|
2021-09-30 11:07:01 +00:00
|
|
|
#[inline(always)]
|
2021-08-02 09:42:48 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
match self {
|
|
|
|
Suspendable::Suspended => panic!("Empty Suspendable at deref_mut"),
|
2023-07-21 16:15:30 +00:00
|
|
|
Suspendable::Active(t) => t,
|
2021-08-02 09:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|