pub struct Page<T> {
pub items: Vec<T>,
pub total: u64,
pub page: u64,
pub per_page: u64,
pub pages: u64,
/* private fields */
}Expand description
Paginated response wrapper.
Wraps a collection of items with pagination metadata and generates RFC 5988 Link headers for navigation.
§JSON Response Format
{
"items": [...],
"total": 100,
"page": 2,
"per_page": 20,
"pages": 5
}§Link Headers
When converted to a response, includes RFC 5988 Link headers:
Link: </items?page=1&per_page=20>; rel="first",
</items?page=1&per_page=20>; rel="prev",
</items?page=3&per_page=20>; rel="next",
</items?page=5&per_page=20>; rel="last"§Example
ⓘ
use fastapi_core::{Pagination, Page};
#[get("/users")]
async fn list_users(cx: &Cx, pagination: Pagination) -> impl IntoResponse {
let users = fetch_users(pagination.offset(), pagination.limit()).await;
let total = count_users().await;
pagination.paginate(users, total, "/users")
}Fields§
§items: Vec<T>Items for the current page.
total: u64Total number of items across all pages.
page: u64Current page number (1-indexed).
per_page: u64Items per page.
pages: u64Total number of pages.
Implementations§
Source§impl<T> Page<T>
impl<T> Page<T>
Sourcepub fn new(
items: Vec<T>,
total: u64,
pagination: Pagination,
base_url: String,
) -> Self
pub fn new( items: Vec<T>, total: u64, pagination: Pagination, base_url: String, ) -> Self
Create a new paginated response.
Sourcepub fn with_values(
items: Vec<T>,
total: u64,
page: u64,
per_page: u64,
base_url: impl Into<String>,
) -> Self
pub fn with_values( items: Vec<T>, total: u64, page: u64, per_page: u64, base_url: impl Into<String>, ) -> Self
Create a page with explicit values (for testing or manual construction).
Sourcepub fn link_header(&self) -> String
pub fn link_header(&self) -> String
Generate RFC 5988 Link header value.
Returns a string with Link headers for navigation:
first: Link to the first pageprev: Link to the previous page (if applicable)next: Link to the next page (if applicable)last: Link to the last page
Trait Implementations§
Source§impl<T: Serialize> IntoResponse for Page<T>
impl<T: Serialize> IntoResponse for Page<T>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Convert into a response.
Auto Trait Implementations§
impl<T> Freeze for Page<T>
impl<T> RefUnwindSafe for Page<T>where
T: RefUnwindSafe,
impl<T> Send for Page<T>where
T: Send,
impl<T> Sync for Page<T>where
T: Sync,
impl<T> Unpin for Page<T>where
T: Unpin,
impl<T> UnwindSafe for Page<T>where
T: UnwindSafe,
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).