diff --git a/tests/snippets/set.py b/tests/snippets/set.py index 9e49dff89e..0117163350 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -205,6 +205,13 @@ def __hash__(self): assert_raises(TypeError, lambda: frozenset([[]])) +a = frozenset([1,2,3]) +b = set() +for e in a: + assert e == 1 or e == 2 or e == 3 + b.add(e) +assert a == b + # set and frozen set assert frozenset([1,2,3]).union(set([4,5])) == frozenset([1,2,3,4,5]) assert set([1,2,3]).union(frozenset([4,5])) == set([1,2,3,4,5]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index ad82c34de0..fef06fd682 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -7,7 +7,7 @@ use std::collections::{hash_map::DefaultHasher, HashMap}; use std::fmt; use std::hash::{Hash, Hasher}; -use crate::function::{OptionalArg, PyFuncArgs}; +use crate::function::OptionalArg; use crate::pyobject::{ PyContext, PyIteratorValue, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; @@ -57,6 +57,336 @@ impl PyValue for PyFrozenSet { } } +trait SetProtocol +where + Self: Sized, +{ + fn get_elements(&self) -> HashMap; + fn as_object(&self) -> &PyObjectRef; + fn create(&self, vm: &VirtualMachine, elements: HashMap) -> PyResult; + fn name(&self) -> &str; + fn len(self, _vm: &VirtualMachine) -> usize { + self.get_elements().len() + } + fn copy(self, vm: &VirtualMachine) -> PyResult { + self.create(vm, self.get_elements()) + } + fn contains(self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { + for element in self.get_elements().iter() { + match vm._eq(needle.clone(), element.1.clone()) { + Ok(value) => { + if objbool::get_value(&value) { + return Ok(vm.new_bool(true)); + } + } + Err(_) => return Err(vm.new_type_error("".to_string())), + } + } + Ok(vm.new_bool(false)) + } + + fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + set_compare_inner( + vm, + self.as_object(), + &other, + &|zelf: usize, other: usize| -> bool { zelf != other }, + false, + ) + } + + fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + set_compare_inner( + vm, + self.as_object(), + &other, + &|zelf: usize, other: usize| -> bool { zelf < other }, + false, + ) + } + + fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + set_compare_inner( + vm, + self.as_object(), + &other, + &|zelf: usize, other: usize| -> bool { zelf <= other }, + false, + ) + } + + fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + set_compare_inner( + vm, + self.as_object(), + &other, + &|zelf: usize, other: usize| -> bool { zelf < other }, + true, + ) + } + + fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + set_compare_inner( + vm, + self.as_object(), + &other, + &|zelf: usize, other: usize| -> bool { zelf <= other }, + true, + ) + } + + fn union(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + validate_set_or_frozenset(vm, other.class())?; + + let mut elements = self.get_elements().clone(); + elements.extend(get_elements(&other).clone()); + + self.create(vm, elements) + } + + fn intersection(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_inner(&other, vm, SetCombineOperation::Intersection) + } + + fn difference(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_inner(&other, vm, SetCombineOperation::Difference) + } + + fn combine_inner( + self, + other: &PyObjectRef, + vm: &VirtualMachine, + op: SetCombineOperation, + ) -> PyResult { + validate_set_or_frozenset(vm, other.class())?; + let mut elements = HashMap::new(); + + for element in self.get_elements().iter() { + let value = vm.call_method(other, "__contains__", vec![element.1.clone()])?; + let should_add = match op { + SetCombineOperation::Intersection => objbool::get_value(&value), + SetCombineOperation::Difference => !objbool::get_value(&value), + }; + if should_add { + elements.insert(element.0.clone(), element.1.clone()); + } + } + + self.create(vm, elements) + } + + fn symmetric_difference(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + validate_set_or_frozenset(vm, other.class())?; + let mut elements = HashMap::new(); + + for element in self.get_elements().iter() { + let value = vm.call_method(&other, "__contains__", vec![element.1.clone()])?; + if !objbool::get_value(&value) { + elements.insert(element.0.clone(), element.1.clone()); + } + } + + for element in get_elements(&other).iter() { + let value = + vm.call_method(self.as_object(), "__contains__", vec![element.1.clone()])?; + if !objbool::get_value(&value) { + elements.insert(element.0.clone(), element.1.clone()); + } + } + + self.create(vm, elements) + } + + fn iter(self, vm: &VirtualMachine) -> PyIteratorValue { + let items = self.get_elements().values().cloned().collect(); + let set_list = vm.ctx.new_list(items); + PyIteratorValue { + position: Cell::new(0), + iterated_obj: set_list, + } + } + + fn repr(self, vm: &VirtualMachine) -> PyResult { + let elements = self.get_elements(); + let s = if elements.is_empty() { + format!("{}()", self.name()) + } else if let Some(_guard) = ReprGuard::enter(self.as_object()) { + let mut str_parts = vec![]; + for elem in elements.values() { + let part = vm.to_repr(elem)?; + str_parts.push(part.value.clone()); + } + + format!("{{{}}}", str_parts.join(", ")) + } else { + format!("{}(...)", self.name()) + }; + Ok(vm.new_str(s)) + } +} + +impl SetProtocol for PySetRef { + fn get_elements(&self) -> HashMap { + self.elements.borrow().clone() + } + fn create(&self, vm: &VirtualMachine, elements: HashMap) -> PyResult { + Ok(PyObject::new( + PySet { + elements: RefCell::new(elements), + }, + PySet::class(vm), + None, + )) + } + fn as_object(&self) -> &PyObjectRef { + self.as_object() + } + fn name(&self) -> &str { + "set" + } +} + +impl SetProtocol for PyFrozenSetRef { + fn get_elements(&self) -> HashMap { + self.elements.clone() + } + fn create(&self, vm: &VirtualMachine, elements: HashMap) -> PyResult { + Ok(PyObject::new( + PyFrozenSet { elements: elements }, + PyFrozenSet::class(vm), + None, + )) + } + fn as_object(&self) -> &PyObjectRef { + self.as_object() + } + fn name(&self) -> &str { + "frozenset" + } +} + +impl PySetRef { + fn add(self, item: PyObjectRef, vm: &VirtualMachine) -> PyResult { + insert_into_set(vm, &mut self.elements.borrow_mut(), &item) + } + + fn remove(self, item: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn remove( + vm: &VirtualMachine, + elements: &mut HashMap, + key: u64, + value: &PyObjectRef, + ) -> PyResult { + match elements.remove(&key) { + None => { + let item_str = format!("{:?}", value); + Err(vm.new_key_error(item_str)) + } + Some(_) => Ok(vm.get_none()), + } + } + perform_action_with_hash(vm, &mut self.elements.borrow_mut(), &item, &remove) + } + + fn discard(self, item: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn discard( + vm: &VirtualMachine, + elements: &mut HashMap, + key: u64, + _value: &PyObjectRef, + ) -> PyResult { + elements.remove(&key); + Ok(vm.get_none()) + } + perform_action_with_hash(vm, &mut self.elements.borrow_mut(), &item, &discard) + } + + fn clear(self, vm: &VirtualMachine) -> PyResult { + self.elements.borrow_mut().clear(); + Ok(vm.get_none()) + } + + fn pop(self, vm: &VirtualMachine) -> PyResult { + let mut elements = self.elements.borrow_mut(); + match elements.clone().keys().next() { + Some(key) => Ok(elements.remove(key).unwrap()), + None => Err(vm.new_key_error("pop from an empty set".to_string())), + } + } + + fn ior(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let iterator = objiter::get_iter(vm, &iterable)?; + while let Ok(v) = vm.call_method(&iterator, "__next__", vec![]) { + insert_into_set(vm, &mut self.elements.borrow_mut(), &v)?; + } + Ok(self.as_object().clone()) + } + + fn update(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.ior(iterable, vm)?; + Ok(vm.get_none()) + } + + fn intersection_update(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_update_inner(&iterable, vm, SetCombineOperation::Intersection)?; + Ok(vm.get_none()) + } + + fn iand(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_update_inner(&iterable, vm, SetCombineOperation::Intersection) + } + + fn difference_update(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_update_inner(&iterable, vm, SetCombineOperation::Difference)?; + Ok(vm.get_none()) + } + + fn isub(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.combine_update_inner(&iterable, vm, SetCombineOperation::Difference) + } + + fn combine_update_inner( + self, + iterable: &PyObjectRef, + vm: &VirtualMachine, + op: SetCombineOperation, + ) -> PyResult { + let mut elements = self.elements.borrow_mut(); + for element in elements.clone().iter() { + let value = vm.call_method(iterable, "__contains__", vec![element.1.clone()])?; + let should_remove = match op { + SetCombineOperation::Intersection => !objbool::get_value(&value), + SetCombineOperation::Difference => objbool::get_value(&value), + }; + if should_remove { + elements.remove(&element.0.clone()); + } + } + Ok(self.as_object().clone()) + } + + fn symmetric_difference_update(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.ixor(iterable, vm)?; + Ok(vm.get_none()) + } + + fn ixor(self, iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let elements_original = self.elements.borrow().clone(); + let iterator = objiter::get_iter(vm, &iterable)?; + while let Ok(v) = vm.call_method(&iterator, "__next__", vec![]) { + insert_into_set(vm, &mut self.elements.borrow_mut(), &v)?; + } + for element in elements_original.iter() { + let value = vm.call_method(&iterable, "__contains__", vec![element.1.clone()])?; + if objbool::get_value(&value) { + self.elements.borrow_mut().remove(&element.0.clone()); + } + } + + Ok(self.as_object().clone()) + } +} + pub fn get_elements(obj: &PyObjectRef) -> HashMap { if let Some(set) = obj.payload::() { return set.elements.borrow().clone(); @@ -131,84 +461,6 @@ fn insert_into_set( perform_action_with_hash(vm, elements, item, &insert) } -fn set_add(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - trace!("set.add called with: {:?}", args); - arg_check!( - vm, - args, - required = [(zelf, Some(vm.ctx.set_type())), (item, None)] - ); - match zelf.payload::() { - Some(set) => insert_into_set(vm, &mut set.elements.borrow_mut(), item), - _ => Err(vm.new_type_error("set.add is called with no item".to_string())), - } -} - -fn set_remove(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - trace!("set.remove called with: {:?}", args); - arg_check!( - vm, - args, - required = [(s, Some(vm.ctx.set_type())), (item, None)] - ); - match s.payload::() { - Some(set) => { - fn remove( - vm: &VirtualMachine, - elements: &mut HashMap, - key: u64, - value: &PyObjectRef, - ) -> PyResult { - match elements.remove(&key) { - None => { - let item_str = format!("{:?}", value); - Err(vm.new_key_error(item_str)) - } - Some(_) => Ok(vm.get_none()), - } - } - perform_action_with_hash(vm, &mut set.elements.borrow_mut(), item, &remove) - } - _ => Err(vm.new_type_error("set.remove is called with no item".to_string())), - } -} - -fn set_discard(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - trace!("set.discard called with: {:?}", args); - arg_check!( - vm, - args, - required = [(s, Some(vm.ctx.set_type())), (item, None)] - ); - match s.payload::() { - Some(set) => { - fn discard( - vm: &VirtualMachine, - elements: &mut HashMap, - key: u64, - _value: &PyObjectRef, - ) -> PyResult { - elements.remove(&key); - Ok(vm.get_none()) - } - perform_action_with_hash(vm, &mut set.elements.borrow_mut(), item, &discard) - } - None => Err(vm.new_type_error("set.discard is called with no item".to_string())), - } -} - -fn set_clear(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - trace!("set.clear called"); - arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]); - match s.payload::() { - Some(set) => { - set.elements.borrow_mut().clear(); - Ok(vm.get_none()) - } - None => Err(vm.new_type_error("".to_string())), - } -} - /* Create a new object of sub-type of set */ fn set_new(cls: PyClassRef, iterable: OptionalArg, vm: &VirtualMachine) -> PyResult { validate_set_or_frozenset(vm, cls.clone())?; @@ -228,111 +480,13 @@ fn set_new(cls: PyClassRef, iterable: OptionalArg, vm: &VirtualMach create_set(vm, elements, cls.clone()) } -fn set_len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - trace!("set.len called with: {:?}", args); - arg_check!(vm, args, required = [(s, None)]); - validate_set_or_frozenset(vm, s.class())?; - let elements = get_elements(s); - Ok(vm.context().new_int(elements.len())) -} - -fn set_copy(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { - trace!("set.copy called with: {:?}", obj); - validate_set_or_frozenset(vm, obj.class())?; - let elements = get_elements(&obj).clone(); - create_set(vm, elements, obj.class()) -} - -fn set_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args, required = [(o, Some(vm.ctx.set_type()))]); - - let elements = get_elements(o); - let s = if elements.is_empty() { - "set()".to_string() - } else if let Some(_guard) = ReprGuard::enter(o) { - let mut str_parts = vec![]; - for elem in elements.values() { - let part = vm.to_repr(elem)?; - str_parts.push(part.value.clone()); - } - - format!("{{{}}}", str_parts.join(", ")) - } else { - "set(...)".to_string() - }; - Ok(vm.new_str(s)) -} - -pub fn set_contains(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args, required = [(set, None), (needle, None)]); - validate_set_or_frozenset(vm, set.class())?; - for element in get_elements(set).iter() { - match vm._eq(needle.clone(), element.1.clone()) { - Ok(value) => { - if objbool::get_value(&value) { - return Ok(vm.new_bool(true)); - } - } - Err(_) => return Err(vm.new_type_error("".to_string())), - } - } - - Ok(vm.new_bool(false)) -} - -fn set_eq(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_compare_inner( - vm, - args, - &|zelf: usize, other: usize| -> bool { zelf != other }, - false, - ) -} - -fn set_ge(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_compare_inner( - vm, - args, - &|zelf: usize, other: usize| -> bool { zelf < other }, - false, - ) -} - -fn set_gt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_compare_inner( - vm, - args, - &|zelf: usize, other: usize| -> bool { zelf <= other }, - false, - ) -} - -fn set_le(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_compare_inner( - vm, - args, - &|zelf: usize, other: usize| -> bool { zelf < other }, - true, - ) -} - -fn set_lt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_compare_inner( - vm, - args, - &|zelf: usize, other: usize| -> bool { zelf <= other }, - true, - ) -} - fn set_compare_inner( vm: &VirtualMachine, - args: PyFuncArgs, + zelf: &PyObjectRef, + other: &PyObjectRef, size_func: &Fn(usize, usize) -> bool, swap: bool, ) -> PyResult { - arg_check!(vm, args, required = [(zelf, None), (other, None)]); - validate_set_or_frozenset(vm, zelf.class())?; validate_set_or_frozenset(vm, other.class())?; @@ -369,225 +523,11 @@ fn set_compare_inner( Ok(vm.new_bool(true)) } -fn set_union(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - validate_set_or_frozenset(vm, zelf.class())?; - validate_set_or_frozenset(vm, other.class())?; - - let mut elements = get_elements(&zelf).clone(); - elements.extend(get_elements(&other).clone()); - - create_set(vm, elements, zelf.class()) -} - -fn set_intersection(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - set_combine_inner(zelf, other, vm, SetCombineOperation::Intersection) -} - -fn set_difference(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - set_combine_inner(zelf, other, vm, SetCombineOperation::Difference) -} - -fn set_symmetric_difference( - zelf: PyObjectRef, - other: PyObjectRef, - vm: &VirtualMachine, -) -> PyResult { - validate_set_or_frozenset(vm, zelf.class())?; - validate_set_or_frozenset(vm, other.class())?; - let mut elements = HashMap::new(); - - for element in get_elements(&zelf).iter() { - let value = vm.call_method(&other, "__contains__", vec![element.1.clone()])?; - if !objbool::get_value(&value) { - elements.insert(element.0.clone(), element.1.clone()); - } - } - - for element in get_elements(&other).iter() { - let value = vm.call_method(&zelf, "__contains__", vec![element.1.clone()])?; - if !objbool::get_value(&value) { - elements.insert(element.0.clone(), element.1.clone()); - } - } - - create_set(vm, elements, zelf.class()) -} - enum SetCombineOperation { Intersection, Difference, } -fn set_combine_inner( - zelf: PyObjectRef, - other: PyObjectRef, - vm: &VirtualMachine, - op: SetCombineOperation, -) -> PyResult { - validate_set_or_frozenset(vm, zelf.class())?; - validate_set_or_frozenset(vm, other.class())?; - let mut elements = HashMap::new(); - - for element in get_elements(&zelf).iter() { - let value = vm.call_method(&other, "__contains__", vec![element.1.clone()])?; - let should_add = match op { - SetCombineOperation::Intersection => objbool::get_value(&value), - SetCombineOperation::Difference => !objbool::get_value(&value), - }; - if should_add { - elements.insert(element.0.clone(), element.1.clone()); - } - } - - create_set(vm, elements, zelf.class()) -} - -fn set_pop(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]); - - match s.payload::() { - Some(set) => { - let mut elements = set.elements.borrow_mut(); - match elements.clone().keys().next() { - Some(key) => Ok(elements.remove(key).unwrap()), - None => Err(vm.new_key_error("pop from an empty set".to_string())), - } - } - _ => Err(vm.new_type_error("".to_string())), - } -} - -fn set_update(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_ior(vm, args)?; - Ok(vm.get_none()) -} - -fn set_ior(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [(zelf, Some(vm.ctx.set_type())), (iterable, None)] - ); - - match zelf.payload::() { - Some(set) => { - let iterator = objiter::get_iter(vm, iterable)?; - while let Ok(v) = vm.call_method(&iterator, "__next__", vec![]) { - insert_into_set(vm, &mut set.elements.borrow_mut(), &v)?; - } - } - _ => return Err(vm.new_type_error("set.update is called with no other".to_string())), - } - Ok(zelf.clone()) -} - -fn set_intersection_update(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_combine_update_inner(vm, args, SetCombineOperation::Intersection)?; - Ok(vm.get_none()) -} - -fn set_iand(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_combine_update_inner(vm, args, SetCombineOperation::Intersection) -} - -fn set_difference_update(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_combine_update_inner(vm, args, SetCombineOperation::Difference)?; - Ok(vm.get_none()) -} - -fn set_isub(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_combine_update_inner(vm, args, SetCombineOperation::Difference) -} - -fn set_combine_update_inner( - vm: &VirtualMachine, - args: PyFuncArgs, - op: SetCombineOperation, -) -> PyResult { - arg_check!( - vm, - args, - required = [(zelf, Some(vm.ctx.set_type())), (iterable, None)] - ); - - match zelf.payload::() { - Some(set) => { - let mut elements = set.elements.borrow_mut(); - for element in elements.clone().iter() { - let value = vm.call_method(iterable, "__contains__", vec![element.1.clone()])?; - let should_remove = match op { - SetCombineOperation::Intersection => !objbool::get_value(&value), - SetCombineOperation::Difference => objbool::get_value(&value), - }; - if should_remove { - elements.remove(&element.0.clone()); - } - } - } - _ => return Err(vm.new_type_error("".to_string())), - } - Ok(zelf.clone()) -} - -fn set_symmetric_difference_update(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - set_ixor(vm, args)?; - Ok(vm.get_none()) -} - -fn set_ixor(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [(zelf, Some(vm.ctx.set_type())), (iterable, None)] - ); - - match zelf.payload::() { - Some(set) => { - let elements_original = set.elements.borrow().clone(); - let iterator = objiter::get_iter(vm, iterable)?; - while let Ok(v) = vm.call_method(&iterator, "__next__", vec![]) { - insert_into_set(vm, &mut set.elements.borrow_mut(), &v)?; - } - for element in elements_original.iter() { - let value = vm.call_method(iterable, "__contains__", vec![element.1.clone()])?; - if objbool::get_value(&value) { - set.elements.borrow_mut().remove(&element.0.clone()); - } - } - } - _ => return Err(vm.new_type_error("".to_string())), - } - - Ok(zelf.clone()) -} - -fn set_iter(zelf: PySetRef, vm: &VirtualMachine) -> PyIteratorValue { - let items = zelf.elements.borrow().values().cloned().collect(); - let set_list = vm.ctx.new_list(items); - PyIteratorValue { - position: Cell::new(0), - iterated_obj: set_list, - } -} - -fn frozenset_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args, required = [(o, Some(vm.ctx.frozenset_type()))]); - - let elements = get_elements(o); - let s = if elements.is_empty() { - "frozenset()".to_string() - } else { - let mut str_parts = vec![]; - for elem in elements.values() { - let part = vm.to_repr(elem)?; - str_parts.push(part.value.clone()); - } - - format!("frozenset({{{}}})", str_parts.join(", ")) - }; - Ok(vm.new_str(s)) -} - pub fn init(context: &PyContext) { let set_type = &context.set_type; @@ -596,41 +536,41 @@ pub fn init(context: &PyContext) { Build an unordered collection of unique elements."; extend_class!(context, set_type, { - "__contains__" => context.new_rustfunc(set_contains), - "__len__" => context.new_rustfunc(set_len), + "__contains__" => context.new_rustfunc(PySetRef::contains), + "__len__" => context.new_rustfunc(PySetRef::len), "__new__" => context.new_rustfunc(set_new), - "__repr__" => context.new_rustfunc(set_repr), - "__eq__" => context.new_rustfunc(set_eq), - "__ge__" => context.new_rustfunc(set_ge), - "__gt__" => context.new_rustfunc(set_gt), - "__le__" => context.new_rustfunc(set_le), - "__lt__" => context.new_rustfunc(set_lt), - "issubset" => context.new_rustfunc(set_le), - "issuperset" => context.new_rustfunc(set_ge), - "union" => context.new_rustfunc(set_union), - "__or__" => context.new_rustfunc(set_union), - "intersection" => context.new_rustfunc(set_intersection), - "__and__" => context.new_rustfunc(set_intersection), - "difference" => context.new_rustfunc(set_difference), - "__sub__" => context.new_rustfunc(set_difference), - "symmetric_difference" => context.new_rustfunc(set_symmetric_difference), - "__xor__" => context.new_rustfunc(set_symmetric_difference), + "__repr__" => context.new_rustfunc(PySetRef::repr), + "__eq__" => context.new_rustfunc(PySetRef::eq), + "__ge__" => context.new_rustfunc(PySetRef::ge), + "__gt__" => context.new_rustfunc(PySetRef::gt), + "__le__" => context.new_rustfunc(PySetRef::le), + "__lt__" => context.new_rustfunc(PySetRef::lt), + "issubset" => context.new_rustfunc(PySetRef::le), + "issuperset" => context.new_rustfunc(PySetRef::ge), + "union" => context.new_rustfunc(PySetRef::union), + "__or__" => context.new_rustfunc(PySetRef::union), + "intersection" => context.new_rustfunc(PySetRef::intersection), + "__and__" => context.new_rustfunc(PySetRef::intersection), + "difference" => context.new_rustfunc(PySetRef::difference), + "__sub__" => context.new_rustfunc(PySetRef::difference), + "symmetric_difference" => context.new_rustfunc(PySetRef::symmetric_difference), + "__xor__" => context.new_rustfunc(PySetRef::symmetric_difference), "__doc__" => context.new_str(set_doc.to_string()), - "add" => context.new_rustfunc(set_add), - "remove" => context.new_rustfunc(set_remove), - "discard" => context.new_rustfunc(set_discard), - "clear" => context.new_rustfunc(set_clear), - "copy" => context.new_rustfunc(set_copy), - "pop" => context.new_rustfunc(set_pop), - "update" => context.new_rustfunc(set_update), - "__ior__" => context.new_rustfunc(set_ior), - "intersection_update" => context.new_rustfunc(set_intersection_update), - "__iand__" => context.new_rustfunc(set_iand), - "difference_update" => context.new_rustfunc(set_difference_update), - "__isub__" => context.new_rustfunc(set_isub), - "symmetric_difference_update" => context.new_rustfunc(set_symmetric_difference_update), - "__ixor__" => context.new_rustfunc(set_ixor), - "__iter__" => context.new_rustfunc(set_iter) + "add" => context.new_rustfunc(PySetRef::add), + "remove" => context.new_rustfunc(PySetRef::remove), + "discard" => context.new_rustfunc(PySetRef::discard), + "clear" => context.new_rustfunc(PySetRef::clear), + "copy" => context.new_rustfunc(PySetRef::copy), + "pop" => context.new_rustfunc(PySetRef::pop), + "update" => context.new_rustfunc(PySetRef::update), + "__ior__" => context.new_rustfunc(PySetRef::ior), + "intersection_update" => context.new_rustfunc(PySetRef::intersection_update), + "__iand__" => context.new_rustfunc(PySetRef::iand), + "difference_update" => context.new_rustfunc(PySetRef::difference_update), + "__isub__" => context.new_rustfunc(PySetRef::isub), + "symmetric_difference_update" => context.new_rustfunc(PySetRef::symmetric_difference_update), + "__ixor__" => context.new_rustfunc(PySetRef::ixor), + "__iter__" => context.new_rustfunc(PySetRef::iter) }); let frozenset_type = &context.frozenset_type; @@ -641,25 +581,26 @@ pub fn init(context: &PyContext) { extend_class!(context, frozenset_type, { "__new__" => context.new_rustfunc(set_new), - "__eq__" => context.new_rustfunc(set_eq), - "__ge__" => context.new_rustfunc(set_ge), - "__gt__" => context.new_rustfunc(set_gt), - "__le__" => context.new_rustfunc(set_le), - "__lt__" => context.new_rustfunc(set_lt), - "issubset" => context.new_rustfunc(set_le), - "issuperset" => context.new_rustfunc(set_ge), - "union" => context.new_rustfunc(set_union), - "__or__" => context.new_rustfunc(set_union), - "intersection" => context.new_rustfunc(set_intersection), - "__and__" => context.new_rustfunc(set_intersection), - "difference" => context.new_rustfunc(set_difference), - "__sub__" => context.new_rustfunc(set_difference), - "symmetric_difference" => context.new_rustfunc(set_symmetric_difference), - "__xor__" => context.new_rustfunc(set_symmetric_difference), - "__contains__" => context.new_rustfunc(set_contains), - "__len__" => context.new_rustfunc(set_len), + "__eq__" => context.new_rustfunc(PyFrozenSetRef::eq), + "__ge__" => context.new_rustfunc(PyFrozenSetRef::ge), + "__gt__" => context.new_rustfunc(PyFrozenSetRef::gt), + "__le__" => context.new_rustfunc(PyFrozenSetRef::le), + "__lt__" => context.new_rustfunc(PyFrozenSetRef::lt), + "issubset" => context.new_rustfunc(PyFrozenSetRef::le), + "issuperset" => context.new_rustfunc(PyFrozenSetRef::ge), + "union" => context.new_rustfunc(PyFrozenSetRef::union), + "__or__" => context.new_rustfunc(PyFrozenSetRef::union), + "intersection" => context.new_rustfunc(PyFrozenSetRef::intersection), + "__and__" => context.new_rustfunc(PyFrozenSetRef::intersection), + "difference" => context.new_rustfunc(PyFrozenSetRef::difference), + "__sub__" => context.new_rustfunc(PyFrozenSetRef::difference), + "symmetric_difference" => context.new_rustfunc(PyFrozenSetRef::symmetric_difference), + "__xor__" => context.new_rustfunc(PyFrozenSetRef::symmetric_difference), + "__contains__" => context.new_rustfunc(PyFrozenSetRef::contains), + "__len__" => context.new_rustfunc(PyFrozenSetRef::len), "__doc__" => context.new_str(frozenset_doc.to_string()), - "__repr__" => context.new_rustfunc(frozenset_repr), - "copy" => context.new_rustfunc(set_copy) + "__repr__" => context.new_rustfunc(PyFrozenSetRef::repr), + "copy" => context.new_rustfunc(PyFrozenSetRef::copy), + "__iter__" => context.new_rustfunc(PyFrozenSetRef::iter) }); }