Macro named_tup::Tup

source ·
Tup!() { /* proc-macro */ }
Expand description

Produces a type annotation for the tup struct. If an expression is needed instead please use the tup! macro.

It is used in a very similar way to the tup! macro but with any expression replaced with a type.

let empty: Tup!() = tup!();
// Since it's just a normal type it can coerce certain literals
let num: Tup!(foo: u8) = tup!(foo: 5);

assert_eq!(test(tup!(foo: 5, bar: 6)), tup!(foo: 5));

// It can also just be used as an argument in functions
fn test(arg: Tup!(foo: i32, bar: i64)) -> Tup!(foo: i32) {
    tup!(foo: arg.foo)
}

However it’s main use case is to provide default values using the #[tup_default] attribute macro and the TupInto trait to convert between different types.

#[tup_default]
pub fn main() {
    let default: Tup!(foo: i32 = 2) = tup!().into_tup();
    let result = default_to_non(tup!().into_tup());

    assert_eq!(result, tup!(foo: 2));
    assert_eq!(result.foo, default.foo);

    // Will print tup { foo: 2 }
    println!("{result:?}");

    // Will print tup { foo: 2 (=2) } where (=2) is the default value
    println!("{default:?}");
}
#[tup_default]
fn default_to_non(n_tup: Tup!(foo: i32 = 2)) -> Tup!(foo: i32) {
    n_tup.into_tup()
}