Skip to content

Commit ec5af4a

Browse files
committed
iterator: create module for legacy abstractions
Not to clutter iterator.rs with legacy abstractions, they are moved to a new module: `legacy`. I recommend viewing this commit **without** whitespace changes.
1 parent 3340b9d commit ec5af4a

File tree

1 file changed

+81
-72
lines changed

1 file changed

+81
-72
lines changed

scylla/src/transport/iterator.rs

Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl QueryPager {
631631
/// to a middle-man [Row] type.
632632
#[inline]
633633
pub fn into_legacy(self) -> LegacyRowIterator {
634-
LegacyRowIterator { raw_iterator: self }
634+
LegacyRowIterator::new(self)
635635
}
636636

637637
pub(crate) async fn new_for_query(
@@ -936,97 +936,106 @@ impl QueryPager {
936936
}
937937
}
938938

939-
/// Iterator over rows returned by paged queries.
940-
///
941-
/// Allows to easily access rows without worrying about handling multiple pages.
942-
pub struct LegacyRowIterator {
943-
raw_iterator: QueryPager,
944-
}
939+
mod legacy {
940+
use super::*;
945941

946-
impl Stream for LegacyRowIterator {
947-
type Item = Result<Row, QueryError>;
942+
/// Iterator over rows returned by paged queries.
943+
///
944+
/// Allows to easily access rows without worrying about handling multiple pages.
945+
pub struct LegacyRowIterator {
946+
raw_stream: QueryPager,
947+
}
948948

949-
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
950-
let mut s = self.as_mut();
949+
impl Stream for LegacyRowIterator {
950+
type Item = Result<Row, QueryError>;
951951

952-
let next_fut = s.raw_iterator.next();
953-
futures::pin_mut!(next_fut);
952+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
953+
let mut s = self.as_mut();
954954

955-
let next_column_iter = ready_some_ok!(next_fut.poll(cx));
955+
let next_fut = s.raw_stream.next();
956+
futures::pin_mut!(next_fut);
956957

957-
let next_ready_row =
958-
Row::deserialize(next_column_iter).map_err(|e| RowsParseError::from(e).into());
958+
let next_column_iter = ready_some_ok!(next_fut.poll(cx));
959959

960-
Poll::Ready(Some(next_ready_row))
961-
}
962-
}
960+
let next_ready_row =
961+
Row::deserialize(next_column_iter).map_err(|e| RowsParseError::from(e).into());
963962

964-
impl LegacyRowIterator {
965-
/// If tracing was enabled returns tracing ids of all finished page queries
966-
pub fn get_tracing_ids(&self) -> &[Uuid] {
967-
self.raw_iterator.tracing_ids()
963+
Poll::Ready(Some(next_ready_row))
964+
}
968965
}
969966

970-
/// Returns specification of row columns
971-
pub fn get_column_specs(&self) -> &[ColumnSpec<'_>] {
972-
self.raw_iterator.column_specs().inner()
973-
}
967+
impl LegacyRowIterator {
968+
pub(super) fn new(raw_stream: QueryPager) -> Self {
969+
Self { raw_stream }
970+
}
974971

975-
pub fn into_typed<RowT>(self) -> LegacyTypedRowIterator<RowT> {
976-
LegacyTypedRowIterator {
977-
row_iterator: self,
978-
_phantom_data: Default::default(),
972+
/// If tracing was enabled returns tracing ids of all finished page queries
973+
pub fn get_tracing_ids(&self) -> &[Uuid] {
974+
self.raw_stream.tracing_ids()
979975
}
980-
}
981-
}
982976

983-
/// Iterator over rows returned by paged queries
984-
/// where each row is parsed as the given type\
985-
/// Returned by `RowIterator::into_typed`
986-
pub struct LegacyTypedRowIterator<RowT> {
987-
row_iterator: LegacyRowIterator,
988-
_phantom_data: std::marker::PhantomData<RowT>,
989-
}
977+
/// Returns specification of row columns
978+
pub fn get_column_specs(&self) -> &[ColumnSpec<'_>] {
979+
self.raw_stream.column_specs().inner()
980+
}
990981

991-
impl<RowT> LegacyTypedRowIterator<RowT> {
992-
/// If tracing was enabled returns tracing ids of all finished page queries
993-
#[inline]
994-
pub fn get_tracing_ids(&self) -> &[Uuid] {
995-
self.row_iterator.get_tracing_ids()
982+
pub fn into_typed<RowT>(self) -> LegacyTypedRowIterator<RowT> {
983+
LegacyTypedRowIterator {
984+
row_iterator: self,
985+
_phantom_data: Default::default(),
986+
}
987+
}
996988
}
997989

998-
/// Returns specification of row columns
999-
#[inline]
1000-
pub fn get_column_specs(&self) -> &[ColumnSpec<'_>] {
1001-
self.row_iterator.get_column_specs()
990+
/// Iterator over rows returned by paged queries
991+
/// where each row is parsed as the given type\
992+
/// Returned by `RowIterator::into_typed`
993+
pub struct LegacyTypedRowIterator<RowT> {
994+
row_iterator: LegacyRowIterator,
995+
_phantom_data: std::marker::PhantomData<RowT>,
1002996
}
1003-
}
1004997

1005-
/// Couldn't get next typed row from the iterator
1006-
#[derive(Error, Debug, Clone)]
1007-
pub enum NextRowError {
1008-
/// Query to fetch next page has failed
1009-
#[error(transparent)]
1010-
QueryError(#[from] QueryError),
998+
impl<RowT> LegacyTypedRowIterator<RowT> {
999+
/// If tracing was enabled returns tracing ids of all finished page queries
1000+
#[inline]
1001+
pub fn get_tracing_ids(&self) -> &[Uuid] {
1002+
self.row_iterator.get_tracing_ids()
1003+
}
10111004

1012-
/// Parsing values in row as given types failed
1013-
#[error(transparent)]
1014-
FromRowError(#[from] FromRowError),
1015-
}
1005+
/// Returns specification of row columns
1006+
#[inline]
1007+
pub fn get_column_specs(&self) -> &[ColumnSpec<'_>] {
1008+
self.row_iterator.get_column_specs()
1009+
}
1010+
}
10161011

1017-
/// Fetching pages is asynchronous so `LegacyTypedRowIterator` does not implement the `Iterator` trait.\
1018-
/// Instead it uses the asynchronous `Stream` trait
1019-
impl<RowT: FromRow> Stream for LegacyTypedRowIterator<RowT> {
1020-
type Item = Result<RowT, NextRowError>;
1012+
/// Couldn't get next typed row from the iterator
1013+
#[derive(Error, Debug, Clone)]
1014+
pub enum NextRowError {
1015+
/// Query to fetch next page has failed
1016+
#[error(transparent)]
1017+
QueryError(#[from] QueryError),
10211018

1022-
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1023-
let mut s = self.as_mut();
1019+
/// Parsing values in row as given types failed
1020+
#[error(transparent)]
1021+
FromRowError(#[from] FromRowError),
1022+
}
1023+
1024+
/// Fetching pages is asynchronous so `LegacyTypedRowIterator` does not implement the `Iterator` trait.\
1025+
/// Instead it uses the asynchronous `Stream` trait
1026+
impl<RowT: FromRow> Stream for LegacyTypedRowIterator<RowT> {
1027+
type Item = Result<RowT, NextRowError>;
10241028

1025-
let next_row = ready_some_ok!(Pin::new(&mut s.row_iterator).poll_next(cx));
1026-
let typed_row_res = RowT::from_row(next_row).map_err(|e| e.into());
1027-
Poll::Ready(Some(typed_row_res))
1029+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1030+
let mut s = self.as_mut();
1031+
1032+
let next_row = ready_some_ok!(Pin::new(&mut s.row_iterator).poll_next(cx));
1033+
let typed_row_res = RowT::from_row(next_row).map_err(|e| e.into());
1034+
Poll::Ready(Some(typed_row_res))
1035+
}
10281036
}
1029-
}
10301037

1031-
// LegacyTypedRowIterator can be moved freely for any RowT so it's Unpin
1032-
impl<RowT> Unpin for LegacyTypedRowIterator<RowT> {}
1038+
// LegacyTypedRowIterator can be moved freely for any RowT so it's Unpin
1039+
impl<RowT> Unpin for LegacyTypedRowIterator<RowT> {}
1040+
}
1041+
pub use legacy::{LegacyRowIterator, LegacyTypedRowIterator, NextRowError};

0 commit comments

Comments
 (0)