Initializer for PyList
@@ -12,8 +12,8 @@ use crate::{
sequence::{MutObjectSequenceOp, ObjectSequenceOp, SequenceMutOp, SequenceOp},
sliceable::{saturate_index, SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp},
types::{
- AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable,
- Iterable, PyComparisonOp, Unconstructible, Unhashable,
+ AsMapping, AsSequence, Comparable, Constructor, Hashable, Initializer, IterNext,
+ IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
utils::collection_repr,
vm::VirtualMachine,
@@ -89,7 +89,7 @@ pub(crate) struct SortOptions {
pub type PyListRef = PyRef<PyList>;
#[pyimpl(
- with(AsMapping, Iterable, Hashable, Comparable, AsSequence),
+ with(Initializer, AsMapping, Iterable, Hashable, Comparable, AsSequence),
flags(BASETYPE)
)]
impl PyList {
@@ -345,20 +345,6 @@ impl PyList {
.map(Into::into)
}
- #[pyslot]
- #[pymethod(magic)]
- fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
- let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
- let iterable: OptionalArg<PyObjectRef> = args.bind(vm)?;
- let mut elements = if let OptionalArg::Present(iterable) = iterable {
- iterable.try_to_value(vm)?
- } else {
- vec![]
- };
- std::mem::swap(zelf.borrow_vec_mut().deref_mut(), &mut elements);
- Ok(())
- }
-
#[pyclassmethod(magic)]
fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::new(cls, args, vm)
@@ -392,6 +378,20 @@ impl PyList {
};
}
+impl Initializer for PyList {
+ type Args = OptionalArg<PyObjectRef>;
+
+ fn init(zelf: PyRef<Self>, iterable: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
+ let mut elements = if let OptionalArg::Present(iterable) = iterable {
+ iterable.try_to_value(vm)?
+ } else {
+ vec![]
+ };
+ std::mem::swap(zelf.borrow_vec_mut().deref_mut(), &mut elements);
+ Ok(())
+ }
+}
+
impl AsMapping for PyList {
fn as_mapping(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
Self::MAPPING_METHODS
