[][src]Macro structural::TS

macro_rules! TS {
    (0) => { ... };
    (1) => { ... };
    (2) => { ... };
    (3) => { ... };
    (4) => { ... };
    (5) => { ... };
    (6) => { ... };
    (7) => { ... };
    (8) => { ... };
    (9) => { ... };
    (_) => { ... };
    ( $literal:literal ) => { ... };
    ($ident:ident) => { ... };
}

For getting the type of a TStr<_> (type-level string).

You can also use tstr_aliases to declare one or more aliases for type-level strings.

Inputs

This has the same syntax as the ts macro, a single identifier,string literal, or integer.

Small Example:

use structural::TS;

type Foo=TS!("foo");

type Bar=TS!(foo); // Equivalent to `TS!("foo")`

type Baz=TS!(100); // Equivalent to `TS!("100")`

Example

This example demonstrates how TStr can be used to manually bound a type parameter with the *VariantField* traits,to access a variant field.

use structural::{StructuralExt,FP,Structural,TS,ts};
use structural::{GetFieldType, GetVariantFieldType, IntoVariantFieldMut, VariantField};

// `GetFieldType<This,FP!(::Ok.0)>` can also be written as
// `GetVariantFieldType<This,TS!(Ok),TS!(0)>`.
//
// `GetVariantFieldType` is useful in generic contexts where
// the name of the variant is taken  separately from the name of the field.
fn into_ok<This>(this: This)->Option<GetFieldType<This,FP!(::Ok.0)>>
where
    This: IntoVariantFieldMut<TS!(Ok),TS!(0)>
{
    // Equivalent to: `this.into_field(fp!(::Ok.0))`
    this.into_field(VariantField::new(ts!("Ok"), ts!("0")))
}

#[derive(Structural)]
enum ResultLike<T,E>{
    Ok(T),
    Err(E),
}

assert_eq!( into_ok(ResultLike::<_,()>::Ok(99)), Some(99));
assert_eq!( into_ok(ResultLike::<(),_>::Err(99)), None);

assert_eq!( into_ok(Result::<_,()>::Ok(99)), Some(99));
assert_eq!( into_ok(Result::<(),_>::Err(99)), None);

Example

This example uses the TS macro to access a single non-nested field, instead of the FP or fp macros.

use structural::{GetField,StructuralExt,Structural,FP,TS};

fn main(){
    let phone=CellPhone{
        memory: Bytes{ bytes:64_000_000_000 },
        charge: Charge{ percent:50 },
    };
    assert_eq!( get_charge(&phone).percent, 50 );

    let battery=Battery{
        charge: Charge{ percent:70 },
    };
    assert_eq!( get_charge(&battery).percent, 70 );
}

type charge_TStr=TS!(charge);

// An `FP!(identifier)` is the same type as `TS!(identifier)`,
// but because it's more flexible it's used for field paths by default.
// Eg:You can write `GetFieldType<FooEnum, FP!(::Foo.bar)>` with `FP` but not with `TS`.
//
// `TS` always produces the `TStr` type,
// while FP produces different types depending on the input.
fn get_charge( this:&dyn GetField<FP!(charge), Ty=Charge> )-> Charge {
    this.field_(charge_TStr::NEW).clone()
}

#[derive(Structural)]
struct CellPhone{
    pub memory: Bytes,
    pub charge: Charge,
}

#[derive(Structural)]
struct Battery{
    pub charge: Charge,
}

#[derive(Debug,Copy,Clone)]
struct Bytes{
    bytes: u64,
}

#[derive(Debug,Copy,Clone)]
struct Charge{
    percent: u8,
}