mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
287 lines
8.3 KiB
Rust
287 lines
8.3 KiB
Rust
use std::cell::Cell;
|
|
use std::fmt;
|
|
|
|
use super::objiter;
|
|
use super::objsequence::{get_item, seq_equal, seq_ge, seq_gt, seq_le, seq_lt, seq_mul};
|
|
use super::objtype::PyClassRef;
|
|
use crate::function::OptionalArg;
|
|
use crate::pyhash;
|
|
use crate::pyobject::{
|
|
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
|
|
};
|
|
use crate::sequence::PySequenceContainer;
|
|
use crate::vm::{ReprGuard, VirtualMachine};
|
|
|
|
/// tuple() -> empty tuple
|
|
/// tuple(iterable) -> tuple initialized from iterable's items
|
|
///
|
|
/// If the argument is a tuple, the return value is the same object.
|
|
#[pyclass]
|
|
pub struct PyTuple {
|
|
elements: Vec<PyObjectRef>,
|
|
}
|
|
|
|
impl fmt::Debug for PyTuple {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
// TODO: implement more informational, non-recursive Debug formatter
|
|
f.write_str("tuple")
|
|
}
|
|
}
|
|
|
|
impl From<Vec<PyObjectRef>> for PyTuple {
|
|
fn from(elements: Vec<PyObjectRef>) -> Self {
|
|
PyTuple { elements }
|
|
}
|
|
}
|
|
|
|
impl PyValue for PyTuple {
|
|
fn class(vm: &VirtualMachine) -> PyClassRef {
|
|
vm.ctx.tuple_type()
|
|
}
|
|
}
|
|
|
|
macro_rules! impl_intopyobj_tuple {
|
|
($(($T:ident, $idx:tt)),+) => {
|
|
impl<$($T: IntoPyObject),*> IntoPyObject for ($($T,)*) {
|
|
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
|
|
Ok(vm.ctx.new_tuple(vec![$(self.$idx.into_pyobject(vm)?),*]))
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl_intopyobj_tuple!((A, 0));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5));
|
|
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5), (G, 6));
|
|
|
|
impl PySequenceContainer for PyTuple {
|
|
fn as_slice(&self) -> &[PyObjectRef] {
|
|
&self.elements
|
|
}
|
|
}
|
|
|
|
impl PyTuple {
|
|
pub fn fast_getitem(&self, idx: usize) -> PyObjectRef {
|
|
self.elements[idx].clone()
|
|
}
|
|
|
|
pub fn as_slice(&self) -> &[PyObjectRef] {
|
|
<PyTuple as PySequenceContainer>::as_slice(self)
|
|
}
|
|
}
|
|
|
|
pub type PyTupleRef = PyRef<PyTuple>;
|
|
|
|
pub fn get_value(obj: &PyObjectRef) -> &[PyObjectRef] {
|
|
obj.payload::<PyTuple>().unwrap().as_slice()
|
|
}
|
|
|
|
#[pyimpl]
|
|
impl PyTuple {
|
|
#[inline]
|
|
fn cmp<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyResult
|
|
where
|
|
F: Fn(&[PyObjectRef], &[PyObjectRef]) -> PyResult<bool>,
|
|
{
|
|
let r = if let Some(other) = other.payload_if_subclass::<PyTuple>(vm) {
|
|
vm.new_bool(op(self.as_slice(), other.as_slice())?)
|
|
} else {
|
|
vm.ctx.not_implemented()
|
|
};
|
|
Ok(r)
|
|
}
|
|
|
|
#[pymethod(name = "__lt__")]
|
|
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| seq_lt(vm, &a, &b), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__gt__")]
|
|
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| seq_gt(vm, &a, &b), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__ge__")]
|
|
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| seq_ge(vm, &a, &b), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__le__")]
|
|
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| seq_le(vm, &a, &b), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__add__")]
|
|
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
if let Some(other) = other.payload_if_subclass::<PyTuple>(vm) {
|
|
let elements = self
|
|
.elements
|
|
.iter()
|
|
.chain(other.as_slice().iter())
|
|
.cloned()
|
|
.collect();
|
|
Ok(vm.ctx.new_tuple(elements))
|
|
} else {
|
|
Ok(vm.ctx.not_implemented())
|
|
}
|
|
}
|
|
|
|
#[pymethod(name = "__bool__")]
|
|
fn bool(&self, _vm: &VirtualMachine) -> bool {
|
|
!self.elements.is_empty()
|
|
}
|
|
|
|
#[pymethod(name = "count")]
|
|
fn count(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
|
|
let mut count: usize = 0;
|
|
for element in self.elements.iter() {
|
|
if vm.identical_or_equal(element, &needle)? {
|
|
count += 1;
|
|
}
|
|
}
|
|
Ok(count)
|
|
}
|
|
|
|
#[pymethod(name = "__eq__")]
|
|
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| seq_equal(vm, &a, &b), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__ne__")]
|
|
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
self.cmp(other, |a, b| Ok(!seq_equal(vm, &a, &b)?), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__hash__")]
|
|
fn hash(&self, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
|
|
pyhash::hash_iter(self.elements.iter(), vm)
|
|
}
|
|
|
|
#[pymethod(name = "__iter__")]
|
|
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyTupleIterator {
|
|
PyTupleIterator {
|
|
position: Cell::new(0),
|
|
tuple: zelf,
|
|
}
|
|
}
|
|
|
|
#[pymethod(name = "__len__")]
|
|
fn len(&self, _vm: &VirtualMachine) -> usize {
|
|
self.elements.len()
|
|
}
|
|
|
|
#[pymethod(name = "__repr__")]
|
|
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
|
let s = if let Some(_guard) = ReprGuard::enter(zelf.as_object()) {
|
|
let mut str_parts = Vec::with_capacity(zelf.elements.len());
|
|
for elem in zelf.elements.iter() {
|
|
let s = vm.to_repr(elem)?;
|
|
str_parts.push(s.as_str().to_owned());
|
|
}
|
|
|
|
if str_parts.len() == 1 {
|
|
format!("({},)", str_parts[0])
|
|
} else {
|
|
format!("({})", str_parts.join(", "))
|
|
}
|
|
} else {
|
|
"(...)".to_string()
|
|
};
|
|
Ok(s)
|
|
}
|
|
|
|
#[pymethod(name = "__mul__")]
|
|
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
|
|
let new_elements = seq_mul(&self.as_slice(), counter).cloned().collect();
|
|
vm.ctx.new_tuple(new_elements)
|
|
}
|
|
|
|
#[pymethod(name = "__rmul__")]
|
|
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
|
|
self.mul(counter, vm)
|
|
}
|
|
|
|
#[pymethod(name = "__getitem__")]
|
|
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
|
get_item(vm, zelf.as_object(), &zelf.elements, needle.clone())
|
|
}
|
|
|
|
#[pymethod(name = "index")]
|
|
fn index(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
|
|
for (index, element) in self.elements.iter().enumerate() {
|
|
if vm.identical_or_equal(element, &needle)? {
|
|
return Ok(index);
|
|
}
|
|
}
|
|
Err(vm.new_value_error("tuple.index(x): x not in tuple".to_string()))
|
|
}
|
|
|
|
#[pymethod(name = "__contains__")]
|
|
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
|
for element in self.elements.iter() {
|
|
if vm.identical_or_equal(element, &needle)? {
|
|
return Ok(true);
|
|
}
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
#[pyslot(new)]
|
|
fn tuple_new(
|
|
cls: PyClassRef,
|
|
iterable: OptionalArg<PyObjectRef>,
|
|
vm: &VirtualMachine,
|
|
) -> PyResult<PyTupleRef> {
|
|
let elements = if let OptionalArg::Present(iterable) = iterable {
|
|
vm.extract_elements(&iterable)?
|
|
} else {
|
|
vec![]
|
|
};
|
|
|
|
PyTuple::from(elements).into_ref_with_type(vm, cls)
|
|
}
|
|
}
|
|
|
|
#[pyclass]
|
|
#[derive(Debug)]
|
|
pub struct PyTupleIterator {
|
|
position: Cell<usize>,
|
|
tuple: PyTupleRef,
|
|
}
|
|
|
|
impl PyValue for PyTupleIterator {
|
|
fn class(vm: &VirtualMachine) -> PyClassRef {
|
|
vm.ctx.tupleiterator_type()
|
|
}
|
|
}
|
|
|
|
#[pyimpl]
|
|
impl PyTupleIterator {
|
|
#[pymethod(name = "__next__")]
|
|
fn next(&self, vm: &VirtualMachine) -> PyResult {
|
|
if self.position.get() < self.tuple.as_slice().len() {
|
|
let ret = self.tuple.as_slice()[self.position.get()].clone();
|
|
self.position.set(self.position.get() + 1);
|
|
Ok(ret)
|
|
} else {
|
|
Err(objiter::new_stop_iteration(vm))
|
|
}
|
|
}
|
|
|
|
#[pymethod(name = "__iter__")]
|
|
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
|
|
zelf
|
|
}
|
|
}
|
|
|
|
pub fn init(context: &PyContext) {
|
|
let tuple_type = &context.types.tuple_type;
|
|
PyTuple::extend_class(context, tuple_type);
|
|
|
|
PyTupleIterator::extend_class(context, &context.types.tupleiterator_type);
|
|
}
|