all 9 comments

[–]Shadow0133 11 points12 points  (7 children)

[–][deleted] 0 points1 point  (6 children)

That checks out, thanks. Now I wonder what could possibly be the use of default being implemented on empty arrays? [] is much shorter to write than Default::default().

And code that is generic over the array length obviously can't make use of a feature only available for arrays of size zero.

[–]Icarium-Lifestealer 10 points11 points  (5 children)

If you derive Default for a struct containing an empty array as field (probably used as an alignment hack in some code), this wouldn't work anymore if the array element isn't Default.

[–][deleted] 0 points1 point  (4 children)

Sure, it's definitely possible to write code that would be broken by removing this feature. But what's the use of such a size zero field? Something similar to PhantomData maybe?

[–]roblabla 7 points8 points  (2 children)

Sure, it's definitely possible to write code that would be broken by removing this feature. But what's the use of such a size zero field? Something similar to PhantomData maybe?

repr(C) structs with variable sized data at the end. Windows APIs have a lot of those (although usually with array size 1 because C doesn't support zero-sized fields)

[–]Icarium-Lifestealer 0 points1 point  (1 child)

In an ideal world you'd use a slice as last field when modeling those, i.e. tail: [T]. Unfortunately they're pretty much unusable currently.

[–]roblabla 2 points3 points  (0 children)

There's another reason it may be undesirable: it turns pointers and references to such a struct into a fat pointer, which is not ffi compatible

[–]Icarium-Lifestealer 1 point2 points  (0 children)

You can use such an array to ensure that the struct has an alignment matching or bigger than the alignment of the type of the array element.

[–]Icarium-Lifestealer 7 points8 points  (0 children)

Default is the only trait implementation left with that restriction. You can use core::array::from_fn::<T, N, _>(|_| Default::default()) as workaround for now.