mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
arrays: add generic copy fn (#13677)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user