use std::fmt;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
pub(crate) struct LifecycleId {
id: usize,
type_name: &'static str,
}
static NEXT_LIFECYCLE_ID: AtomicUsize = AtomicUsize::new(2000);
impl LifecycleId {
pub(crate) fn new<T>() -> Self {
let type_name = std::any::type_name::<T>();
let type_name = type_name.rsplit("::").next().unwrap_or(type_name);
Self::new_with_type_name(type_name)
}
pub(crate) fn new_with_type_name(type_name: &'static str) -> Self {
let id = NEXT_LIFECYCLE_ID.fetch_add(1, Ordering::AcqRel);
tracing::debug!(target: "dag::lifecycle::create", type_name = type_name, id = id);
Self { id, type_name }
}
}
impl fmt::Debug for LifecycleId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.type_name, self.id)
}
}
impl Clone for LifecycleId {
fn clone(&self) -> Self {
let type_name = self.type_name;
let id = NEXT_LIFECYCLE_ID.fetch_add(1, Ordering::AcqRel);
tracing::debug!(
target: "dag::lifecycle::clone",
type_name = type_name,
id = id,
from_id = self.id,
);
Self { id, type_name }
}
}
impl Drop for LifecycleId {
fn drop(&mut self) {
let type_name = self.type_name;
let id = self.id;
tracing::debug!(target: "dag::lifecycle::drop", type_name = type_name, id = id);
}
}