#specialization #macro #zerocost

macro min-specialization

Experimental implementation of specialization

2 releases

new 0.1.1 Sep 15, 2024
0.1.0 Sep 1, 2024

#1348 in Rust patterns

Download history 132/week @ 2024-08-29 13/week @ 2024-09-05 216/week @ 2024-09-12

361 downloads per month

MIT license

56KB
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

~300–760KB
~17K SLoC