marker_trait

Crates.iomarker_trait
lib.rsmarker_trait
version2.0.1
sourcesrc
created_at2023-09-18 01:14:34.207258+00
updated_at2024-10-09 08:09:37.703406+00
descriptionImplement a blanket implementation for a marker trait.
homepage
repositoryhttps://github.com/Alorel/marker_trait-rs
max_upload_size
id975447
size10,937
Art (Alorel)

documentation

README

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
Commit count: 9

cargo fmt