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

I found a beautiful gem of the most hideous code in the stdlib I've ever seen... and I cannot fully seem to justify it. #109006

Closed
mahmoud-moursy opened this issue Mar 10, 2023 · 4 comments

Comments

@mahmoud-moursy
Copy link
Contributor

I was meandering through the Rust library source code when I stumbled across this glorious gem:

// The Default impls cannot be done with const generics because `[T; 0]` doesn't
// require Default to be implemented, and having different impl blocks for
// different numbers isn't supported yet.

macro_rules! array_impl_default {
    {$n:expr, $t:ident $($ts:ident)*} => {
        #[stable(since = "1.4.0", feature = "array_default")]
        #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
        impl<T> const Default for [T; $n] where T: ~const Default {
            fn default() -> [T; $n] {
                [$t::default(), $($ts::default()),*]
            }
        }
        array_impl_default!{($n - 1), $($ts)*}
    };
    {$n:expr,} => {
        #[stable(since = "1.4.0", feature = "array_default")]
        #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
        impl<T> const Default for [T; $n] {
            fn default() -> [T; $n] { [] }
        }
    };
}

array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}

Can anyone please tell me what is so important about [T; 0]'s Default impl to the point that it warrants code as... well, I hate to say it but, vile and disgusting as this?

I found this diamond in the rough when I noticed that Default was only implemented for [T; N] up to [T; 32]; I instantly assumed code repetition (for which I was sort of right, ugly macro), and swooped in to save the day with const generics... or so I thought??

Is there a reason why core::array::from_fn cannot be simply be used? I personally do not believe that the performance benefit outweighs the flexibility and SEVERE code quality penalty. What are your thoughts, though?

And if whoever happens to be reviewing this issue sides with me on the matter, then can I just go ahead and get rid of this demon?

@mahmoud-moursy mahmoud-moursy changed the title I found a beautiful gem of the most hideous code I've ever seen... and I cannot fully seem to justify it. I found a beautiful gem of the most hideous code in the stdlib I've ever seen... and I cannot fully seem to justify it. Mar 10, 2023
@clubby789
Copy link
Contributor

can I just go ahead and get rid of this demon?

If you mean just removing the Default impl, this has been stabilised for a long time so can't be removed. Any replacement must also ensure [T; N] (where T: Default and 1 <= N <= 32) implements Default and that [T; 0] always implements Default

@Noratrieb
Copy link
Member

I've heard you like fun macros?

macro_rules! thread_local {

@memoryruins
Copy link
Contributor

Implementing array's Default with const generics is tracked in #61415. Previous attempts include #74254 and #84838.

@workingjubilee
Copy link
Member

You are welcome to take another swing at it, @T-O-R-U-S, if you find a way without breaking backwards compatibility.

But this issue is a duplicate. Closing!

@workingjubilee workingjubilee closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2023
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

5 participants