Skip to content

Commit e0aca86

Browse files
bytes: convert methods to new args style
1 parent e427212 commit e0aca86

File tree

1 file changed

+39
-82
lines changed

1 file changed

+39
-82
lines changed

vm/src/obj/objbytes.rs

Lines changed: 39 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use std::cell::Cell;
2+
use std::collections::hash_map::DefaultHasher;
23
use std::hash::{Hash, Hasher};
34
use std::ops::Deref;
45

56
use num_traits::ToPrimitive;
67

7-
use crate::function::{OptionalArg, PyFuncArgs};
8-
use crate::pyobject::{
9-
PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
10-
};
8+
use crate::function::OptionalArg;
9+
use crate::pyobject::{PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue};
1110
use crate::vm::VirtualMachine;
1211

1312
use super::objint;
14-
use super::objtype::{self, PyClassRef};
13+
use super::objtype::PyClassRef;
1514

1615
#[derive(Debug)]
1716
pub struct PyBytes {
@@ -94,102 +93,60 @@ fn bytes_new(
9493
}
9594

9695
impl PyBytesRef {
97-
fn eq(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
98-
arg_check!(
99-
vm,
100-
args,
101-
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
102-
);
103-
104-
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
105-
get_value(a).to_vec() == get_value(b).to_vec()
96+
fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
97+
if let Ok(other) = other.downcast::<PyBytes>() {
98+
vm.ctx.new_bool(self.value == other.value)
10699
} else {
107-
return Ok(vm.ctx.not_implemented());
108-
};
109-
Ok(vm.ctx.new_bool(result))
100+
vm.ctx.not_implemented()
101+
}
110102
}
111103

112-
fn ge(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
113-
arg_check!(
114-
vm,
115-
args,
116-
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
117-
);
118-
119-
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
120-
get_value(a).to_vec() >= get_value(b).to_vec()
104+
fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
105+
if let Ok(other) = other.downcast::<PyBytes>() {
106+
vm.ctx.new_bool(self.value >= other.value)
121107
} else {
122-
return Ok(vm.ctx.not_implemented());
123-
};
124-
Ok(vm.ctx.new_bool(result))
108+
vm.ctx.not_implemented()
109+
}
125110
}
126111

127-
fn gt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
128-
arg_check!(
129-
vm,
130-
args,
131-
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
132-
);
133-
134-
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
135-
get_value(a).to_vec() > get_value(b).to_vec()
112+
fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
113+
if let Ok(other) = other.downcast::<PyBytes>() {
114+
vm.ctx.new_bool(self.value > other.value)
136115
} else {
137-
return Ok(vm.ctx.not_implemented());
138-
};
139-
Ok(vm.ctx.new_bool(result))
116+
vm.ctx.not_implemented()
117+
}
140118
}
141119

142-
fn le(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
143-
arg_check!(
144-
vm,
145-
args,
146-
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
147-
);
148-
149-
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
150-
get_value(a).to_vec() <= get_value(b).to_vec()
120+
fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
121+
if let Ok(other) = other.downcast::<PyBytes>() {
122+
vm.ctx.new_bool(self.value <= other.value)
151123
} else {
152-
return Ok(vm.ctx.not_implemented());
153-
};
154-
Ok(vm.ctx.new_bool(result))
124+
vm.ctx.not_implemented()
125+
}
155126
}
156127

157-
fn lt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
158-
arg_check!(
159-
vm,
160-
args,
161-
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
162-
);
163-
164-
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
165-
get_value(a).to_vec() < get_value(b).to_vec()
128+
fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
129+
if let Ok(other) = other.downcast::<PyBytes>() {
130+
vm.ctx.new_bool(self.value < other.value)
166131
} else {
167-
return Ok(vm.ctx.not_implemented());
168-
};
169-
Ok(vm.ctx.new_bool(result))
132+
vm.ctx.not_implemented()
133+
}
170134
}
171135

172-
fn len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
173-
arg_check!(vm, args, required = [(a, Some(vm.ctx.bytes_type()))]);
174-
175-
let byte_vec = get_value(a).to_vec();
176-
Ok(vm.ctx.new_int(byte_vec.len()))
136+
fn len(self, _vm: &VirtualMachine) -> usize {
137+
self.value.len()
177138
}
178139

179-
fn hash(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
180-
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
181-
let data = get_value(zelf);
182-
let mut hasher = std::collections::hash_map::DefaultHasher::new();
183-
data.hash(&mut hasher);
184-
let hash = hasher.finish();
185-
Ok(vm.ctx.new_int(hash))
140+
fn hash(self, _vm: &VirtualMachine) -> u64 {
141+
let mut hasher = DefaultHasher::new();
142+
self.value.hash(&mut hasher);
143+
hasher.finish()
186144
}
187145

188-
fn repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
189-
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]);
190-
let value = get_value(obj);
191-
let data = String::from_utf8(value.to_vec()).unwrap();
192-
Ok(vm.new_str(format!("b'{}'", data)))
146+
fn repr(self, _vm: &VirtualMachine) -> String {
147+
// TODO: don't just unwrap
148+
let data = String::from_utf8(self.value.clone()).unwrap();
149+
format!("b'{}'", data)
193150
}
194151

195152
fn iter(obj: PyBytesRef, _vm: &VirtualMachine) -> PyIteratorValue {

0 commit comments

Comments
 (0)