Skip to content

Commit 81d4bc7

Browse files
committed
PyMemoryView::try_bytes take closure instead of returning vec
1 parent 28216a8 commit 81d4bc7

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

vm/src/bytesinner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl TryFromObject for PyBytesInner {
4242
elements: j.borrow_value().elements.to_vec()
4343
}),
4444
k @ PyMemoryView => Ok(PyBytesInner {
45-
elements: k.try_value().unwrap()
45+
elements: k.try_bytes(|v| v.to_vec()).unwrap()
4646
}),
4747
l @ PyList => l.to_byte_inner(vm),
4848
obj => {
@@ -358,7 +358,7 @@ impl PyBytesInner {
358358
.collect::<PyResult<Vec<_>>>()?)
359359
}
360360
_ => match_class!(match object {
361-
i @ PyMemoryView => Ok(i.try_value().unwrap()),
361+
i @ PyMemoryView => Ok(i.try_bytes(|v| v.to_vec()).unwrap()),
362362
_ => Err(vm.new_index_error(
363363
"can assign only bytes, buffers, or iterables of ints in range(0, 256)"
364364
.to_owned()

vm/src/obj/objint.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,7 @@ pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, base: &BigInt) -> PyResult
759759
}
760760
memoryview @ PyMemoryView => {
761761
// TODO: proper error handling instead of `unwrap()`
762-
let bytes = memoryview.try_value().unwrap();
763-
bytes_to_int(&bytes)
762+
memoryview.try_bytes(|bytes| bytes_to_int(&bytes)).unwrap()
764763
}
765764
array @ PyArray => {
766765
let bytes = array.tobytes();

vm/src/obj/objmemory.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ pub type PyMemoryViewRef = PyRef<PyMemoryView>;
1616

1717
#[pyimpl]
1818
impl PyMemoryView {
19-
pub fn try_value(&self) -> Option<Vec<u8>> {
20-
try_as_bytes(self.obj_ref.clone(), |bytes| bytes.to_vec())
19+
pub fn try_bytes<F, R>(&self, f: F) -> Option<R>
20+
where
21+
F: Fn(&[u8]) -> R,
22+
{
23+
try_as_bytes(self.obj_ref.clone(), f)
2124
}
2225

2326
#[pyslot]

0 commit comments

Comments
 (0)