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
359 downloads per month
10KB
69 lines
Implement a blanket implementation for a marker trait.
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