improved PyByteInner.setitem
@@ -426,14 +426,7 @@ impl PyByteInner {
}
}
- pub fn setitem(
- &mut self,
- needle: Either<PyIntRef, PySliceRef>,
- object: PyObjectRef,
- vm: &VirtualMachine,
- ) -> PyResult {
- match needle {
- Either::A(int) => {
+ fn setindex(&mut self, int: PyIntRef, object: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Some(idx) = self.elements.get_pos(int.as_bigint().to_i32().unwrap()) {
let result = match_class!(object,
i @ PyInt => {
@@ -445,18 +438,20 @@ impl PyByteInner {
},
_ => {Err(vm.new_type_error("an integer is required".to_string()))}
);
- match result {
- Ok(value) => {
+ let value = result?;
self.elements[idx] = value;
Ok(vm.new_int(value))
- }
- Err(error) => Err(error),
- }
} else {
Err(vm.new_index_error("index out of range".to_string()))
}
}
- Either::B(slice) => {
+
+ fn setslice(
+ &mut self,
+ slice: PySliceRef,
+ object: PyObjectRef,
+ vm: &VirtualMachine,
+ ) -> PyResult {
let sec = match PyIterable::try_from_object(vm, object.clone()) {
Ok(sec) => {
let items: Result<Vec<PyObjectRef>, _> = sec.iter(vm)?.collect();
@@ -473,8 +468,7 @@ impl PyByteInner {
"can assign only bytes, buffers, or iterables of ints in range(0, 256)"
.to_string()))),
};
- match sec {
- Ok(items) => {
+ let items = sec?;
let range = self
.elements
.get_slice_range(&slice.start_index(vm)?, &slice.stop_index(vm)?);
@@ -483,9 +477,16 @@ impl PyByteInner {
.ctx
.new_bytes(self.elements.get_slice_items(vm, slice.as_object())?))
}
- Err(error) => Err(error),
- }
- }
+
+ pub fn setitem(
+ &mut self,
+ needle: Either<PyIntRef, PySliceRef>,
+ object: PyObjectRef,
+ vm: &VirtualMachine,
+ ) -> PyResult {
+ match needle {
+ Either::A(int) => self.setindex(int, object, vm),
+ Either::B(slice) => self.setslice(slice, object, vm),
}
}
