You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>constDefaultfor[T; $n]whereT:~constDefault{fndefault()->[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>constDefaultfor[T; $n]{fndefault()->[T; $n]{[]}}};}array_impl_default!{32,TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT}
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?
The text was updated successfully, but these errors were encountered:
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
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
I was meandering through the Rust library source code when I stumbled across this glorious gem:
Can anyone please tell me what is so important about
[T; 0]
'sDefault
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?
The text was updated successfully, but these errors were encountered: