Add list.copy()

This commit is contained in:
Adrian Wielgosik
2019-02-13 00:21:42 +01:00
parent e5af4caecf
commit ecfc70ef99
2 changed files with 15 additions and 0 deletions

View File

@@ -219,6 +219,12 @@ fn list_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
}
fn list_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.list_type()))]);
let elements = get_elements(zelf);
Ok(vm.ctx.new_list(elements.clone()))
}
fn list_count(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
@@ -439,6 +445,7 @@ pub fn init(context: &PyContext) {
context.set_attr(&list_type, "__doc__", context.new_str(list_doc.to_string()));
context.set_attr(&list_type, "append", context.new_rustfunc(list_append));
context.set_attr(&list_type, "clear", context.new_rustfunc(list_clear));
context.set_attr(&list_type, "copy", context.new_rustfunc(list_copy));
context.set_attr(&list_type, "count", context.new_rustfunc(list_count));
context.set_attr(&list_type, "extend", context.new_rustfunc(list_extend));
context.set_attr(&list_type, "index", context.new_rustfunc(list_index));