hidden-trait

Crates.iohidden-trait
lib.rshidden-trait
version0.1.2
sourcesrc
created_at2022-12-10 07:42:44.948224
updated_at2022-12-11 05:16:25.58923
descriptionProc macro for exposing a trait implementation
homepage
repositoryhttps://github.com/kvark/hidden-trait
max_upload_size
id733762
size8,612
Dzmitry Malyshau (kvark)

documentation

README

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();
}
Commit count: 6

cargo fmt