Background
In my project, MyStructA and MyStructB have very similar code structure, which may only differs in some fields' type, for example:
struct MyStructA {
foo: u32
}
impl MyStructA {
fn bar(&self) -> u32 { self.foo + 1 }
}
struct MyStructB {
foo: u64
}
impl MyStructA {
fn bar(&self) -> u64 { self.foo + 1 }
}
MyStructA and MyStructB have similar fields, derive macros, impl traits, methods ...
In order to have a nice code, I can extract the same code to a generic:
struct MyStruct<T> {
foo: T
}
and just use newtype pattern:
type MyStructA = MyStruct<u32>;
type MyStructB = MyStruct<u64>;
Problem
Things become complicated when I want to add visibility.
I am writing a library, so for users, MyStruct<T> has no meaning, while MyStructA and MyStructB are what really matters. So:
MyStruct<T>may have private orpub(crate)visibility (and for not exposing private to public interface, I markMyStruct<T>aspubinside a private modulem)MyStructAandMyStructBhavepubvisibility.
For now, the code becomes:
mod m {
pub struct MyStruct<T> {
foo: T
}
// and some impls for `MyStruct<T>`
}
pub type MyStructA = m::MyStruct<u32>;
pub type MyStructB = m::MyStruct<u64>;
When I use cargo doc to generate documentations, the doc for MyStructA and MyStructB only tell users that they are type alias to MyStruct, with no methods, fields, derive marco or impl marco in it. And since I intentionally make MyStruct<T> private, we can never know MyStructA and MyStructB have foo field or any other methods in the documentation.
There are two possible solutions here:
- Is there any way where we can make doc of
MyStruct<T>into type aliasMyStructAandMyStructB? - Aside from newtype pattern, is there any way where we can write code once here, and generate two type
MyStructAandMyStructB?