array: fix multiple array clone
parent
8a5ca4cbdc
commit
dc8b82e9a4
|
@ -282,7 +282,17 @@ pub fn (a &array) clone() array {
|
|||
len: a.len
|
||||
cap: a.cap
|
||||
}
|
||||
C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)
|
||||
// Recursively clone-generated elements if array element is array type
|
||||
if a.element_size == sizeof(array) {
|
||||
for i in 0..a.len {
|
||||
ar := array{}
|
||||
C.memcpy(&ar, byteptr(a.data) + i * a.element_size, sizeof(array))
|
||||
ar_clone := ar.clone()
|
||||
C.memcpy(byteptr(arr.data) + i * a.element_size, &ar_clone, a.element_size)
|
||||
}
|
||||
} else {
|
||||
C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
|
|
|
@ -361,6 +361,16 @@ fn test_clone() {
|
|||
assert nums.slice(1, 3).str() == '[2, 3]'
|
||||
}
|
||||
|
||||
fn test_mutli_array_clone() {
|
||||
mut array1 := [[1, 2, 3], [4, 5, 6]]
|
||||
mut array2 := array1.clone()
|
||||
|
||||
array1[0][1] = 0
|
||||
|
||||
assert array1 == [[1, 0, 3], [4, 5, 6]]
|
||||
assert array2 == [[1, 2, 3], [4, 5, 6]]
|
||||
}
|
||||
|
||||
fn test_doubling() {
|
||||
mut nums := [1, 2, 3, 4, 5]
|
||||
for i in 0..nums.len {
|
||||
|
|
Loading…
Reference in New Issue