use std::any::TypeId;
use std::fmt;
use std::sync::LazyLock;
use crate::shared::{self, AliasContext};
static GLOBAL: LazyLock<AliasContext> = LazyLock::new(AliasContext::new);
pub fn global_ctx() -> &'static AliasContext {
&GLOBAL
}
pub trait Aliasing: fmt::Debug + 'static {
fn aliased(&self) -> Aliased<'_, Self> {
Aliased { val: self }
}
fn alias_prefix(prefix: &str) {
shared::set_prefix(global_ctx(), TypeId::of::<Self>(), prefix);
}
fn alias_numbered(&self) -> &Self {
shared::register_numbered(
global_ctx(),
TypeId::of::<Self>(),
format!("{self:?}"),
format!("{self:#?}"),
);
self
}
fn alias_named(&self, name: &str) -> &Self {
shared::register_named(
global_ctx(),
TypeId::of::<Self>(),
format!("{self:?}"),
format!("{self:#?}"),
name,
);
self
}
}
impl<T> Aliasing for T where T: fmt::Debug + 'static {}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Aliased<'v, T: ?Sized> {
val: &'v T,
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for Aliased<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
shared::fmt_aliased(self.val, global_ctx(), f)
}
}