#traits #expose #proc-macro #hidden #exposing

macro hidden-trait

Proc macro for exposing a trait implementation

3 releases

0.1.2 Dec 11, 2022
0.1.1 Dec 11, 2022
0.1.0 Dec 10, 2022

#12 in #exposing

Download history 1995/week @ 2024-07-22 2533/week @ 2024-07-29 1879/week @ 2024-08-05 1539/week @ 2024-08-12 1908/week @ 2024-08-19 2424/week @ 2024-08-26 1987/week @ 2024-09-02 1712/week @ 2024-09-09 2051/week @ 2024-09-16 1843/week @ 2024-09-23 1795/week @ 2024-09-30 1864/week @ 2024-10-07 1644/week @ 2024-10-14 1504/week @ 2024-10-21 1693/week @ 2024-10-28 1550/week @ 2024-11-04

6,655 downloads per month
Used in 6 crates (via blade-graphics)

MIT license

5KB
66 lines

hidden-trait

Build Status Docs Crates.io

This library is a proc macro to expose a trait implementation.

The case we are trying to solve here: a library exposes some concrete structure for people to use. There can be multiple of them (e.g. Vector2, Vector3, Vector4 in a math library), or maybe it's one per platform (Vulkan vs Metal). Important part is - internally the library would like to have a trait implemented by this public type, but it doesn't want to expose the trait itself because of ergonomic reasons. Hence, "hidden-trait" to rescue.

mod hidden {
    trait Foo {
        fn foo(&self) -> u32;
    }

    pub struct Bar;

    #[hidden_trait::expose]
    impl Foo for Bar {
        fn foo(&self) -> u32 {
            42
        }
    }
}

fn main() {
    let bar = hidden::Bar;
    // calling the trait method as if it's ours
    bar.foo();
}

Dependencies

~1.5MB
~38K SLoC