mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
sequence::cmp take DynPyIter instead of SimpleSeq
to avoid duplicated code generation for Box operation
This commit is contained in:
1 parent
826dc900f8
commit
b677b88bdd
4 files changed
+37
-52
No files matched your search
@@ -530,12 +530,12 @@ impl PyList {
|
||||
#[inline]
|
||||
fn cmp<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyResult<PyComparisonValue>
|
||||
where
|
||||
F: Fn(&Vec<PyObjectRef>, &Vec<PyObjectRef>) -> PyResult<bool>,
|
||||
F: Fn(sequence::DynPyIter, sequence::DynPyIter) -> PyResult<bool>,
|
||||
{
|
||||
let r = if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
|
||||
Implemented(op(
|
||||
&self.borrow_elements().clone(),
|
||||
&other.borrow_elements().clone(),
|
||||
self.borrow_elements().boxed_iter(),
|
||||
other.borrow_elements().boxed_iter(),
|
||||
)?)
|
||||
} else {
|
||||
NotImplemented
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::pyobject::{
|
||||
PyArithmaticValue::{self, *},
|
||||
PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
};
|
||||
use crate::sequence;
|
||||
use crate::sequence::{self, SimpleSeq};
|
||||
use crate::vm::{ReprGuard, VirtualMachine};
|
||||
|
||||
/// tuple() -> empty tuple
|
||||
@@ -81,10 +81,13 @@ impl PyTuple {
|
||||
#[inline]
|
||||
fn cmp<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyResult<PyComparisonValue>
|
||||
where
|
||||
F: Fn(&Vec<PyObjectRef>, &Vec<PyObjectRef>) -> PyResult<bool>,
|
||||
F: Fn(sequence::DynPyIter, sequence::DynPyIter) -> PyResult<bool>,
|
||||
{
|
||||
let r = if let Some(other) = other.payload_if_subclass::<PyTuple>(vm) {
|
||||
Implemented(op(&self.elements, &other.elements)?)
|
||||
Implemented(op(
|
||||
self.as_slice().boxed_iter(),
|
||||
other.as_slice().boxed_iter(),
|
||||
)?)
|
||||
} else {
|
||||
NotImplemented
|
||||
};
|
||||
@@ -116,8 +119,8 @@ impl PyTuple {
|
||||
if let Some(other) = other.payload_if_subclass::<PyTuple>(vm) {
|
||||
let elements: Vec<_> = self
|
||||
.elements
|
||||
.iter()
|
||||
.chain(other.as_slice().iter())
|
||||
.boxed_iter()
|
||||
.chain(other.as_slice().boxed_iter())
|
||||
.cloned()
|
||||
.collect();
|
||||
Implemented(elements.into())
|
||||
|
||||
+21
-39
@@ -7,7 +7,7 @@ pub(super) type DynPyIter<'a> = Box<dyn ExactSizeIterator<Item = &'a PyObjectRef
|
||||
#[allow(clippy::len_without_is_empty)]
|
||||
pub(crate) trait SimpleSeq {
|
||||
fn len(&self) -> usize;
|
||||
fn iter(&self) -> DynPyIter;
|
||||
fn boxed_iter(&self) -> DynPyIter;
|
||||
}
|
||||
|
||||
impl<'a, D> SimpleSeq for D
|
||||
@@ -18,18 +18,14 @@ where
|
||||
self.deref().len()
|
||||
}
|
||||
|
||||
fn iter(&self) -> DynPyIter {
|
||||
fn boxed_iter(&self) -> DynPyIter {
|
||||
Box::new(self.deref().iter())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn eq(
|
||||
vm: &VirtualMachine,
|
||||
zelf: &impl SimpleSeq,
|
||||
other: &impl SimpleSeq,
|
||||
) -> PyResult<bool> {
|
||||
pub(crate) fn eq(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
|
||||
if zelf.len() == other.len() {
|
||||
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
|
||||
for (a, b) in Iterator::zip(zelf, other) {
|
||||
if a.is(b) {
|
||||
continue;
|
||||
}
|
||||
@@ -43,58 +39,44 @@ pub(crate) fn eq(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn lt(
|
||||
vm: &VirtualMachine,
|
||||
zelf: &impl SimpleSeq,
|
||||
other: &impl SimpleSeq,
|
||||
) -> PyResult<bool> {
|
||||
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
|
||||
pub(crate) fn lt(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
|
||||
let fallback = zelf.len() < other.len();
|
||||
for (a, b) in Iterator::zip(zelf, other) {
|
||||
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
Ok(zelf.len() < other.len())
|
||||
Ok(fallback)
|
||||
}
|
||||
|
||||
pub(crate) fn gt(
|
||||
vm: &VirtualMachine,
|
||||
zelf: &impl SimpleSeq,
|
||||
other: &impl SimpleSeq,
|
||||
) -> PyResult<bool> {
|
||||
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
|
||||
pub(crate) fn gt(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
|
||||
let fallback = zelf.len() > other.len();
|
||||
for (a, b) in Iterator::zip(zelf, other) {
|
||||
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
Ok(zelf.len() > other.len())
|
||||
Ok(fallback)
|
||||
}
|
||||
|
||||
pub(crate) fn ge(
|
||||
vm: &VirtualMachine,
|
||||
zelf: &impl SimpleSeq,
|
||||
other: &impl SimpleSeq,
|
||||
) -> PyResult<bool> {
|
||||
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
|
||||
pub(crate) fn ge(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
|
||||
let fallback = zelf.len() >= other.len();
|
||||
for (a, b) in Iterator::zip(zelf, other) {
|
||||
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(zelf.len() >= other.len())
|
||||
Ok(fallback)
|
||||
}
|
||||
|
||||
pub(crate) fn le(
|
||||
vm: &VirtualMachine,
|
||||
zelf: &impl SimpleSeq,
|
||||
other: &impl SimpleSeq,
|
||||
) -> PyResult<bool> {
|
||||
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
|
||||
pub(crate) fn le(vm: &VirtualMachine, zelf: DynPyIter, other: DynPyIter) -> PyResult<bool> {
|
||||
let fallback = zelf.len() <= other.len();
|
||||
for (a, b) in Iterator::zip(zelf, other) {
|
||||
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
|
||||
return Ok(v);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(zelf.len() <= other.len())
|
||||
Ok(fallback)
|
||||
}
|
||||
|
||||
pub(crate) struct SeqMul<'a> {
|
||||
@@ -115,7 +97,7 @@ impl<'a> Iterator for SeqMul<'a> {
|
||||
None
|
||||
} else {
|
||||
self.repetitions -= 1;
|
||||
self.iter = Some(self.seq.iter());
|
||||
self.iter = Some(self.seq.boxed_iter());
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ mod _collections {
|
||||
IdProtocol, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyIterable, PyObjectRef,
|
||||
PyRef, PyResult, PyValue,
|
||||
};
|
||||
use crate::sequence;
|
||||
use crate::sequence::{self, SimpleSeq};
|
||||
use crate::vm::ReprGuard;
|
||||
use crate::VirtualMachine;
|
||||
use itertools::Itertools;
|
||||
@@ -54,7 +54,7 @@ mod _collections {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
fn iter(&self) -> sequence::DynPyIter {
|
||||
fn boxed_iter(&self) -> sequence::DynPyIter {
|
||||
Box::new(self.0.iter())
|
||||
}
|
||||
}
|
||||
@@ -285,12 +285,12 @@ mod _collections {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyComparisonValue>
|
||||
where
|
||||
F: Fn(&SimpleSeqDeque, &SimpleSeqDeque) -> PyResult<bool>,
|
||||
F: Fn(sequence::DynPyIter, sequence::DynPyIter) -> PyResult<bool>,
|
||||
{
|
||||
let r = if let Some(other) = other.payload_if_subclass::<PyDeque>(vm) {
|
||||
Implemented(op(
|
||||
&self.borrow_deque().into(),
|
||||
&other.borrow_deque().into(),
|
||||
SimpleSeqDeque::from(self.borrow_deque()).boxed_iter(),
|
||||
SimpleSeqDeque::from(other.borrow_deque()).boxed_iter(),
|
||||
)?)
|
||||
} else {
|
||||
NotImplemented
|
||||
|
||||
Reference in new issue
Block a user