#specialization #zero-cost #macro #generics

macro min-specialization

Experimental implementation of specialization

3 releases

0.1.2 Sep 21, 2024
0.1.1 Sep 15, 2024
0.1.0 Sep 1, 2024

#1993 in Rust patterns

MIT license

57KB
1.5K SLoC

min-specialization Latest Version Documentation GitHub Actions

Rust's specialization feature allows you to provide a default implementation of a trait for generic types and then specialize it for specific types. This feature is currently unstable and only available on the nightly version of Rust.

This crate emulates Rust's #[feature(min_specialization)] unstable feature on stable Rust.

Example

# use min_specialization::specialization;
#[specialization]
mod inner {
    #[allow(unused)]
    trait Trait<U> {
        type Ty;
        fn number(_: U) -> Self::Ty;
    }

    impl<T, U> Trait<U> for T {
        type Ty = usize;
        default fn number(_: U) -> Self::Ty {
            0
        }
    }

    impl<U> Trait<U> for () {
        fn number(_: U) -> Self::Ty {
            1
        }
    }
}

see tests for more.

Dependencies

~275–720KB
~16K SLoC