1
- use once_cell:: sync:: Lazy ;
2
-
3
1
/*
4
2
* Builtin set type with a sequence of unique items.
5
3
*/
6
4
use super :: {
7
5
builtins_iter, IterStatus , PositionIterInternal , PyDict , PyDictRef , PyGenericAlias , PyTupleRef ,
8
6
PyType , PyTypeRef ,
9
7
} ;
10
- use crate :: atomic_func;
11
- use crate :: common:: { ascii, hash:: PyHash , lock:: PyMutex , rc:: PyRc } ;
12
8
use crate :: {
9
+ atomic_func,
13
10
class:: PyClassImpl ,
11
+ common:: { ascii, hash:: PyHash , lock:: PyMutex , rc:: PyRc } ,
12
+ convert:: ToPyResult ,
14
13
dictdatatype:: { self , DictSize } ,
15
14
function:: { ArgIterable , FuncArgs , OptionalArg , PosArgs , PyArithmeticValue , PyComparisonValue } ,
16
- protocol:: { PyIterReturn , PySequenceMethods } ,
15
+ protocol:: { PyIterReturn , PyNumberMethods , PySequenceMethods } ,
17
16
recursion:: ReprGuard ,
17
+ types:: AsNumber ,
18
18
types:: {
19
19
AsSequence , Comparable , Constructor , Hashable , Initializer , IterNext , IterNextIterable ,
20
20
Iterable , PyComparisonOp , Unconstructible , Unhashable ,
@@ -23,6 +23,7 @@ use crate::{
23
23
vm:: VirtualMachine ,
24
24
AsObject , Context , Py , PyObject , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject ,
25
25
} ;
26
+ use once_cell:: sync:: Lazy ;
26
27
use std:: { fmt, ops:: Deref } ;
27
28
28
29
pub type SetContentType = dictdatatype:: Dict < ( ) > ;
@@ -488,7 +489,15 @@ fn reduce_set(
488
489
}
489
490
490
491
#[ pyclass(
491
- with( Constructor , Initializer , AsSequence , Hashable , Comparable , Iterable ) ,
492
+ with(
493
+ Constructor ,
494
+ Initializer ,
495
+ AsSequence ,
496
+ Hashable ,
497
+ Comparable ,
498
+ Iterable ,
499
+ AsNumber
500
+ ) ,
492
501
flags( BASETYPE )
493
502
) ]
494
503
impl PySet {
@@ -792,6 +801,67 @@ impl Iterable for PySet {
792
801
}
793
802
}
794
803
804
+ impl AsNumber for PySet {
805
+ fn as_number ( ) -> & ' static PyNumberMethods {
806
+ static AS_NUMBER : Lazy < PyNumberMethods > = Lazy :: new ( || PyNumberMethods {
807
+ subtract : atomic_func ! ( |number, other, vm| {
808
+ PySet :: number_downcast( number)
809
+ . sub( other. to_owned( ) , vm)
810
+ . to_pyresult( vm)
811
+ } ) ,
812
+ and : atomic_func ! ( |number, other, vm| {
813
+ PySet :: number_downcast( number)
814
+ . and( other. to_owned( ) , vm)
815
+ . to_pyresult( vm)
816
+ } ) ,
817
+ xor : atomic_func ! ( |number, other, vm| {
818
+ PySet :: number_downcast( number)
819
+ . xor( other. to_owned( ) , vm)
820
+ . to_pyresult( vm)
821
+ } ) ,
822
+ or : atomic_func ! ( |number, other, vm| {
823
+ PySet :: number_downcast( number)
824
+ . or( other. to_owned( ) , vm)
825
+ . to_pyresult( vm)
826
+ } ) ,
827
+ inplace_subtract : atomic_func ! ( |number, other, vm| {
828
+ PySet :: isub(
829
+ PySet :: number_downcast( number) . to_owned( ) ,
830
+ AnySet :: try_from_object( vm, other. to_owned( ) ) ?,
831
+ vm,
832
+ )
833
+ . to_pyresult( vm)
834
+ } ) ,
835
+ inplace_and : atomic_func ! ( |number, other, vm| {
836
+ PySet :: iand(
837
+ PySet :: number_downcast( number) . to_owned( ) ,
838
+ AnySet :: try_from_object( vm, other. to_owned( ) ) ?,
839
+ vm,
840
+ )
841
+ . to_pyresult( vm)
842
+ } ) ,
843
+ inplace_xor : atomic_func ! ( |number, other, vm| {
844
+ PySet :: ixor(
845
+ PySet :: number_downcast( number) . to_owned( ) ,
846
+ AnySet :: try_from_object( vm, other. to_owned( ) ) ?,
847
+ vm,
848
+ )
849
+ . to_pyresult( vm)
850
+ } ) ,
851
+ inplace_or : atomic_func ! ( |number, other, vm| {
852
+ PySet :: ior(
853
+ PySet :: number_downcast( number) . to_owned( ) ,
854
+ AnySet :: try_from_object( vm, other. to_owned( ) ) ?,
855
+ vm,
856
+ )
857
+ . to_pyresult( vm)
858
+ } ) ,
859
+ ..PyNumberMethods :: NOT_IMPLEMENTED
860
+ } ) ;
861
+ & AS_NUMBER
862
+ }
863
+ }
864
+
795
865
impl Constructor for PyFrozenSet {
796
866
type Args = OptionalArg < PyObjectRef > ;
797
867
@@ -822,7 +892,7 @@ impl Constructor for PyFrozenSet {
822
892
823
893
#[ pyclass(
824
894
flags( BASETYPE ) ,
825
- with( Constructor , AsSequence , Hashable , Comparable , Iterable )
895
+ with( Constructor , AsSequence , Hashable , Comparable , Iterable , AsNumber )
826
896
) ]
827
897
impl PyFrozenSet {
828
898
#[ pymethod( magic) ]
@@ -1019,6 +1089,35 @@ impl Iterable for PyFrozenSet {
1019
1089
}
1020
1090
}
1021
1091
1092
+ impl AsNumber for PyFrozenSet {
1093
+ fn as_number ( ) -> & ' static PyNumberMethods {
1094
+ static AS_NUMBER : Lazy < PyNumberMethods > = Lazy :: new ( || PyNumberMethods {
1095
+ subtract : atomic_func ! ( |number, other, vm| {
1096
+ PyFrozenSet :: number_downcast( number)
1097
+ . sub( other. to_owned( ) , vm)
1098
+ . to_pyresult( vm)
1099
+ } ) ,
1100
+ and : atomic_func ! ( |number, other, vm| {
1101
+ PyFrozenSet :: number_downcast( number)
1102
+ . and( other. to_owned( ) , vm)
1103
+ . to_pyresult( vm)
1104
+ } ) ,
1105
+ xor : atomic_func ! ( |number, other, vm| {
1106
+ PyFrozenSet :: number_downcast( number)
1107
+ . xor( other. to_owned( ) , vm)
1108
+ . to_pyresult( vm)
1109
+ } ) ,
1110
+ or : atomic_func ! ( |number, other, vm| {
1111
+ PyFrozenSet :: number_downcast( number)
1112
+ . or( other. to_owned( ) , vm)
1113
+ . to_pyresult( vm)
1114
+ } ) ,
1115
+ ..PyNumberMethods :: NOT_IMPLEMENTED
1116
+ } ) ;
1117
+ & AS_NUMBER
1118
+ }
1119
+ }
1120
+
1022
1121
struct AnySet {
1023
1122
object : PyObjectRef ,
1024
1123
}
0 commit comments