Skip to content

Commit 9dcca68

Browse files
authored
Merge pull request RustPython#2012 from clemado1/develop
Implement remove method of array
2 parents 0c11b8b + 53a116f commit 9dcca68

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

vm/src/stdlib/array.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ macro_rules! def_array_enum {
105105
}
106106
}
107107

108+
fn remove(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()>{
109+
match self {
110+
$(ArrayContentType::$n(v) => {
111+
let val = $t::try_from_object(vm, obj)?;
112+
if let Some(pos) = v.iter().position(|&a| a == val) {
113+
v.remove(pos);
114+
} else {
115+
return Err(vm.new_value_error("array.remove(x): x not in array".to_owned()));
116+
}
117+
})*
118+
}
119+
Ok(())
120+
}
121+
108122
fn frombytes(&mut self, b: &[u8]) {
109123
match self {
110124
$(ArrayContentType::$n(v) => {
@@ -298,6 +312,11 @@ impl PyArray {
298312
self.borrow_value().count(x, vm)
299313
}
300314

315+
#[pymethod]
316+
fn remove(&self, x: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
317+
self.borrow_value_mut().remove(x, vm)
318+
}
319+
301320
#[pymethod]
302321
fn extend(&self, iter: PyIterable, vm: &VirtualMachine) -> PyResult<()> {
303322
let mut array = self.borrow_value_mut();

0 commit comments

Comments
 (0)