Proc macro to generate concrete types from generics?

Considering code like the following:

pub struct Generic<T> {
    inner: Vec<T>, // just an example
}
impl<T> Generic<T> {
    /// description for rustdoc
    pub fn do_stuff(&self) {}
}
pub type Foo = Generic<bool>;
pub type Bar = Generic<usize>;

Is there a crate that would allow to turn the above into something like:

pub struct Foo {
    inner: Vec<bool>, // just an example
}
impl Foo {
    /// description for rustdoc
    pub fn do_stuf(&self) {}
}
pub struct Bar {
    inner: Vec<usize>, // just an example
}
impl Bar {
    /// description for rustdoc
    pub fn do_stuf(&self) {}
}

etc.
The idea being that the documentation for the latter would be easier to digest.

From the crates I do know, the closest would be inherent, with something like:

use inherent::inherent;
trait Trait<T> {
    fn do_stuff(&self) { }
}
pub struct Foo {
    inner: Vec<bool>, // just an example
}
#[inherent]
impl Trait<bool> for Foo {
    /// description for rustdoc
    pub fn do_stuff(&self);
}
pub struct Bar {
    inner: Vec<usize>, // just an example
}
#[inherent]
impl Trait<usize> for Bar {
    /// description for rustdoc
    pub fn do_stuff(&self);
}

which would allow to avoid the code duplication (the actual implementation would be in the trait), but documentation would be largely repeated (although for e.g. examples, that's necessary).

Would a declarative macro work?

macro_rules! generic {
    ($id:ident,$t:ty) => {
        pub struct $id {
            inner: Vec<$t>,
        }
        impl $id {
            /// description for rustdoc
            pub fn do_stuff(&self) {}
        }
    };
}

generic!(Foo, bool);
generic!(Bar, usize);

That could work, but in many cases rustfmt doesn't want to touch inside macros.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.