Working with visibility
Our Constructor
template above doesn't really make sense
if it's applied to a non-public type;
It would define the new()
function as public,
when the type is not meant to be exposed outside the crate.
(Rust may even complain that we're declaring
a public function that returns a private type!)
Let's fix this, and have our template give our constructor the same visibility as the type itself:
#![allow(unused)] fn main() { use derive_deftly::define_derive_deftly; define_derive_deftly! { Constructor for struct: impl<$tgens> $ttype where $twheres { // (this "$tvis" is new) $tvis fn new( $( $fname: $ftype , ) ) -> Self { Self { $( $fname , ) } } } } }
Here instead of saying pub fn new
,
we said $tvis fn new
.
The
$tvis
keyword will expand
to the visibility of the top-level type.
There is a similar keyword
$fvis
that expands to the visibility of the current field.
(Since enum variants are always visible,
there is no such keyword as $vvis
.
Since enum fields are always visible,
$fvis
in an enum always expands to pub
.)