use std::cell::UnsafeCell;
use std::fmt::Debug;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

/// 程序退出信号
pub struct ExitSignal<T: Debug + Clone>(pub Sender<T>, pub T, pub bool);
impl<T: Debug + Clone> Drop for ExitSignal<T> {
    fn drop(&mut self) {
        if self.2 {
            return;
        }
        debug!("ExitSignal drop: {:?}", &self.1);
        if let Err(_e) = self.0.send(self.1.clone()) {
            error!("程序退出drop执行退出信息出错: {:?}", _e);
        }
    }
}
impl<T: Debug + Clone> ExitSignal<T> {
    pub fn new_channl() -> (Sender<T>, Receiver<T>) {
        mpsc::channel()
    }
    pub fn close(&mut self, msg: T) {
        if let Err(_e) = self.0.send(msg) {
            error!("程序退出信号出错: {:?}", _e.to_string());
        };
        self.2 = true;
    }
}

/// 程序退出信号
pub struct ExitSignal2<F: FnMut()>(pub F);
impl<F: FnMut()> Drop for ExitSignal2<F> {
    fn drop(&mut self) {
        self.0()
    }
}
