WIP adding unique mutable case

This commit is contained in:
Dan Knutson 2021-10-03 13:54:47 -05:00
parent d1ee9c90b2
commit 2a72439117
2 changed files with 46 additions and 1 deletions

View File

@ -852,7 +852,33 @@ pub fn listDropAt(
dec(element);
}
const output = RocList.allocate(alignment, (size - 1), element_width);
// NOTE
// we need to return an empty list explicitly,
// because we rely on the pointer field being null if the list is empty
// which also requires duplicating the utils.decref call to spend the RC token
if (size < 2) {
utils.decref(list.bytes, size * element_width, alignment);
return RocList.empty();
}
if (list.isUnique()) {
var i = drop_index;
while (i < size) : (i += 1) {
const copy_target = source_ptr + i * element_width;
const copy_source = copy_target + element_width;
@memcpy(copy_target, copy_source, element_width);
}
var new_list = list;
new_list.length -= 1;
return new_list;
}
const stdout = std.io.getStdOut().writer();
stdout.print("Hit non-unique branch with list, {any}!\n", .{list}) catch unreachable;
const output = RocList.allocate(alignment, size - 1, element_width);
const target_ptr = output.bytes orelse unreachable;
const head_size = drop_index * element_width;

View File

@ -211,6 +211,25 @@ fn list_drop_at() {
RocList<i64>
);
assert_evals_to!("List.dropAt [] 1", RocList::from_slice(&[]), RocList<i64>);
assert_evals_to!("List.dropAt [0] 0", RocList::from_slice(&[]), RocList<i64>);
assert_evals_to!(
indoc!(
r#"
list : List I64
list = [ 1, 2, 3 ]
{ newList: List.dropAt list 0, original: list }
"#
),
(
// new_list
RocList::from_slice(&[2, 3]),
// original
RocList::from_slice(&[1, 2, 3]),
),
(RocList<i64>, RocList<i64>,)
);
}
#[test]