From 48b67759eded88e26db71a85b4ea8b7e3a153fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= <76261501+zecakeh@users.noreply.github.com> Date: Thu, 30 Mar 2023 16:39:08 +0200 Subject: [PATCH] sdk: Make PaginationOption's custom variant's strategy return a ControlFlow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is semantically more correct than an Option. Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/room/timeline/pagination.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/matrix-sdk/src/room/timeline/pagination.rs b/crates/matrix-sdk/src/room/timeline/pagination.rs index 98a0188f5..ef0f10ab5 100644 --- a/crates/matrix-sdk/src/room/timeline/pagination.rs +++ b/crates/matrix-sdk/src/room/timeline/pagination.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; +use std::{fmt, ops::ControlFlow}; /// Options for pagination. pub struct PaginationOptions<'a> { @@ -48,10 +48,11 @@ impl<'a> PaginationOptions<'a> { /// items for the last request as well as summed over all /// requests in a `paginate_backwards` call, and can decide /// whether to do another request (by returning - /// `Some(next_event_limit)`) or not (by returning `None`). + /// `ControlFlow::Continue(next_event_limit)`) or not (by returning + /// `ControlFlow::Break(())`). pub fn custom( initial_event_limit: u16, - pagination_strategy: impl FnMut(PaginationOutcome) -> Option + Send + 'a, + pagination_strategy: impl FnMut(PaginationOutcome) -> ControlFlow<(), u16> + Send + 'a, ) -> Self { Self::new(PaginationOptionsInner::Custom { event_limit_if_first: Some(initial_event_limit), @@ -71,7 +72,10 @@ impl<'a> PaginationOptions<'a> { (pagination_outcome.total_items_added < *items).then_some(*event_limit) } PaginationOptionsInner::Custom { event_limit_if_first, strategy } => { - event_limit_if_first.take().or_else(|| strategy(pagination_outcome)) + event_limit_if_first.take().or_else(|| match strategy(pagination_outcome) { + ControlFlow::Continue(event_limit) => Some(event_limit), + ControlFlow::Break(_) => None, + }) } } } @@ -91,7 +95,7 @@ pub enum PaginationOptionsInner<'a> { }, Custom { event_limit_if_first: Option, - strategy: Box Option + Send + 'a>, + strategy: Box ControlFlow<(), u16> + Send + 'a>, }, }