#traits #marker #blanket #macro #implementation

macro marker_trait

Implement a blanket implementation for a marker trait

3 stable releases

2.0.1 Oct 9, 2024
2.0.0 Oct 5, 2024
1.0.0 Sep 18, 2023

#821 in Rust patterns

Download history 1/week @ 2024-07-29 11/week @ 2024-09-23 96/week @ 2024-09-30 208/week @ 2024-10-07 44/week @ 2024-10-14

359 downloads per month

Apache-2.0

10KB
69 lines

Implement a blanket implementation for a marker trait.

MASTER CI status crates.io badge Coverage Status dependencies badge

Examples

Basic example
#[marker_trait::marker_trait]
trait Cloneable: Clone + PartialEq {}

#[derive(Clone, Eq, PartialEq, Debug)]
struct Wrapper<T>(T);

fn acceptor<T: Cloneable>(value: T) -> T { value }

assert_eq!(acceptor(Wrapper(1)), Wrapper(1)); // Compiles fine

Generated output:

trait Cloneable: Clone + PartialEq {}
impl<T: Clone + PartialEq> Cloneable for T {}
Generic example
trait MySuper<A, B>: AsRef<A> {
    type C;

    fn foo(self) -> Result<B, Self::C>;
}

#[marker_trait::marker_trait]
trait MySub<B, C>: MySuper<Self, B, C = C> + Sized {
}

struct MyStruct;
impl AsRef<MyStruct> for MyStruct {
  fn as_ref(&self) -> &Self { self }
}
impl MySuper<MyStruct, i8> for MyStruct {
  type C = u8;
  fn foo(self) -> Result<i8, Self::C> { Err(u8::MAX) }
}

fn acceptor<T: MySub<i8, u8>>(input: T) -> u8 { input.foo().unwrap_err() }

assert_eq!(acceptor(MyStruct), u8::MAX);

Generated output:

impl<B, C, __MarkerTrait__: MySuper<Self, B, C = C> + Sized> MySub<B, C> for __MarkerTrait__ {}
Failing examples
#[marker_trait::marker_trait]
trait Cloneable: Clone {}

struct NonClone;

fn acceptor<T: Cloneable>(value: T) -> T { value }

let _ = acceptor(NonClone); // Doesn't implement clone and therefore cloneable
#[marker_trait::marker_trait]
trait MyTrait: AsRef<Self::Foo> { // Empty trait body expected
  type Foo;
}
#[marker_trait::marker_trait]
trait Foo {} // Expected at least one supertrait

Dependencies

~260–710KB
~17K SLoC