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