Skip to content

Commit 463b516

Browse files
authored
Implement number protocol for PyBytes (RustPython#3903)
1 parent db6fd16 commit 463b516

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

vm/src/builtins/bytes.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ use crate::{
1313
function::Either,
1414
function::{ArgBytesLike, ArgIterable, OptionalArg, OptionalOption, PyComparisonValue},
1515
protocol::{
16-
BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods,
16+
BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods, PyNumberMethods,
1717
PySequenceMethods,
1818
},
1919
sliceable::{SequenceIndex, SliceableSequenceOp},
2020
types::{
21-
AsBuffer, AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
22-
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
21+
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
22+
IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
2323
},
2424
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
2525
TryFromBorrowedObject, TryFromObject, VirtualMachine,
@@ -108,7 +108,8 @@ impl PyBytes {
108108
Comparable,
109109
AsBuffer,
110110
Iterable,
111-
Constructor
111+
Constructor,
112+
AsNumber
112113
)
113114
)]
114115
impl PyBytes {
@@ -606,6 +607,17 @@ impl AsSequence for PyBytes {
606607
};
607608
}
608609

610+
impl AsNumber for PyBytes {
611+
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
612+
remainder: Some(|number, other, vm| {
613+
Self::number_downcast(number)
614+
.mod_(other.to_owned(), vm)
615+
.to_pyresult(vm)
616+
}),
617+
..PyNumberMethods::NOT_IMPLEMENTED
618+
};
619+
}
620+
609621
impl Hashable for PyBytes {
610622
#[inline]
611623
fn hash(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {

0 commit comments

Comments
 (0)