Skip to content

Commit 251ce4e

Browse files
committed
ArgByteOrder
1 parent 4d244f0 commit 251ce4e

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

vm/src/builtins/int.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use crate::{
55
common::hash,
66
convert::{ToPyObject, ToPyResult},
77
format::FormatSpec,
8-
function::{ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue},
8+
function::{
9+
ArgByteOrder, ArgIntoBool, OptionalArg, OptionalOption, PyArithmeticValue,
10+
PyComparisonValue,
11+
},
912
types::{Comparable, Constructor, Hashable, PyComparisonOp},
1013
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromBorrowedObject,
1114
VirtualMachine,
@@ -626,15 +629,12 @@ impl PyInt {
626629
vm: &VirtualMachine,
627630
) -> PyResult<PyRef<Self>> {
628631
let signed = args.signed.map_or(false, Into::into);
629-
let value = match (args.byteorder.as_str(), signed) {
630-
("big", true) => BigInt::from_signed_bytes_be(&args.bytes.elements),
631-
("big", false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),
632-
("little", true) => BigInt::from_signed_bytes_le(&args.bytes.elements),
633-
("little", false) => BigInt::from_bytes_le(Sign::Plus, &args.bytes.elements),
634-
_ => {
635-
return Err(
636-
vm.new_value_error("byteorder must be either 'little' or 'big'".to_owned())
637-
)
632+
let value = match (args.byteorder, signed) {
633+
(ArgByteOrder::Big, true) => BigInt::from_signed_bytes_be(&args.bytes.elements),
634+
(ArgByteOrder::Big, false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),
635+
(ArgByteOrder::Little, true) => BigInt::from_signed_bytes_le(&args.bytes.elements),
636+
(ArgByteOrder::Little, false) => {
637+
BigInt::from_bytes_le(Sign::Plus, &args.bytes.elements)
638638
}
639639
};
640640
Self::with_value(cls, value, vm)
@@ -656,16 +656,11 @@ impl PyInt {
656656
_ => {}
657657
}
658658

659-
let mut origin_bytes = match (args.byteorder.as_str(), signed) {
660-
("big", true) => value.to_signed_bytes_be(),
661-
("big", false) => value.to_bytes_be().1,
662-
("little", true) => value.to_signed_bytes_le(),
663-
("little", false) => value.to_bytes_le().1,
664-
_ => {
665-
return Err(
666-
vm.new_value_error("byteorder must be either 'little' or 'big'".to_owned())
667-
);
668-
}
659+
let mut origin_bytes = match (args.byteorder, signed) {
660+
(ArgByteOrder::Big, true) => value.to_signed_bytes_be(),
661+
(ArgByteOrder::Big, false) => value.to_bytes_be().1,
662+
(ArgByteOrder::Little, true) => value.to_signed_bytes_le(),
663+
(ArgByteOrder::Little, false) => value.to_bytes_le().1,
669664
};
670665

671666
let origin_len = origin_bytes.len();
@@ -678,21 +673,21 @@ impl PyInt {
678673
_ => vec![0u8; byte_len - origin_len],
679674
};
680675

681-
let bytes = match args.byteorder.as_str() {
682-
"big" => {
676+
let bytes = match args.byteorder {
677+
ArgByteOrder::Big => {
683678
let mut bytes = append_bytes;
684679
bytes.append(&mut origin_bytes);
685680
bytes
686681
}
687-
"little" => {
682+
ArgByteOrder::Little => {
688683
let mut bytes = origin_bytes;
689684
bytes.append(&mut append_bytes);
690685
bytes
691686
}
692-
_ => Vec::new(),
693687
};
694688
Ok(bytes.into())
695689
}
690+
696691
#[pyproperty]
697692
fn real(&self, vm: &VirtualMachine) -> PyRef<Self> {
698693
// subclasses must return int here
@@ -759,15 +754,15 @@ pub struct IntOptions {
759754
#[derive(FromArgs)]
760755
struct IntFromByteArgs {
761756
bytes: PyBytesInner,
762-
byteorder: PyStrRef,
757+
byteorder: ArgByteOrder,
763758
#[pyarg(named, optional)]
764759
signed: OptionalArg<ArgIntoBool>,
765760
}
766761

767762
#[derive(FromArgs)]
768763
struct IntToByteArgs {
769764
length: PyIntRef,
770-
byteorder: PyStrRef,
765+
byteorder: ArgByteOrder,
771766
#[pyarg(named, optional)]
772767
signed: OptionalArg<ArgIntoBool>,
773768
}

vm/src/function/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,26 @@ pub use builtin::{IntoPyNativeFunc, OwnedParam, PyNativeFunc, RefParam};
1616
pub use either::Either;
1717
pub use number::{ArgIntoBool, ArgIntoComplex, ArgIntoFloat};
1818
pub use protocol::{ArgCallable, ArgIterable, ArgMapping, ArgSequence};
19+
20+
use crate::{builtins::PyStr, convert::TryFromBorrowedObject, PyObject, PyResult, VirtualMachine};
21+
22+
#[derive(Clone, Copy, PartialEq, Eq)]
23+
pub enum ArgByteOrder {
24+
Big,
25+
Little,
26+
}
27+
28+
impl TryFromBorrowedObject for ArgByteOrder {
29+
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Self> {
30+
obj.try_value_with(
31+
|s: &PyStr| match s.as_str() {
32+
"big" => Ok(Self::Big),
33+
"little" => Ok(Self::Little),
34+
_ => {
35+
Err(vm.new_value_error("byteorder must be either 'little' or 'big'".to_owned()))
36+
}
37+
},
38+
vm,
39+
)
40+
}
41+
}

0 commit comments

Comments
 (0)