pub trait IsTStr:
Identity<Type = TStr<Self::Arg>>
+ 'static
+ StrLike<__TStr = Self>
+ Copy
+ Clone
+ Debug
+ Display
+ Default
+ Hash
+ Eq
+ Ord
+ PartialEq
+ PartialEq<str>
+ for<'a> PartialEq<&'a str>
+ for<'a, 'b> PartialEq<&'a &'b str>
+ PartialOrd
+ PartialOrd<str>
+ for<'a, 'b> PartialOrd<&'a &'b str>
+ Send
+ Sized
+ Sync
+ Unpin {
type Arg: TStrArg;
const VAL: Self;
const LENGTH: usize;
const BYTES: &[u8];
const STR: &str;
// Provided methods
fn to_tstr(self) -> TStr<Self::Arg> { ... }
fn from_tstr(tstr: TStr<Self::Arg>) -> Self { ... }
fn len(self) -> usize { ... }
fn to_str(self) -> &'static str { ... }
fn to_bytes(self) -> &'static [u8] { ... }
fn tstr_eq<Rhs: IsTStr>(self, rhs: Rhs) -> bool { ... }
fn tstr_ne<Rhs: IsTStr>(self, rhs: Rhs) -> bool { ... }
fn tstr_cmp<Rhs: IsTStr>(self, rhs: Rhs) -> Ordering { ... }
fn type_eq<Rhs: IsTStr>(self, rhs: Rhs) -> TypeCmp<Self, Rhs> { ... }
}Expand description
Many associated items of the TStr type-level string,
as well as supertraits for traits implemented by it.
This trait is sealed and cannot be implemented outside of the tstr crate.
Required Associated Constants§
Required Associated Types§
Provided Methods§
Sourcefn to_tstr(self) -> TStr<Self::Arg>
fn to_tstr(self) -> TStr<Self::Arg>
Coerces Self to TStr<Self::Arg>, only necessary in generic contexts
The const equivalent of this trait method is the
TStr::from_gen constructor.
While it’s always possible to construct a TStr through its
new constructor,
this method ensures that it’s the same string as Self.
§Example
use tstr::{IsTStr, TStr};
#[repr(transparent)]
struct Foo<T, N: IsTStr> {
val: T,
// since TStr is zero-sized, it can be put in `#[repr(transparent)]` types
// next to the wrapped non-Zero-Sized-Type.
name: TStr<N::Arg>,
}
impl<T, N: IsTStr> Foo<T, N> {
pub fn new(val: T, tstr: N) -> Self {
Self{ val, name: tstr.to_tstr() }
}
}Sourcefn from_tstr(tstr: TStr<Self::Arg>) -> Self
fn from_tstr(tstr: TStr<Self::Arg>) -> Self
Coerces a TStr into Self, only necessary in generic contexts.
The const equivalent of this trait method is the
TStr::to_gen method.
While it’s always possible to construct Self through the
VAL associated constant,
this function ensures that it’s the same string as the argument.
§Example
use tstr::{IsTStr, TStr};
#[repr(transparent)]
struct Foo<T, N: IsTStr> {
val: T,
name: TStr<N::Arg>,
}
impl<T, N: IsTStr> Foo<T, N> {
fn name(&self) -> N {
N::from_tstr(self.name)
}
}Sourcefn to_str(self) -> &'static str
fn to_str(self) -> &'static str
Gets the &'static str equivalent of this TStr
The const equivalent of this trait is the
tstr::to_str function.
§Example
use tstr::{IsTStr, TStr, ts};
let foo: TStr<_> = ts!(foo);
assert_eq!(foo.to_str(), "foo");
let bar_str: &str = ts!("bar").to_str();
assert_eq!(bar_str, "bar");
Sourcefn to_bytes(self) -> &'static [u8]
fn to_bytes(self) -> &'static [u8]
Gets the &'static [u8] equivalent of this TStr
The const equivalent of this trait is the
tstr::to_bytes function.
§Example
use tstr::{IsTStr, TStr, ts};
let foo: TStr<_> = ts!(foo);
assert_eq!(foo.to_bytes(), "foo".as_bytes());
let bar_str: &[u8] = ts!("bar").to_bytes();
assert_eq!(bar_str, "bar".as_bytes());
Sourcefn tstr_eq<Rhs: IsTStr>(self, rhs: Rhs) -> bool
fn tstr_eq<Rhs: IsTStr>(self, rhs: Rhs) -> bool
Compares two TStrs for equality
The const equivalent of this trait is the
tstr::eq function.
This method exists to allow comparing any TStr to any other,
because the for<Rhs: IsTStr> PartialEq<Rhs> supertrait can’t be written on stable.
§Examples
use tstr::{IsTStr, ts};
assert!( ts!("foo").tstr_eq(ts!("foo")));
assert!(!ts!("foo").tstr_eq(ts!("bar")));
Sourcefn tstr_ne<Rhs: IsTStr>(self, rhs: Rhs) -> bool
fn tstr_ne<Rhs: IsTStr>(self, rhs: Rhs) -> bool
Compares two TStrs for inequality
The const equivalent of this trait is the
tstr::ne function.
This method exists to allow comparing any TStr to any other,
because the for<Rhs: IsTStr> PartialEq<Rhs> supertrait can’t be written on stable.
§Examples
use tstr::{IsTStr, ts};
assert!(!ts!("foo").tstr_ne(ts!("foo")));
assert!( ts!("foo").tstr_ne(ts!("bar")));
Sourcefn tstr_cmp<Rhs: IsTStr>(self, rhs: Rhs) -> Ordering
fn tstr_cmp<Rhs: IsTStr>(self, rhs: Rhs) -> Ordering
Compares two TStrs for ordering
The const equivalent of this trait is the
tstr::cmp function.
This method exists to allow comparing any TStr to any other,
because the for<Rhs: IsTStr> PartialOrd<Rhs> supertrait can’t be written on stable.
§Examples
use tstr::{IsTStr, ts};
use core::cmp::Ordering;
assert_eq!(ts!("foo").tstr_cmp(ts!("foo")), Ordering::Equal);
assert_eq!(ts!("foo").tstr_cmp(ts!("bar")), Ordering::Greater);
assert_eq!(ts!("bar").tstr_cmp(ts!("foo")), Ordering::Less);
Sourcefn type_eq<Rhs: IsTStr>(self, rhs: Rhs) -> TypeCmp<Self, Rhs>
fn type_eq<Rhs: IsTStr>(self, rhs: Rhs) -> TypeCmp<Self, Rhs>
Compares two TStrs for equality,
returning a proof of (in)equality of Self and Rhs
The const equivalent of this trait is the
tstr::type_eq function.
§Example
use tstr::{IsTStr, TStr, TS, ts};
use tstr::typewit::TypeCmp;
assert_eq!(is_right_guess(Guess(ts!(foo))), None);
assert_eq!(is_right_guess(Guess(ts!(bar))), None);
assert_eq!(is_right_guess(Guess(ts!(world))), None);
assert!(is_right_guess(Guess(ts!(hello))).is_some_and(|x| x.0 == "hello"));
#[derive(Debug, PartialEq, Eq)]
struct Guess<S: IsTStr>(S);
fn is_right_guess<S: IsTStr>(guess: Guess<S>) -> Option<Guess<impl IsTStr>> {
let ret: Option<Guess<TS!(hello)>> = typecast_guess(guess).ok();
ret
}
/// Coerces `Guess<A>` to `Guess<B>` if `A == B`, returns `Err(guess)` if `A != B`.
fn typecast_guess<A, B>(guess: Guess<A>) -> Result<Guess<B>, Guess<A>>
where
A: IsTStr,
B: IsTStr,
{
tstr::typewit::type_fn!{
// type-level function from `S` to `Guess<S>`
struct GuessFn;
impl<S: IsTStr> S => Guess<S>
}
match A::VAL.type_eq(B::VAL) {
TypeCmp::Eq(te) => Ok(
// te is a `TypeEq<A, B>`, a value-level proof that both args are the same type.
te
.map(GuessFn) // : TypeEq<Guess<A>, Guess<B>>
.to_right(guess) // : Guess<B>
),
TypeCmp::Ne(_) => Err(guess),
}
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.