Limiting a template to one kind of type.
The Constructor
template above doesn't work for enumerations.
If you try to apply it to one, you'll get
a not-entirely-helpful error message.
In earlier examples, we've shown how to make templates that apply to enums as well as structs. But let's suppose that in this case, we want our template to be struct-only.
We can tell derive-deftly about this restriction, to help it generate more useful error messages:
#![allow(unused)] fn main() { use derive_deftly::define_derive_deftly; define_derive_deftly! { Constructor for struct: // (1) // The rest is unchanged impl<$tgens> $ttype where $twheres { pub fn new( $( $fname: $ftype , ) ) -> Self { Self { $( $fname , ) } } } } }
(Note the use of
for struct
above at // (1)
.)
Now if we try to apply our template to an enum, we'll get a more useful error:
error: template defined for struct, but applied to enum