#disjoint #associated #impl #exclusive #implementation

disjoint_impls

支持多种相互不重叠的实现

8个版本 (破坏性)

0.7.1 2024年5月20日
0.7.0 2024年1月23日
0.6.0 2024年1月8日
0.5.0 2023年11月23日
0.1.0 2023年9月23日

#1019 in 过程宏

Download history 62/week @ 2024-04-13 114/week @ 2024-04-20 53/week @ 2024-04-27 47/week @ 2024-05-04 81/week @ 2024-05-11 184/week @ 2024-05-18 64/week @ 2024-05-25 34/week @ 2024-06-01 67/week @ 2024-06-08 32/week @ 2024-06-15 17/week @ 2024-06-22 75/week @ 2024-06-29 134/week @ 2024-07-06 99/week @ 2024-07-13 49/week @ 2024-07-20 93/week @ 2024-07-27

每月下载量421次

Apache-2.0

64KB
1.5K SLoC

License: Apache 2.0 Crates.io

该库将得到维护(至少)直到Rust编译器直接允许这种用法此用法

描述

此库允许您编写Rust编译器(目前)不允许的某些类型的互斥实现。具体来说,这是一个类型由关联类型约束的互斥实现。人们可能希望以下语法能够编译,而无需调用disjoint_impls!,但它不能

use disjoint_impls::disjoint_impls;

pub trait Dispatch {
    type Group;
}

pub enum GroupA {}
impl Dispatch for String {
    type Group = GroupA;
}

pub enum GroupB {}
impl Dispatch for i32 {
    type Group = GroupB;
}

// Basic example
disjoint_impls! {
    pub trait BasicKita {
        const BASIC_NAME: &'static str;

        fn basic_name() -> &'static str {
            "Default blanket"
        }
    }

    impl<T: Dispatch<Group = GroupA>> BasicKita for T {
        const BASIC_NAME: &'static str = "Blanket A";
    }
    impl<U: Dispatch<Group = GroupB>> BasicKita for U {
        const BASIC_NAME: &'static str = "Blanket B";

        fn basic_name() -> &'static str {
            "Blanket B"
        }
    }
}

// Complex example
disjoint_impls! {
    pub trait ComplexKita {
        const COMPLEX_NAME: &'static str;
    }

    impl<T: Dispatch<Group = GroupA>, U: Dispatch<Group = GroupA>> ComplexKita for (T, U) {
        const COMPLEX_NAME: &'static str = "Blanket AA";
    }
    impl<U, T> ComplexKita for (U, T)
    where
        U: Dispatch<Group = GroupA>,
        T: Dispatch<Group = GroupB>
    {
        const COMPLEX_NAME: &'static str = "Blanket AB";
    }
    impl<T: Dispatch<Group = GroupB>, U: Dispatch> ComplexKita for (T, U) {
        const COMPLEX_NAME: &'static str = "Blanket B*";
    }

    impl<T: Dispatch<Group = GroupA>> ComplexKita for T {
        const COMPLEX_NAME: &'static str = "Blanket A";
    }
    impl<U: Dispatch<Group = GroupB>> ComplexKita for U {
        const COMPLEX_NAME: &'static str = "Blanket B";
    }
}

fn main() {
    assert_eq!("Blanket A", String::BASIC_NAME);
    assert_eq!("Blanket B", i32::BASIC_NAME);

    assert_eq!("Default blanket", String::basic_name());
    assert_eq!("Blanket B", i32::basic_name());

    assert_eq!("Blanket A", String::COMPLEX_NAME);
    assert_eq!("Blanket B", i32::COMPLEX_NAME);

    assert_eq!("Blanket AA", <(String, String)>::COMPLEX_NAME);
    assert_eq!("Blanket AB", <(String, i32)>::COMPLEX_NAME);
    assert_eq!("Blanket B*", <(i32, String)>::COMPLEX_NAME);
}

其他更复杂的示例可以在tests中找到

依赖项

~325–780KB
~18K SLoC