Skip to main content

Paginated

Struct Paginated 

Source
pub struct Paginated<T> {
    pub items: Vec<T>,
    pub total: i64,
    pub limit: i64,
    pub offset: i64,
}
Expand description

A page of rows plus the total, for pagination links and JSON envelopes.

Query::paginate builds one with two queries (the rows and COUNT(*)). Serializes as {"items": [...], "total": 42, "limit": 20, "offset": 0}.

§Free plan

The count reads every matching row once more: on large tables, prefer plain “next page” links without a total (Page::next after Query::all) or cache the total.

§Examples

use ocre::{Page, Paginated};

let page = Paginated { items: vec!["a", "b"], total: 5, limit: 2, offset: 2 };
assert_eq!(page.current_page(), 2);
assert_eq!(page.total_pages(), 3);
assert!(page.has_next() && page.has_previous());
assert_eq!(page.next_page(), Some(Page { limit: 2, offset: 4 }));
assert_eq!(page.previous_page(), Some(Page { limit: 2, offset: 0 }));
assert_eq!(serde_json::to_string(&page).unwrap(), r#"{"items":["a","b"],"total":5,"limit":2,"offset":2}"#);

Fields§

§items: Vec<T>

The rows of this page.

§total: i64

Rows matching the query, all pages together.

§limit: i64

Rows per page.

§offset: i64

Rows skipped before this page.

Implementations§

Source§

impl<T> Paginated<T>

Source

pub fn current_page(&self) -> i64

1-based number of this page.

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 0, limit: 10, offset: 0 };
assert_eq!(page.current_page(), 1);
Source

pub fn total_pages(&self) -> i64

Number of pages (at least 1, even with no row).

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 21, limit: 10, offset: 0 };
assert_eq!(page.total_pages(), 3);
Source

pub fn has_next(&self) -> bool

Whether rows follow this page.

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 10, limit: 10, offset: 0 };
assert!(!page.has_next());
Source

pub fn has_previous(&self) -> bool

Whether rows come before this page.

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 10, limit: 10, offset: 0 };
assert!(!page.has_previous());
Source

pub fn next_page(&self) -> Option<Page>

The next page, if any.

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 3, limit: 2, offset: 2 };
assert_eq!(page.next_page(), None);
Source

pub fn previous_page(&self) -> Option<Page>

The previous page, if any (never a negative offset).

§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 30, limit: 10, offset: 5 };
assert_eq!(page.previous_page(), Some(ocre::Page { limit: 10, offset: 0 }));
Source

pub fn map<U>(self, f: impl FnMut(T) -> U) -> Paginated<U>

Converts the items, keeping the counts (e.g. rows into view structs).

§Examples
let page = ocre::Paginated { items: vec![1, 2], total: 2, limit: 10, offset: 0 };
assert_eq!(page.map(|n| n * 10).items, [10, 20]);

Trait Implementations§

Source§

impl<T: Clone> Clone for Paginated<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Paginated<T>

Source§

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

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

impl<T: PartialEq> PartialEq for Paginated<T>

Source§

fn eq(&self, other: &Paginated<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Paginated<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Paginated<T>

Auto Trait Implementations§

§

impl<T> Freeze for Paginated<T>

§

impl<T> RefUnwindSafe for Paginated<T>
where T: RefUnwindSafe,

§

impl<T> Send for Paginated<T>
where T: Send,

§

impl<T> Sync for Paginated<T>
where T: Sync,

§

impl<T> Unpin for Paginated<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Paginated<T>

§

impl<T> UnwindSafe for Paginated<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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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
§

impl<T> ToRawKvValue for T
where T: Serialize,

§

fn raw_kv_value(&self) -> Result<JsValue, KvError>

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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V