assert_type_match
A niche utility macro to statically assert that a type matches another type.
Purpose
The primary purpose of this crate is to make copying and pasting types from other crates into your own more future-proof by statically asserting that the types match.
This situation happens sometimes when you want to add your own methods or documentation on a foreign type.
By using this crate, you can ensure that changes made upstream will be caught by the compiler, so you can update your code accordingly.
Usage
// Pretend this type comes from a foreign crate:
If foreign_crate::ColorSpace
ever changes, the compiler will catch it.
For example, if foreign_crate::ColorSpace
adds a new variant Hsla
, we'll get the following error:
error[E0004]: non-exhaustive patterns: `foreign_crate::ColorSpace::Hsla` not covered
Configuration
The behavior of the macro can be configured.
One common pattern is implementing From
to convert between the types.
This can be automatically generated by setting the from
attribute:
let my_space: ColorSpace = Rgb;
// Convert to the foreign type:
let foreign_space: ColorSpace = my_space.into;
// And back again:
let my_space: ColorSpace = foreign_space.into;
There are other attributes available, such as test_only
and skip_name
,
as well as attributes to control the behavior of specific fields and variants.
See the docs for more information.