Skip to content

Commit 490c638

Browse files
coolreader18youknowone
authored andcommitted
Add size_hint() to PyIterator
1 parent 50770f5 commit 490c638

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

vm/src/pyobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,12 @@ impl<T> PyIterable<T> {
980980
},
981981
)?;
982982

983+
let lenhint = objiter::length_hint(vm, iter_obj.clone())?;
984+
983985
Ok(PyIterator {
984986
vm,
985987
obj: iter_obj,
988+
lenhint,
986989
_item: std::marker::PhantomData,
987990
})
988991
}
@@ -991,6 +994,7 @@ impl<T> PyIterable<T> {
991994
pub struct PyIterator<'a, T> {
992995
vm: &'a VirtualMachine,
993996
obj: PyObjectRef,
997+
lenhint: Option<usize>,
994998
_item: std::marker::PhantomData<T>,
995999
}
9961000

@@ -1012,6 +1016,10 @@ where
10121016
}
10131017
}
10141018
}
1019+
1020+
fn size_hint(&self) -> (usize, Option<usize>) {
1021+
(self.lenhint.unwrap_or(0), self.lenhint)
1022+
}
10151023
}
10161024

10171025
impl<T> TryFromObject for PyIterable<T>

0 commit comments

Comments
 (0)