use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll, Waker};
use std::time::{Duration, Instant};
use id_effect::Exit;
use id_effect::{BoxFuture, Effect, Executor, FiberContext, LocalBoxFuture, NotLocal, succeed};
use stats_alloc::{INSTRUMENTED_SYSTEM, Region, StatsAlloc};
#[global_allocator]
static GLOBAL: &StatsAlloc<std::alloc::System> = &INSTRUMENTED_SYSTEM;
struct StubExec;
impl Executor for StubExec {
fn spawn(&self, _: BoxFuture<'static, ()>) {
unreachable!("the pure fast path spawns no tasks")
}
fn spawn_local(&self, _: LocalBoxFuture<'static, ()>) -> Result<(), NotLocal> {
Err(NotLocal)
}
fn spawn_blocking(&self, _: Box<dyn FnOnce() + Send>) {
unreachable!("the pure fast path spawns no blocking work")
}
fn sleep(&self, _: Duration) -> BoxFuture<'static, ()> {
unreachable!("the pure fast path never sleeps")
}
fn now(&self) -> Instant {
Instant::now()
}
}
#[test]
fn succeed_and_drive_through_the_pure_fast_path_allocates_nothing() {
let fiber = FiberContext::new_root(Arc::new(StubExec));
let waker = Waker::noop();
let mut cx = Context::from_waker(waker);
let region = Region::new(GLOBAL);
let eff: Effect<u32> = succeed(1);
let mut fut = eff.run_on(Arc::clone(&fiber));
let out = Pin::new(&mut fut).poll(&mut cx);
let stats = region.change();
assert!(matches!(out, Poll::Ready(Exit::Success(1))));
assert_eq!(stats.allocations, 0, "{stats:?}");
assert_eq!(stats.reallocations, 0, "{stats:?}");
}