use std::ops::{Deref, Drop};
use tokio::sync::mpsc::Sender;
use crate::FnId;
#[derive(Debug)]
pub struct FnRef<'f, F> {
pub(crate) fn_id: FnId,
pub(crate) r#fn: &'f F,
pub(crate) fn_done_tx: Sender<FnId>,
}
impl<'f, F> Deref for FnRef<'f, F> {
type Target = F;
fn deref(&self) -> &Self::Target {
self.r#fn
}
}
impl<'f, F> Drop for FnRef<'f, F> {
fn drop(&mut self) {
let _ = self.fn_done_tx.try_send(self.fn_id);
}
}
#[cfg(test)]
mod tests {
use tokio::sync::mpsc;
use crate::FnId;
use super::FnRef;
#[test]
fn debug() {
let (fn_done_tx, _fn_done_rx) = mpsc::channel(1);
let fn_ref = FnRef {
fn_id: FnId::new(1),
r#fn: &(),
fn_done_tx,
};
assert!(
format!("{fn_ref:?}")
.starts_with("FnRef { fn_id: NodeIndex(FnIdInner(1)), fn: (), fn_done_tx: Sender")
);
}
}