Refactor code using Either type

This commit is contained in:
Young
2021-11-28 18:37:36 +09:00
parent 290f91cd5e
commit fe26754ad1

View File

@@ -1,6 +1,5 @@
use super::{
PyBytes, PyBytesRef, PyInt, PyList, PyListRef, PySlice, PyStr, PyStrRef, PyTuple, PyTupleRef,
PyTypeRef,
PyBytes, PyBytesRef, PyInt, PyListRef, PySlice, PyStr, PyStrRef, PyTuple, PyTupleRef, PyTypeRef,
};
use crate::common::{
borrow::{BorrowedValue, BorrowedValueMut},
@@ -630,15 +629,13 @@ impl PyMemoryView {
));
}
let tup = shape.payload_if_subclass::<PyTuple>(vm);
let list = shape.payload_if_subclass::<PyList>(vm);
let l = tup.map(|t| PyList::from(t.as_slice().iter().cloned().collect_vec()));
let list_from_tuple = l.as_ref();
let shape = list.xor(list_from_tuple).ok_or_else(|| {
vm.new_type_error("memoryview: must be a list or a tuple".to_owned())
})?;
let shape = match shape {
Either::A(tup) => {
let elements = tup.as_slice().iter().cloned().collect_vec();
vm.ctx.new_list(elements)
}
Either::B(list) => list,
};
let shape_vec = shape.borrow_vec();
let shape_ndim = shape_vec.len();