Skip to main content

Page

Struct Page 

Source
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
}

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: u64

Total number of items across all pages.

§page: u64

Current page number (1-indexed).

§per_page: u64

Items per page.

§pages: u64

Total number of pages.

Implementations§

Source§

impl<T> Page<T>

Source

pub fn new( items: Vec<T>, total: u64, pagination: Pagination, base_url: String, ) -> Self

Create a new paginated response.

Source

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).

Source

pub fn len(&self) -> usize

Get the number of items on the current page.

Source

pub fn is_empty(&self) -> bool

Check if the page is empty.

Source

pub fn has_next(&self) -> bool

Check if there is a next page.

Source

pub fn has_prev(&self) -> bool

Check if there is a previous page.

Generate RFC 5988 Link header value.

Returns a string with Link headers for navigation:

  • first: Link to the first page
  • prev: Link to the previous page (if applicable)
  • next: Link to the next page (if applicable)
  • last: Link to the last page
Source

pub fn map<U, F>(self, f: F) -> Page<U>
where F: FnMut(T) -> U,

Map the items using a transformation function.

Trait Implementations§

Source§

impl<T: Clone> Clone for Page<T>

Source§

fn clone(&self) -> Page<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Page<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Serialize> IntoResponse for Page<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T