arrays: add generic copy fn (#13677)

This commit is contained in:
Nick Treleaven 2022-03-08 07:44:04 +00:00 committed by GitHub
parent beb1b8ce1b
commit 17fcc788f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View file

@ -530,3 +530,15 @@ fn swap_nonoverlapping<T>(x_ &T, y_ &T, count int) {
memswap(x, y, len)
}
}
// copy copies the `src` array elements to the `dst` array.
// The number of the elements copied is the minimum of the length of both arrays.
// Returns the number of elements copied.
pub fn copy<T>(dst []T, src []T) int {
min := if dst.len < src.len { dst.len } else { src.len }
if min > 0 {
blen := min * int(sizeof(T))
unsafe { vmemmove(&T(dst.data), src.data, blen) }
}
return min
}