Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freestanding associated types #4

Open
mversic opened this issue Jan 8, 2024 · 0 comments
Open

Freestanding associated types #4

mversic opened this issue Jan 8, 2024 · 0 comments

Comments

@mversic
Copy link
Owner

mversic commented Jan 8, 2024

Add support for freestanding associated types, i.e. types that don't comprise id like here:

use disjoint_impls::disjoint_impls;

pub trait Dispatch {
    type Group;
}

pub enum GroupA {}
impl Dispatch for (u16, u32) {
    type Group = GroupA;
}
impl Dispatch for (i16, i32) {
    type Group = GroupA;
}

pub enum GroupB {}
impl Dispatch for (i32, String) {
    type Group = GroupB;
}
impl Dispatch for (u32, String) {
    type Group = GroupB;
}

trait A {
    type B;
}

disjoint_impls! {
    pub trait Kita<U: A<B = u32>> {
        const NAME: &'static str;
    }

    impl<T, U, C> Kita<(U, C)> for T where (U, C): Dispatch<Group = GroupA> + A<B = u32> {
        const NAME: &'static str = "1st blanket A";
    }
    impl<T, U, C> Kita<(U, C)> for T where (U, C): Dispatch<Group = GroupB> + A<B = u32> {
        const NAME: &'static str = "1st blanket B";
    }

    impl<T: Dispatch<Group = GroupA>> Kita<(i32,)> for T {
        const NAME: &'static str = "2nd blanket A";
    }
    impl<T: Dispatch<Group = GroupB>> Kita<(i32,)> for T {
        const NAME: &'static str = "2nd blanket B";
    }
}

impl A for (i32,) {
    type B = u32;
}

fn main() {
    assert_eq!("Blanket A", <u32 as Kita<(u16, u32)>>::NAME);
}

This is associated type in trait definition. What if associated type is in the impl?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant