Description
I want to construct a big array by concatenating smaller arrays returned by other functions. As a simple example:
pub fn g(f: &Fn() -> [u64;40]) -> [u64;80] {
let a = f();
let b = f();
let mut ret = [0u64;80];
ret[0..40].copy_from_slice(&a[..]);
ret[40..80].copy_from_slice(&b[..]);
ret
}
Rust nightly generates a call to memcpy.
Is there a way to prevent this memcpy? Am I missing obvious other way to write this function?
Of course one could rewrite the called function f
to take a &mut [u64]
instead of returning the array, but that removes compile-time checks on the length and introduces bounds checks. Using &mut [u64;40]
as an "out" argument solves that problem, but then I don't see a safe way to get two &mut [u64;40]
into [u64;80]
without using transmute
.
(Background: I'm implementing the XMSSMT hash-based signature in Rust, which involves concatenating lots of hashes. The usual Rust hash library returns an array (actually a GenericArray
) instead of using a &mut [u64;...]
parameter which led me to believe that the copy could be optimised away.)