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

Add #[default] macro (or different name) #7

Open
AldaronLau opened this issue Sep 8, 2023 · 0 comments
Open

Add #[default] macro (or different name) #7

AldaronLau opened this issue Sep 8, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@AldaronLau
Copy link
Member

AldaronLau commented Sep 8, 2023

An attribute macro to create a separate trait that can set default values for associated items, and default blocks for methods and associated functions. Everything that implements the "default" trait will implement the trait.

use traitful::default;

#[default(
    TraitDefault => Default::default(),
    TraitUnimplemented => unimplemented!(),
)]
pub trait Trait {
    fn uwu() -> i32;
}

// becomes:

pub trait Trait {
    fn uwu() -> i32;
}

pub trait TraitDefault {
    fn uwu() -> i32 {
        Default::default()
    }
}

pub trait TraitUnimplemented {
    fn uwu() -> i32 {
        unimplemented!()
    }
}

mod traitful_default__ {
    use super::*;

    impl<T: TraitDefault> Trait for T {
        fn uwu() -> i32 {
            <Self as TraitDefault>::uwu()
        }
    }

    impl<T: TraitUnimplemented> TraitDefault for T {
        fn uwu() -> i32 {
            <Self as TraitUnimplemented>::uwu()
        }
    }
}
@AldaronLau AldaronLau added the enhancement New feature or request label Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant