use std::cell::Cell;

use super::Driver;

pub type CurrentSlot = dope_core::driver::CurrentSlot<crate::UringBackend>;

thread_local! {
    static CURRENT: Cell<Option<CurrentSlot>> = const { Cell::new(None) };
}

#[inline(always)]
pub fn set_current(slot: Option<CurrentSlot>) {
    CURRENT.with(|cell| cell.set(slot));
}

#[inline(always)]
pub fn current() -> Option<CurrentSlot> {
    CURRENT.with(|cell| cell.get())
}

#[inline(always)]
pub fn current_driver_mut<'a>() -> &'a mut Driver {
    let slot = current()
        .expect("dope-uring invariant violated: no current driver (called outside runtime?)");
    unsafe { &mut *slot.driver().as_ptr() }
}
