I have a derive-macro, let's call it Mirror
, that is implemented for named structs, like so:
#[derive(Mirror)]
struct Point {
x: f32,
y: f32,
}
Mirror is a trait too:
trait Mirror {
type Mirrored;
fn mirror(self) -> Self::Mirrored;
}
The macro generates the associated Mirrored
type. It is always a named struct and its fields depend on the fields of the base type (Point
).
Now, the user can do let m = Point { .. }.mirror();
which is good. However, I want the user to be able to add an impl for the mirrored type, as well as an alias and export it publicly. My first attempt was generating an arbitrary (undocumented) name from the macro, then using pub type MyPoint = <Point as Mirror>::Mirrored; impl MyPoint { .. }
but that gives error[E0118]: no base type found for inherent implementation
.
I'm now considering:
- Generate a deterministic name in the macro, e.g.
PointMirror
. This is not ideal since it relies on an implementation detail of the proc-macro. - Add attributes for choosing a name, e.g.
#[mirror(name="MyType", visibility=pub)]
. This is at least explicit, but is awkward.
Is there any precedent in Rust for how to choose a name and visibility of a generated type?