Skip to main content

Module paginator

Module paginator 

Source
Expand description

Defines some types and traits to simplify working with list RPCs.

When listing large collections of items Google Cloud services typically break the responses in “pages”. Each one of these pages contains a limited number of items, and a token to request the next page. The caller must repeatedly call the list RPC (using the token from the previous page) to obtain additional items.

Sequencing these calls can be tedious. The Google Cloud client libraries for Rust provide implementations of this trait to simplify the use of these paginated APIs.

For more information on the RPCs with paginated responses see AIP-4233.

§Example: stream each item for a list operation

struct Page { items: Vec<String>, token: String }
use paginator::{Paginator, ItemPaginator};
fn list() -> impl Paginator<Page, Error> {
    // ... details omitted ...
}
let mut items = list().items();
while let Some(item) = items.next().await {
    let item = item?;
    println!("  item = {item}");
}

§Example: manually iterate over all the pages for a list operation

struct Page { items: Vec<String>, token: String }
use paginator::Paginator;
fn list() -> impl Paginator<Page, Error> {
    // ... details omitted ...
}
let mut pages = list();
while let Some(page) = pages.next().await {
    let page = page?;
    println!("a page with {} items", page.items.len());
    page.items.into_iter().enumerate().for_each(|(n, i)| println!("  item[{n}]= {i}"));
}

Traits§

ItemPaginator
Automatically stream items in list RPCs.
Paginator
Automatically streams pages in list RPCs.