|
1 | 1 | use super::{PyInt, PyStrRef, PyType, PyTypeRef};
|
2 | 2 | use crate::{
|
3 |
| - class::PyClassImpl, convert::ToPyObject, function::OptionalArg, identifier, types::Constructor, |
| 3 | + class::PyClassImpl, |
| 4 | + convert::{ToPyObject, ToPyResult}, |
| 5 | + function::OptionalArg, |
| 6 | + identifier, |
| 7 | + protocol::PyNumberMethods, |
| 8 | + types::{AsNumber, Constructor}, |
4 | 9 | AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
|
5 | 10 | VirtualMachine,
|
6 | 11 | };
|
@@ -102,7 +107,7 @@ impl Constructor for PyBool {
|
102 | 107 | }
|
103 | 108 | }
|
104 | 109 |
|
105 |
| -#[pyclass(with(Constructor))] |
| 110 | +#[pyclass(with(Constructor, AsNumber))] |
106 | 111 | impl PyBool {
|
107 | 112 | #[pymethod(magic)]
|
108 | 113 | fn repr(zelf: bool, vm: &VirtualMachine) -> PyStrRef {
|
@@ -166,6 +171,24 @@ impl PyBool {
|
166 | 171 | }
|
167 | 172 | }
|
168 | 173 |
|
| 174 | +impl AsNumber for PyBool { |
| 175 | + fn as_number() -> &'static PyNumberMethods { |
| 176 | + static AS_NUMBER: PyNumberMethods = PyNumberMethods { |
| 177 | + and: Some(|number, other, vm| { |
| 178 | + PyBool::and(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm) |
| 179 | + }), |
| 180 | + xor: Some(|number, other, vm| { |
| 181 | + PyBool::xor(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm) |
| 182 | + }), |
| 183 | + or: Some(|number, other, vm| { |
| 184 | + PyBool::or(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm) |
| 185 | + }), |
| 186 | + ..PyInt::AS_NUMBER |
| 187 | + }; |
| 188 | + &AS_NUMBER |
| 189 | + } |
| 190 | +} |
| 191 | + |
169 | 192 | pub(crate) fn init(context: &Context) {
|
170 | 193 | PyBool::extend_class(context, context.types.bool_type);
|
171 | 194 | }
|
|
0 commit comments