arrays: move carray_to_varray from builtin, make it generic (#15503)

This commit is contained in:
Larpon 2022-08-23 10:12:50 +02:00 committed by GitHub
parent 2dde7ff5ba
commit 9dd8228f91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 12 deletions

View file

@ -570,3 +570,12 @@ fn can_copy_bits<T>() bool {
}
return false
}
// carray_to_varray copies a C byte array into a V array of type `T`.
// See also: `cstring_to_vstring`
[unsafe]
pub fn carray_to_varray<T>(c_array voidptr, c_array_len int) []T {
mut v_array := []T{len: c_array_len}
unsafe { vmemcpy(v_array.data, c_array, c_array_len * int(sizeof(T))) }
return v_array
}