Crates.io | marker_trait |
lib.rs | marker_trait |
version | 2.0.1 |
source | src |
created_at | 2023-09-18 01:14:34.207258+00 |
updated_at | 2024-10-09 08:09:37.703406+00 |
description | Implement a blanket implementation for a marker trait. |
homepage | |
repository | https://github.com/Alorel/marker_trait-rs |
max_upload_size | |
id | 975447 |
size | 10,937 |
Implement a blanket implementation for a marker trait.
#[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 {}
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__ {}
#[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