#traits #marker #implementation #blanket #macro

macro marker_trait

Implement a blanket implementation for a marker trait

1 stable release

1.0.0 Sep 18, 2023

#1433 in Rust patterns

Apache-2.0

12KB
159 lines

Implement a blanket implementation for a marker trait.

MASTER CI status crates.io badge docs.rs badge dependencies badge

Basic Example

#[marker_trait::marker_trait]
pub trait AsyncTask: Send + 'static {}

struct MySendStatic;
static_assertions::assert_impl_all!(MySendStatic: Send, AsyncTask);

Generated output:

pub trait AsyncTask: Send + 'static {}
impl<T: Send + 'static> AsyncTask for T {}

Sealed example

#[marker_trait::marker_trait(sealed)]
pub trait AsyncTask: Send + 'static {}

struct MySendStatic;
static_assertions::assert_impl_all!(MySendStatic: Send, AsyncTask, __SealModuleForAsyncTask__::Sealed);

Generated output:

pub trait AsyncTask: Send + 'static + __SealModuleForAsyncTask__::Sealed {}
mod __SealModuleForAsyncTask__ {
   use super::*;

    impl<__AsyncTaskImplementor__> Sealed for __AsyncTaskImplementor__
      where __AsyncTaskImplementor__: Send + 'static {}

    pub trait Sealed {}
}
#[automatically_derived]
impl<__MarkerTrait__: Send + 'static + __SealModuleForAsyncTask__::Sealed> AsyncTask for __MarkerTrait__ {}

Dependencies

~0.4–0.8MB
~19K SLoC