use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct Unique<T: 'static + Send> {
inner: Arc<Mutex<Option<T>>>,
}
impl<T> Clone for Unique<T>
where
T: 'static + Send,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T: 'static + Send> Unique<T> {
pub fn new(inner: T) -> Self {
Self {
inner: Arc::new(Mutex::new(Some(inner))),
}
}
pub fn take(&self) -> Option<T> {
let result = self.inner.lock().unwrap().take();
if result.is_some() {
tracing::info!(
"Resource {} has been taken",
std::any::type_name::<Unique<T>>()
);
}
result
}
}