Please add a cloning version
Please add a cloning version of the array macro: The current version copy-pastes the given expr N times, which is slower than cloning when using it with an expr that's slower to compute than cloning the constructed object. What I'm suggesting is something like this (but more panic-safe with MaybeUninit, Drop etc.):
#[macro_export]
macro_rules! array_clone {
($item:expr; $n:expr) => (unsafe {
let mut a: [_; $n] = ::std::mem::zeroed();
let elem = $item;
for x in &mut a[..] {
::std::ptr::write(x, elem.clone());
}
a
})
}
I know, this can be emulated with exprs like {let foo = heavy_constructor(); array![foo; N]}
but it's less readable,
especially when the intended use-case is in combinations with derive-new
's #[new(value = "..")]
and smart-default
's #[default = ".."]
.
This is the most common use-case for me, for using this macro, and I'd really appreciate a cloning version.