mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #4466 from moreal/array-contains
Implement `array.array.__contains__`
This commit is contained in:
@@ -112,3 +112,8 @@ assert str(a.__class__.__name__) == "array"
|
||||
# test arrayiterator name
|
||||
i = iter(a)
|
||||
assert str(i.__class__.__name__) == "arrayiterator"
|
||||
|
||||
# teset array.__contains__
|
||||
a = array('B', [0])
|
||||
assert a.__contains__(0)
|
||||
assert not a.__contains__(1)
|
||||
|
||||
@@ -1160,6 +1160,23 @@ mod array {
|
||||
zelf.as_object().dict(),
|
||||
))
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, value: PyObjectRef, vm: &VirtualMachine) -> bool {
|
||||
let array = self.array.read();
|
||||
for element in array
|
||||
.iter(vm)
|
||||
.map(|x| x.expect("Expected to be checked by array.len() and read lock."))
|
||||
{
|
||||
if let Ok(true) =
|
||||
element.rich_compare_bool(value.as_object(), PyComparisonOp::Eq, vm)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl Comparable for PyArray {
|
||||
|
||||
Reference in New Issue
Block a user