8000 Refactor list, tuple and collections.deque by youknowone · Pull Request #1655 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Refactor list, tuple and collections.deque #1655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 8, 2020
Prev Previous commit
remove unnessessary pub
  • Loading branch information
youknowone committed Jan 8, 2020
commit 5e2035f9799b3e9f900441fe38c1f03f433464b7
15 changes: 9 additions & 6 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ impl PyList {
}

impl PyList {
pub fn get_len(&self) -> usize {
fn get_len(&self) -> usize {
self.elements.borrow().len()
}

pub fn get_pos(&self, p: i32) -> Option<usize> {
fn get_pos(&self, p: i32) -> Option<usize> {
// convert a (potentially negative) positon into a real index
if p < 0 {
if -p as usize > self.get_len() {
Expand All @@ -82,7 +82,7 @@ impl PyList {
}
}

pub fn get_slice_pos(&self, slice_pos: &BigInt) -> usize {
fn get_slice_pos(&self, slice_pos: &BigInt) -> usize {
if let Some(pos) = slice_pos.to_i32() {
if let Some(index) = self.get_pos(pos) {
// within bounds
Expand All @@ -99,7 +99,7 @@ impl PyList {
}
}

pub fn get_slice_range(&self, start: &Option<BigInt>, stop: &Option<BigInt>) -> Range<usize> {
fn get_slice_range(&self, start: &Option<BigInt>, stop: &Option<BigInt>) -> Range<usize> {
let start = start.as_ref().map(|x| self.get_slice_pos(x)).unwrap_or(0);
let stop = stop
.as_ref()
Expand All @@ -109,7 +109,10 @@ impl PyList {
start..stop
}

pub fn get_byte_inner(&self, vm: &VirtualMachine) -> PyResult<objbyteinner::PyByteInner> {
pub(crate) fn get_byte_inner(
&self,
vm: &VirtualMachine,
) -> PyResult<objbyteinner::PyByteInner> {
let mut elements = Vec::<u8>::with_capacity(self.get_len());
for elem in self.elements.borrow().iter() {
match PyIntRef::try_from_object(vm, elem.clone()) {
Expand Down Expand Up @@ -146,7 +149,7 @@ pub type PyListRef = PyRef<PyList>;
#[pyimpl]
impl PyList {
#[pymethod]
pub fn append(&self, x: PyObjectRef, _vm: &VirtualMachine) {
pub(crate) fn append(&self, x: PyObjectRef, _vm: &VirtualMachine) {
self.elements.borrow_mut().push(x);
}

Expand Down
8 changes: 2 additions & 6 deletions vm/src/obj/objtuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,18 @@ impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5));
impl_intopyobj_tuple!((A, 0), (B, 1), (C, 2), (D, 3), (E, 4), (F, 5), (G, 6));

impl PyTuple {
pub fn fast_getitem(&self, idx: usize) -> PyObjectRef {
pub(crate) fn fast_getitem(&self, idx: usize) -> PyObjectRef {
self.elements[idx].clone()
}

pub fn as_slice(&self) -> &[PyObjectRef] {
&self.elements
}

pub fn as_sequence(&self) -> &impl SimpleSeq {
&self.elements
}
}

pub type PyTupleRef = PyRef<PyTuple>;

pub fn get_value(obj: &PyObjectRef) -> &[PyObjectRef] {
pub(crate) fn get_value(obj: &PyObjectRef) -> &[PyObjectRef] {
obj.payload::<PyTuple>().unwrap().as_slice()
}

Expand Down
38 changes: 30 additions & 8 deletions vm/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ where

// impl<'a, I>

pub fn eq(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) -> PyResult<bool> {
pub(crate) fn eq(
vm: &VirtualMachine,
zelf: &impl SimpleSeq,
other: &impl SimpleSeq,
) -> PyResult<bool> {
if zelf.len() == other.len() {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if a.is(b) {
Expand All @@ -67,7 +71,11 @@ pub fn eq(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) ->
}
}

pub fn lt(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) -> PyResult<bool> {
pub(crate) fn lt(
vm: &VirtualMachine,
zelf: &impl SimpleSeq,
other: &impl SimpleSeq,
) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
return Ok(v);
Expand All @@ -76,7 +84,11 @@ pub fn lt(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) ->
Ok(zelf.len() < other.len())
}

pub fn gt(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) -> PyResult<bool> {
pub(crate) fn gt(
vm: &VirtualMachine,
zelf: &impl SimpleSeq,
other: &impl SimpleSeq,
) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
return Ok(v);
Expand All @@ -85,7 +97,11 @@ pub fn gt(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) ->
Ok(zelf.len() > other.len())
}

pub fn ge(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) -> PyResult<bool> {
pub(crate) fn ge(
vm: &VirtualMachine,
zelf: &impl SimpleSeq,
other: &impl SimpleSeq,
) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
return Ok(v);
Expand All @@ -95,7 +111,11 @@ pub fn ge(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) ->
Ok(zelf.len() >= other.len())
}

pub fn le(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) -> PyResult<bool> {
pub(crate) fn le(
vm: &VirtualMachine,
zelf: &impl SimpleSeq,
other: &impl SimpleSeq,
) -> PyResult<bool> {
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
return Ok(v);
Expand All @@ -105,11 +125,14 @@ pub fn le(vm: &VirtualMachine, zelf: &impl SimpleSeq, other: &impl SimpleSeq) ->
Ok(zelf.len() <= other.len())
}

pub struct SeqMul<'a> {
pub(crate) struct SeqMul<'a> {
seq: &'a dyn SimpleSeq,
repetitions: usize,
iter: Option<DynPyIter<'a>>,
}

impl ExactSizeIterator for SeqMul<'_> {}

impl<'a> Iterator for SeqMul<'a> {
type Item = &'a PyObjectRef;
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -135,9 +158,8 @@ impl<'a> Iterator for SeqMul<'a> {
(size, Some(size))
}
}
impl ExactSizeIterator for SeqMul<'_> {}

pub fn seq_mul(seq: &impl SimpleSeq, repetitions: isize) -> SeqMul {
pub(crate) fn seq_mul(seq: &impl SimpleSeq, repetitions: isize) -> SeqMul {
SeqMul {
seq,
repetitions: repetitions.max(0) as usize,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct PyDequeOptions {
}

impl PyDeque {
pub fn borrow_deque<'a>(&'a self) -> impl std::ops::Deref<Target = VecDeque<PyObjectRef>> + 'a {
fn borrow_deque<'a>(&'a self) -> impl std::ops::Deref<Target = VecDeque<PyObjectRef>> + 'a {
self.deque.borrow()
}
}
Expand Down
0