doc_code_snippets! {
mod "guide_02",
type_ident=Guide02,
template=r##"
Here's an example of defining and
using a type-level-enum as a type-parameter to a struct.
Trait/Type glossary:
- ConstValue: A trait for type-level values,also used here to refer to implementors of it.
- ConstWrapper:
Zero-sized wrapper type for ConstValue that unconditionally implements
Eq/PartialEq/Ord/PartialOrd/Copy/Clone/Debug/etc ,
delegating IntoRuntime/GetField/SetField/etc to the wrapped ConstValue.
- IntoRuntime : trait for converting a ConstValue to a runtime-value.
//@use_codeblock:declare-enum,ignore
This is the enum we will use for declaring the mutability of the wrapper.
//@use_codeblock:declare-struct,ignore
Here is the zero-overhead wrapper struct that determines the mutability of its contents based on
the `Mut` parameter.
The `MutConstValue` derive macro generates the `Wrapper`
type alias which passes `Mut` wrapped inside a ConstWrapper
so that `Mut` does not have to implement Debug/PartialEq/etc.
Prefer using the type alias declared by MutConstValue everywhere,
except for Drop,instead use the generated `Wrapper_Ty` type to
which all the attributes are delegated.
The reason why Mut is wrapped inside a ConstWrapper in the field type is
because Rust does not allow type parameters to not be used.
//@use_codeblock:constructor,ignore
This is the constructor for Wrapper,which takes the `Mut` by value,
so as not to require using type inference.
When constructing a ConstWrapper inside a constructor function prefer using ConstWrapper::NEW.
When constructing a ConstWrapper whose type can't be inferred use Type::CW.
When there is a value that needs to be converted to a ConstWrapper
either use value.to_cw() or value.into() (if the type is Copy),
//@use_codeblock:deref-impls,ignore
The impls of Deref for Wrapper,
note that Deref takes a generic Mut parameter because it doesn't matter what the mutability
is for shared references,
and that DerefMut requires the ConstValue parameter to be `Mutable` .