pub struct TStr<T>(/* private fields */);
Expand description
A type-level string type, similar to a &'static str
const parameter.
§Examples
§Accessing Fields
This example demonstrates how you can use TStr
to implement a generic accessor trait.
use tstr::TStr;
use tstr::{TS, ts};
fn main() {
let mut tup = (3, 5, 8);
assert_eq!(tup.get(ts!(0)), &3);
assert_eq!(tup.get(ts!(1)), &5);
assert_eq!(tup.get(ts!(2)), &8);
let old_0 = replace(&mut tup, ts!(0), 333);
let old_1 = replace(&mut tup, ts!(1), 555);
let old_2 = replace(&mut tup, ts!(2), 888);
assert_eq!(tup.get(ts!(0)), &333);
assert_eq!(tup.get(ts!(1)), &555);
assert_eq!(tup.get(ts!(2)), &888);
assert_eq!(old_0, 3);
assert_eq!(old_1, 5);
assert_eq!(old_2, 8);
}
fn replace<T, N>(this: &mut T, name: TStr<N>, replacement: T::Field) -> T::Field
where
T: Access<TStr<N>>,
T::Field: Clone,
{
let ret = this.get(name).clone();
this.set(name, replacement);
ret
}
trait Access<N> {
type Field;
fn get(&self, _field_name: N) -> &Self::Field;
fn set(&mut self, _field_name: N, val: Self::Field);
}
impl<A, B, C> Access<TS!(0)> for (A, B, C) {
type Field = A;
fn get(&self, _field_name: TS!(0)) -> &A {
&self.0
}
fn set(&mut self, _field_name: TS!(0), val: A){
self.0 = val;
}
}
impl<A, B, C> Access<TS!(1)> for (A, B, C) {
type Field = B;
fn get(&self, _field_name: TS!(1)) -> &B {
&self.1
}
fn set(&mut self, _field_name: TS!(1), val: B){
self.1 = val;
}
}
impl<A, B, C> Access<TS!(2)> for (A, B, C) {
type Field = C;
fn get(&self, _field_name: TS!(2)) -> &C {
&self.2
}
fn set(&mut self, _field_name: TS!(2), val: C){
self.2 = val;
}
}
Implementations§
Trait Implementations§
source§impl<T> Ord for TStr<T>
impl<T> Ord for TStr<T>
source§impl<T> PartialEq for TStr<T>
impl<T> PartialEq for TStr<T>
source§impl<T> PartialOrd for TStr<T>
impl<T> PartialOrd for TStr<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<T, U> TStrEq<TStr<U>> for TStr<T>where
T: TStrEq<U>,
Available on crate feature cmp_traits
only.
impl<T, U> TStrEq<TStr<U>> for TStr<T>where
T: TStrEq<U>,
Available on crate feature
cmp_traits
only.source§impl<T, U> TStrOrd<TStr<U>> for TStr<T>where
T: TStrOrd<U>,
Available on crate features cmp_traits
and const_generics
only.
impl<T, U> TStrOrd<TStr<U>> for TStr<T>where
T: TStrOrd<U>,
Available on crate features
cmp_traits
and const_generics
only.impl<T> Copy for TStr<T>
impl<T> Eq for TStr<T>
Auto Trait Implementations§
impl<T> Freeze for TStr<T>
impl<T> RefUnwindSafe for TStr<T>
impl<T> Send for TStr<T>
impl<T> Sync for TStr<T>
impl<T> Unpin for TStr<T>
impl<T> UnwindSafe for TStr<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more