#![allow(deprecated)]
use crate::{Stop, StopReason, StopToken};
#[deprecated(
since = "0.4.5",
note = "use `StopToken`, which checks the same way and is `Clone`"
)]
pub struct BoxedStop(pub(crate) StopToken);
impl BoxedStop {
#[inline]
pub fn new<T: Stop + 'static>(stop: T) -> Self {
Self(StopToken::new(stop))
}
}
impl Stop for BoxedStop {
#[inline(always)]
#[track_caller]
fn check(&self) -> Result<(), StopReason> {
self.0.check()
}
#[inline(always)]
#[track_caller]
fn should_stop(&self) -> bool {
self.0.should_stop()
}
#[inline(always)]
fn may_stop(&self) -> bool {
self.0.may_stop()
}
}
impl core::fmt::Debug for BoxedStop {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("BoxedStop").finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{StopSource, Stopper, Unstoppable};
#[test]
fn boxed_stop_from_unstoppable() {
let stop = BoxedStop::new(Unstoppable);
assert!(!stop.should_stop());
assert!(stop.check().is_ok());
assert!(!stop.may_stop());
}
#[test]
fn boxed_stop_from_stopper() {
let stopper = Stopper::new();
let stop = BoxedStop::new(stopper.clone());
assert!(!stop.should_stop());
stopper.cancel();
assert!(stop.should_stop());
assert_eq!(stop.check(), Err(StopReason::Cancelled));
}
#[test]
fn boxed_stop_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<BoxedStop>();
}
#[test]
fn boxed_stop_debug() {
let stop = BoxedStop::new(Unstoppable);
let debug = alloc::format!("{:?}", stop);
assert!(debug.contains("BoxedStop"));
}
#[test]
fn boxed_stop_avoids_monomorphization() {
fn process(stop: BoxedStop) -> bool {
stop.should_stop()
}
assert!(!process(BoxedStop::new(Unstoppable)));
assert!(!process(BoxedStop::new(StopSource::new())));
assert!(!process(BoxedStop::new(Stopper::new())));
}
#[test]
fn may_stop_delegates_through_boxed() {
assert!(!BoxedStop::new(Unstoppable).may_stop());
assert!(BoxedStop::new(Stopper::new()).may_stop());
}
#[test]
fn unstoppable_no_allocation() {
let stop = BoxedStop::new(Unstoppable);
assert!(!stop.may_stop());
assert!(stop.check().is_ok());
}
}