use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
use core::ops::Deref;
use core::ptr;
use crate::rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
mod private {
#[derive(Clone, Copy, Debug)]
pub struct PrivateZst;
}
pub struct Interned<'a, T>(pub &'a T, pub private::PrivateZst);
impl<'a, T> Interned<'a, T> {
#[inline]
pub const fn new_unchecked(t: &'a T) -> Self {
Interned(t, private::PrivateZst)
}
}
impl<'a, T> Clone for Interned<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for Interned<'a, T> {}
impl<'a, T> Deref for Interned<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.0
}
}
impl<'a, T> PartialEq for Interned<'a, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
ptr::eq(self.0, other.0)
}
}
impl<'a, T> Eq for Interned<'a, T> {}
impl<'a, T> Hash for Interned<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, s: &mut H) {
ptr::hash(self.0, s)
}
}
impl<T> StableHash for Interned<'_, T>
where
T: StableHash,
{
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
self.0.stable_hash(hcx, hasher);
}
}
impl<T: Debug> Debug for Interned<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests;