#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

#10 in #exposing

Download history 1141/week @ 2024-03-14 1158/week @ 2024-03-21 1060/week @ 2024-03-28 1061/week @ 2024-04-04 1756/week @ 2024-04-11 1975/week @ 2024-04-18 2789/week @ 2024-04-25 3069/week @ 2024-05-02 3020/week @ 2024-05-09 2299/week @ 2024-05-16 2397/week @ 2024-05-23 2172/week @ 2024-05-30 2086/week @ 2024-06-06 2949/week @ 2024-06-13 2614/week @ 2024-06-20 2046/week @ 2024-06-27

10,055 downloads per month
Used in 5 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
~36K SLoC