MCPcopy Index your code
hub / github.com/RustPython/RustPython / as_number

Method as_number

crates/vm/src/builtins/set.rs:824–936  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

822
823impl AsNumber for PySet {
824 fn as_number() -> &'static PyNumberMethods {
825 static AS_NUMBER: PyNumberMethods = PyNumberMethods {
826 // Binary ops check both operands are sets (like CPython's set_sub, etc.)
827 // This is needed because __rsub__ swaps operands: a.__rsub__(b) calls subtract(b, a)
828 subtract: Some(|a, b, vm| {
829 if !AnySet::check(a, vm) || !AnySet::check(b, vm) {
830 return Ok(vm.ctx.not_implemented());
831 }
832 if let Some(a) = a.downcast_ref::<PySet>() {
833 a.__sub__(b.to_owned(), vm).to_pyresult(vm)
834 } else if let Some(a) = a.downcast_ref::<PyFrozenSet>() {
835 // When called via __rsub__, a might be PyFrozenSet
836 a.__sub__(b.to_owned(), vm)
837 .map(|r| {
838 r.map(|s| PySet {
839 inner: s.inner.clone(),
840 })
841 })
842 .to_pyresult(vm)
843 } else {
844 Ok(vm.ctx.not_implemented())
845 }
846 }),
847 and: Some(|a, b, vm| {
848 if !AnySet::check(a, vm) || !AnySet::check(b, vm) {
849 return Ok(vm.ctx.not_implemented());
850 }
851 if let Some(a) = a.downcast_ref::<PySet>() {
852 a.__and__(b.to_owned(), vm).to_pyresult(vm)
853 } else if let Some(a) = a.downcast_ref::<PyFrozenSet>() {
854 a.__and__(b.to_owned(), vm)
855 .map(|r| {
856 r.map(|s| PySet {
857 inner: s.inner.clone(),
858 })
859 })
860 .to_pyresult(vm)
861 } else {
862 Ok(vm.ctx.not_implemented())
863 }
864 }),
865 xor: Some(|a, b, vm| {
866 if !AnySet::check(a, vm) || !AnySet::check(b, vm) {
867 return Ok(vm.ctx.not_implemented());
868 }
869 if let Some(a) = a.downcast_ref::<PySet>() {
870 a.__xor__(b.to_owned(), vm).to_pyresult(vm)
871 } else if let Some(a) = a.downcast_ref::<PyFrozenSet>() {
872 a.__xor__(b.to_owned(), vm)
873 .map(|r| {
874 r.map(|s| PySet {
875 inner: s.inner.clone(),
876 })
877 })
878 .to_pyresult(vm)
879 } else {
880 Ok(vm.ctx.not_implemented())
881 }

Callers

nothing calls this directly

Calls 11

SomeClass · 0.50
checkFunction · 0.50
not_implementedMethod · 0.45
to_pyresultMethod · 0.45
__sub__Method · 0.45
to_ownedMethod · 0.45
mapMethod · 0.45
cloneMethod · 0.45
__and__Method · 0.45
__xor__Method · 0.45
__or__Method · 0.45

Tested by

no test coverage detected