use crate::allocator::StrAllocator;
use crate::ast::{HasArenaAlt, Str};
pub trait Contains {
type T: ?Sized;
fn inner(&self) -> &Self::T;
}
impl<T> Contains for &T {
type T = T;
#[inline]
fn inner(&self) -> &Self::T {
self
}
}
pub trait MetaData {
type T: ?Sized;
fn meta_data(&self) -> &Self::T;
fn display_meta_data(&self) -> String;
}
pub trait Repr {
type T;
fn repr(&self) -> &Self::T;
}
pub trait Allocatable<Env> {
type Out;
fn allocate(&self, env: &mut Env) -> Self::Out;
}
impl Contains for str {
type T = str;
#[inline]
fn inner(&self) -> &Self::T {
self
}
}
impl MetaData for &str {
type T = &'static str;
#[inline]
fn meta_data(&self) -> &Self::T {
&""
}
fn display_meta_data(&self) -> String {
"".into()
}
}
impl<Env: HasArenaAlt> Allocatable<Env> for &str {
type Out = Str;
#[inline]
fn allocate(&self, env: &mut Env) -> Self::Out {
env.arena_alt().allocate_symbol(self)
}
}
impl Contains for String {
type T = str;
#[inline]
fn inner(&self) -> &Self::T {
self
}
}
impl MetaData for String {
type T = &'static str;
#[inline]
fn meta_data(&self) -> &Self::T {
&""
}
fn display_meta_data(&self) -> String {
"".into()
}
}
impl<Env: HasArenaAlt> Allocatable<Env> for String {
type Out = Str;
#[inline]
fn allocate(&self, env: &mut Env) -> Self::Out {
env.arena_alt().allocate_symbol(self)
}
}
impl<T> MetaData for &T
where
T: MetaData,
{
type T = T::T;
fn meta_data(&self) -> &Self::T {
(*self).meta_data()
}
fn display_meta_data(&self) -> String {
(*self).display_meta_data()
}
}
impl<Env, T> Allocatable<Env> for &T
where
T: Allocatable<Env>,
{
type Out = T::Out;
fn allocate(&self, env: &mut Env) -> Self::Out {
(*self).allocate(env)
}
}
pub trait AllocatableString<Env>: Allocatable<Env, Out = Str> + MetaData {}
impl<Env, T> AllocatableString<Env> for T where T: Allocatable<Env, Out = Str> + MetaData {}