From ef24de24a82fe681581cc130f342363c47c0969a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 7 Jun 2024 07:48:48 +0200 Subject: Merging upstream version 1.75.0+dfsg1. Signed-off-by: Daniel Baumann --- .../grid/records/into_records/limit_row_records.rs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 vendor/tabled/src/grid/records/into_records/limit_row_records.rs (limited to 'vendor/tabled/src/grid/records/into_records/limit_row_records.rs') diff --git a/vendor/tabled/src/grid/records/into_records/limit_row_records.rs b/vendor/tabled/src/grid/records/into_records/limit_row_records.rs new file mode 100644 index 000000000..a461c6682 --- /dev/null +++ b/vendor/tabled/src/grid/records/into_records/limit_row_records.rs @@ -0,0 +1,59 @@ +//! The module contains [`LimitRows`] records iterator. + +use crate::grid::records::IntoRecords; + +/// [`LimitRows`] is an records iterator which limits amount of rows. +#[derive(Debug)] +pub struct LimitRows { + records: I, + limit: usize, +} + +impl LimitRows<()> { + /// Creates new [`LimitRows`] iterator. + pub fn new(records: I, limit: usize) -> LimitRows { + LimitRows { records, limit } + } +} + +impl IntoRecords for LimitRows +where + I: IntoRecords, +{ + type Cell = I::Cell; + type IterColumns = I::IterColumns; + type IterRows = LimitRowsIter<::IntoIter>; + + fn iter_rows(self) -> Self::IterRows { + LimitRowsIter { + iter: self.records.iter_rows().into_iter(), + limit: self.limit, + } + } +} + +/// A rows iterator for [`LimitRows`] +#[derive(Debug)] +pub struct LimitRowsIter { + iter: I, + limit: usize, +} + +impl Iterator for LimitRowsIter +where + I: Iterator, + I::Item: IntoIterator, + ::Item: AsRef, +{ + type Item = I::Item; + + fn next(&mut self) -> Option { + if self.limit == 0 { + return None; + } + + self.limit -= 1; + + self.iter.next() + } +} -- cgit v1.2.3