use core::{
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
fmt::{self, Debug},
hash::{Hash, Hasher},
marker::PhantomData,
};
#[cfg(feature = "const_panic")]
use const_panic::{
PanicVal,
fmt::{FmtArg, PanicFmt},
};
use crate::{IsTStr, TStrArg, strlike::StrLike};
#[cfg_attr(not(feature = "const_panic"), doc = "```ignore")]
#[cfg_attr(feature = "const_panic", doc = "```rust")]
pub struct TStr<S>(#[doc(hidden)] pub PhantomData<fn() -> S>);
const _: () = assert!(size_of::<crate::TS!("")>() == 0);
const _: () = assert!(align_of::<crate::TS!("")>() == 1);
impl<S> TStr<S> {
pub const fn new() -> Self {
TStr(PhantomData)
}
}
impl<S: TStrArg> TStr<S> {
pub const fn from_gen<G>(tstr: G) -> Self
where
G: IsTStr<Arg = S>,
{
<G as typewit::Identity>::TYPE_EQ.to_right(tstr)
}
pub const fn to_gen<G>(self) -> G
where
G: IsTStr<Arg = S>,
{
<G as typewit::Identity>::TYPE_EQ.to_left(self)
}
}
impl<S> Copy for TStr<S> {}
impl<S> Clone for TStr<S> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<S> Default for TStr<S> {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl<S> Debug for TStr<S>
where
S: TStrArg,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(self.to_str(), f)
}
}
impl<S> fmt::Display for TStr<S>
where
S: TStrArg,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.to_str(), f)
}
}
impl<S, S2> PartialEq<S2> for TStr<S>
where
S: TStrArg,
S2: ?Sized + StrLike,
{
#[inline(always)]
fn eq(&self, other: &S2) -> bool {
<str as PartialEq>::eq(self.as_str(), other.as_str())
}
}
impl<S> PartialEq<&str> for TStr<S>
where
S: TStrArg,
{
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
<str as PartialEq>::eq(self.as_str(), other)
}
}
impl<S> PartialEq<&&str> for TStr<S>
where
S: TStrArg,
{
#[inline(always)]
fn eq(&self, other: &&&str) -> bool {
<str as PartialEq>::eq(self.as_str(), other)
}
}
impl<S2> PartialEq<TStr<S2>> for str
where
S2: TStrArg,
{
#[inline(always)]
fn eq(&self, other: &TStr<S2>) -> bool {
<str as PartialEq>::eq(self, other.as_str())
}
}
impl<S2> PartialEq<TStr<S2>> for &str
where
S2: TStrArg,
{
#[inline(always)]
fn eq(&self, other: &TStr<S2>) -> bool {
<str as PartialEq>::eq(self, other.as_str())
}
}
impl<S2> PartialEq<TStr<S2>> for &&str
where
S2: TStrArg,
{
#[inline(always)]
fn eq(&self, other: &TStr<S2>) -> bool {
<str as PartialEq>::eq(self, other.as_str())
}
}
impl<S> Eq for TStr<S> where S: TStrArg {}
impl<S, S2> PartialOrd<S2> for TStr<S>
where
S: TStrArg,
S2: ?Sized + StrLike,
{
#[inline(always)]
fn partial_cmp(&self, other: &S2) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self.as_str(), other.as_str())
}
}
impl<S> PartialOrd<&str> for TStr<S>
where
S: TStrArg,
{
#[inline(always)]
fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self.as_str(), other)
}
}
impl<S> PartialOrd<&&str> for TStr<S>
where
S: TStrArg,
{
#[inline(always)]
fn partial_cmp(&self, other: &&&str) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self.as_str(), other)
}
}
impl<S2> PartialOrd<TStr<S2>> for str
where
S2: TStrArg,
{
#[inline(always)]
fn partial_cmp(&self, other: &TStr<S2>) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self, other.as_str())
}
}
impl<S2> PartialOrd<TStr<S2>> for &str
where
S2: TStrArg,
{
#[inline(always)]
fn partial_cmp(&self, other: &TStr<S2>) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self, other.as_str())
}
}
impl<S2> PartialOrd<TStr<S2>> for &&str
where
S2: TStrArg,
{
#[inline(always)]
fn partial_cmp(&self, other: &TStr<S2>) -> Option<Ordering> {
<str as PartialOrd>::partial_cmp(self, other.as_str())
}
}
impl<S> Ord for TStr<S>
where
S: TStrArg,
{
#[inline(always)]
fn cmp(&self, _other: &Self) -> Ordering {
Ordering::Equal
}
}
impl<S> Hash for TStr<S>
where
S: TStrArg,
{
fn hash<H>(&self, _state: &mut H)
where
H: Hasher,
{
}
}
#[cfg(feature = "const_panic")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_panic")))]
impl<S> PanicFmt for TStr<S>
where
S: TStrArg,
{
type This = Self;
type Kind = const_panic::IsCustomType;
const PV_COUNT: usize = 1;
}
#[cfg(feature = "const_panic")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_panic")))]
impl<S> TStr<S> {
pub const fn to_panicval(&self, fmtarg: FmtArg) -> PanicVal<'static>
where
S: TStrArg,
{
const_panic::StdWrapper(crate::to_str(*self)).to_panicval(fmtarg)
}
pub const fn to_panicvals(&self, fmtarg: FmtArg) -> [PanicVal<'static>; 1]
where
S: TStrArg,
{
[self.to_panicval(fmtarg)]
}
}