pub struct BoxedStop(/* private fields */);Expand description
A heap-allocated Stop implementation.
This type provides dynamic dispatch for Stop, avoiding monomorphization
bloat when you don’t need the performance of generics.
§Example
use almost_enough::{BoxedStop, StopSource, Stopper, Unstoppable, Stop};
fn process(stop: BoxedStop) {
for i in 0..1000 {
if i % 100 == 0 && stop.should_stop() {
return;
}
// process...
}
}
// Works with any Stop implementation
process(BoxedStop::new(Unstoppable));
process(BoxedStop::new(StopSource::new()));
process(BoxedStop::new(Stopper::new()));Implementations§
Source§impl BoxedStop
impl BoxedStop
Sourcepub fn new<T: Stop + 'static>(stop: T) -> Self
pub fn new<T: Stop + 'static>(stop: T) -> Self
Create a new boxed stop from any Stop implementation.
Sourcepub fn active_stop(&self) -> Option<&dyn Stop>
pub fn active_stop(&self) -> Option<&dyn Stop>
Returns the effective inner stop if it may stop, collapsing indirection.
The returned &dyn Stop points directly to the concrete type inside
the box, bypassing the BoxedStop wrapper. In a hot loop, subsequent
check() calls go through one vtable dispatch instead of two.
Returns None if the inner stop is a no-op (e.g., Unstoppable).
§Example
use almost_enough::{BoxedStop, Stopper, Unstoppable, Stop, StopReason};
fn hot_loop(stop: &BoxedStop) -> Result<(), StopReason> {
let stop = stop.active_stop(); // Option<&dyn Stop>, collapsed
for i in 0..1000 {
stop.check()?;
}
Ok(())
}
// Unstoppable: returns None, check() is always Ok(())
assert!(hot_loop(&BoxedStop::new(Unstoppable)).is_ok());
// Stopper: returns Some(&Stopper), one vtable dispatch per check()
assert!(hot_loop(&BoxedStop::new(Stopper::new())).is_ok());Trait Implementations§
Auto Trait Implementations§
impl Freeze for BoxedStop
impl !RefUnwindSafe for BoxedStop
impl Send for BoxedStop
impl Sync for BoxedStop
impl Unpin for BoxedStop
impl UnsafeUnpin for BoxedStop
impl !UnwindSafe for BoxedStop
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> StopExt for Twhere
T: Stop,
impl<T> StopExt for Twhere
T: Stop,
Source§impl<T> TimeoutExt for Twhere
T: Stop,
impl<T> TimeoutExt for Twhere
T: Stop,
Source§fn with_timeout(self, duration: Duration) -> WithTimeout<Self>
fn with_timeout(self, duration: Duration) -> WithTimeout<Self>
Add a timeout to this stop. Read more
Source§fn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
fn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
Add an absolute deadline to this stop. Read more