constduck
implements compile-time duck typing via const generics.
It only works on structs.
Note: if you can, use a custom trait instead of constduck
.
Only use constduck
in cases where you can't implement a custom trait.
You must #[derive(ConstDuck)]
on all traits that you want to duck-type.
For example:
#
# use *;
This implements Field<"money">
and ConstructFrom<T: WithField<"money">>
for Donald
.
You can use constduck
to derive implementations for traits, like #[derive(..)].
See [constduck/examples/debug-print.rs] for an example.
The example implements a trait (MyPrint
) for all types that derive ConstDuck
.
You can use [Field::get
], [Field::get_consume
] and [Field::set
] to access the fields of the struct. For example:
#
# use *;
use Sub;
let mut donald = Donald ;
let mut scrooge = Scrooge ;
, Ty = N>>
where N: Clone,
N: ,
N:
assert_eq!;
deduct_money;
assert_eq!;
assert_eq!;
deduct_money;
assert_eq!;
The main use case for constduck
is in macros.
You sometimes need to specify a type in a macro.
If you only know the type of the struct, it is normally impossible to obtain the type of a field.
With constduck
, you can write this type using generics. For example:
#
# use *;
}
make_getter!;