[][src]Macro openapi_context::new_context_type

macro_rules! new_context_type {
    ($context_name:ident, $empty_context_name:ident, $($types:ty),+ ) => { ... };
    (impl extend_has $context_name:ident, $empty_context_name:ident, $head:ty, $($tail:ty),+ ) => { ... };
    (impl extend_has $context_name:ident, $empty_context_name:ident, $head:ty) => { ... };
    (impl extend_has_helper
        $context_name:ident,
        $empty_context_name:ident,
        $type:ty,
        $($types:ty),+
        ) => { ... };
}

Defines a struct that can be used to build up contexts recursively by adding one item to the context at a time, and a unit struct representing an empty context. The first argument is the name of the newly defined context struct that is used to add an item to the context, the second argument is the name of the empty context struct, and subsequent arguments are the types that can be stored in contexts built using these struct.

A cons list built using the generated context type will implement Has and Pop for each type T that appears in the list, provided that the list only contains the types that were passed to the macro invocation after the context type name.

All list types constructed using the generated types will implement Push<T> for all types T that appear in the list passed to the macro invocation.

E.g.


#[derive(Default)]
struct MyType1;
#[derive(Default)]
struct MyType2;
#[derive(Default)]
struct MyType3;
#[derive(Default)]
struct MyType4;

new_context_type!(MyContext, MyEmpContext, MyType1, MyType2, MyType3);

fn use_has_my_type_1<T: Has<MyType1>> (_: &T) {}
fn use_has_my_type_2<T: Has<MyType2>> (_: &T) {}
fn use_has_my_type_3<T: Has<MyType3>> (_: &T) {}
fn use_has_my_type_4<T: Has<MyType4>> (_: &T) {}

// will implement `Has<MyType1>` and `Has<MyType2>` because these appear
// in the type, and were passed to `new_context_type!`. Will not implement
// `Has<MyType3>` even though it was passed to `new_context_type!`, because
// it is not included in the type.
type ExampleContext = MyContext<MyType1, MyContext<MyType2,  MyEmpContext>>;

// Will not implement `Has<MyType4>` even though it appears in the type,
// because `MyType4` was not passed to `new_context_type!`.
type BadContext = MyContext<MyType1, MyContext<MyType4, MyEmpContext>>;

fn main() {
    let context : ExampleContext =
        MyEmpContext::default()
        .push(MyType2{})
        .push(MyType1{});

    use_has_my_type_1(&context);
    use_has_my_type_2(&context);
    // use_has_my_type_3(&context);      // will fail

    // Will fail because `MyType4`// was not passed to `new_context_type!`
    // let context = MyEmpContext::default().push(MyType4{});

    let bad_context: BadContext = BadContext::default();
    // use_has_my_type_4(&bad_context);  // will fail

}

See the context_tests module for more usage examples.