From e9c76915ac037cb1a458e9748eee5463bee9e7cc Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Wed, 27 Apr 2022 01:47:16 +0900 Subject: [PATCH] Initializer and Constructor for PySet --- vm/src/builtins/set.rs | 52 +++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index 2db3c7bcf..9295e39ec 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -13,8 +13,8 @@ use crate::{ protocol::{PyIterReturn, PySequenceMethods}, recursion::ReprGuard, types::{ - AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, - PyComparisonOp, Unconstructible, Unhashable, + AsSequence, Comparable, Constructor, Hashable, Initializer, IterNext, IterNextIterable, + Iterable, PyComparisonOp, Unconstructible, Unhashable, }, utils::collection_repr, vm::VirtualMachine, @@ -394,27 +394,11 @@ impl PySet { } } -#[pyimpl(with(AsSequence, Hashable, Comparable, Iterable), flags(BASETYPE))] +#[pyimpl( + with(Constructor, Initializer, AsSequence, Hashable, Comparable, Iterable), + flags(BASETYPE) +)] impl PySet { - #[pyslot] - fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - PySet::default().into_ref_with_type(vm, cls).map(Into::into) - } - - #[pyslot] - #[pymethod(magic)] - fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> { - let zelf: PyRef = zelf.try_into_value(vm)?; - let iterable: OptionalArg = args.bind(vm)?; - if zelf.len() > 0 { - zelf.clear(); - } - if let OptionalArg::Present(it) = iterable { - zelf.update(PosArgs::new(vec![it]), vm)?; - } - Ok(()) - } - #[pymethod(magic)] fn len(&self) -> usize { self.inner.len() @@ -658,6 +642,28 @@ impl PySet { } } +impl Constructor for PySet { + type Args = FuncArgs; + + fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { + PySet::default().into_ref_with_type(vm, cls).map(Into::into) + } +} + +impl Initializer for PySet { + type Args = OptionalArg; + + fn init(zelf: PyRef, iterable: Self::Args, vm: &VirtualMachine) -> PyResult<()> { + if zelf.len() > 0 { + zelf.clear(); + } + if let OptionalArg::Present(it) = iterable { + zelf.update(PosArgs::new(vec![it]), vm)?; + } + Ok(()) + } +} + impl AsSequence for PySet { fn as_sequence( _zelf: &crate::Py, @@ -736,7 +742,7 @@ impl Constructor for PyFrozenSet { #[pyimpl( flags(BASETYPE), - with(AsSequence, Hashable, Comparable, Iterable, Constructor) + with(Constructor, AsSequence, Hashable, Comparable, Iterable) )] impl PyFrozenSet { // Also used by ssl.rs windows.