pub struct Pagination { /* private fields */ }Expand description
Pagination query parameters extractor.
Extracts common pagination parameters from the query string:
page: Current page number (1-indexed, default: 1)per_pageorlimit: Items per page (default: 20, max: 100)offset: Alternative to page-based pagination (overrides page if set)
§Example
ⓘ
use fastapi_core::Pagination;
#[get("/items")]
async fn list_items(cx: &Cx, pagination: Pagination) -> impl IntoResponse {
let offset = pagination.offset();
let limit = pagination.limit();
// Fetch items from database with offset and limit
let items = db.fetch_items(offset, limit).await;
// Return paginated response
pagination.paginate(items, total_count, "/items")
}§Query String Formats
# Page-based (preferred)
?page=2&per_page=10
# Using limit alias
?page=2&limit=10
# Offset-based (for cursor-style pagination)
?offset=20&limit=10§Configuration
Use PaginationConfig to customize defaults and limits:
ⓘ
use fastapi_core::{Pagination, PaginationConfig};
let config = PaginationConfig::new()
.default_per_page(50)
.max_per_page(200);
// The config can be stored in app state and used by handlersImplementations§
Source§impl Pagination
impl Pagination
Sourcepub fn from_offset(offset: u64, limit: u64) -> Self
pub fn from_offset(offset: u64, limit: u64) -> Self
Create pagination from offset and limit.
Sourcepub fn offset(&self) -> u64
pub fn offset(&self) -> u64
Calculate the offset for database queries.
If an explicit offset was provided, returns that.
Otherwise, calculates from page number: (page - 1) * per_page.
Sourcepub fn total_pages(&self, total_items: u64) -> u64
pub fn total_pages(&self, total_items: u64) -> u64
Calculate total number of pages given a total item count.
Trait Implementations§
Source§impl Clone for Pagination
impl Clone for Pagination
Source§fn clone(&self) -> Pagination
fn clone(&self) -> Pagination
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Pagination
impl Debug for Pagination
Source§impl Default for Pagination
impl Default for Pagination
Source§impl FromRequest for Pagination
impl FromRequest for Pagination
Source§type Error = Infallible
type Error = Infallible
Error type when extraction fails.
Source§async fn from_request(
_ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( _ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
Source§impl PartialEq for Pagination
impl PartialEq for Pagination
impl Copy for Pagination
impl Eq for Pagination
impl StructuralPartialEq for Pagination
Auto Trait Implementations§
impl Freeze for Pagination
impl RefUnwindSafe for Pagination
impl Send for Pagination
impl Sync for Pagination
impl Unpin for Pagination
impl UnwindSafe for Pagination
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).