[][src]Struct egg_mode::cursor::CursorIter

#[must_use = "cursor iterators are lazy and do nothing unless consumed"]pub struct CursorIter<T> where
    T: Cursor + DeserializeOwned
{ pub page_size: Option<i32>, pub previous_cursor: i64, pub next_cursor: i64, // some fields omitted }

Represents a paginated list of results, such as the users who follow a specific user or the lists owned by that user.

This struct is given by several methods in this library, whenever Twitter would return a cursored list of items. It implements the Stream trait, loading items in batches so that several can be immedately returned whenever a single network call completes.

use futures::{StreamExt, TryStreamExt};

egg_mode::user::followers_of("rustlang", &token).take(10).try_for_each(|resp| {
    println!("{}", resp.screen_name);
    futures::future::ok(())
}).await.unwrap();

You can even collect the results, letting you get one set of rate-limit information for the entire search setup:

use futures::{StreamExt, TryStreamExt};
use egg_mode::Response;
use egg_mode::user::TwitterUser;
use egg_mode::error::Result;

// Because Streams don't have a FromIterator adaptor, we load all the responses first, then
// collect them into the final Vec
let names: Result<Vec<TwitterUser>> =
    egg_mode::user::followers_of("rustlang", &token)
        .take(10)
        .map_ok(|r| r.response)
        .try_collect::<Vec<_>>()
        .await;

CursorIter has an adaptor of its own, with_page_size, that you can use before consuming it. with_page_size will let you set how many users are pulled in with a single network call. Calling it after starting iteration will clear any current results.

(A note about with_page_size/page_size: While the CursorIter struct always has this method and field available, not every cursored call supports changing page size. Check the individual method documentation for notes on what page sizes are allowed.)

The Stream implementation yields Response<T::Item> on a successful iteration, and Error for errors, so network errors, rate-limit errors and other issues are passed directly through in poll(). The Stream implementation will allow you to poll again after an error to re-initiate the late network call; this way, you can wait for your network connection to return or for your rate limit to refresh and try again with the same state.

Manual paging

The Stream implementation works by loading in a page of results (with size set by the method's default or by with_page_size/the page_size field) when it's polled, and serving the individual elements from that locally-cached page until it runs out. This can be nice, but it also means that your only warning that something involves a network call is that the stream returns Poll::Pending, by which time the network call has already started. If you want to know that ahead of time, that's where the call() method comes in. By using call(), you can get the cursor struct directly from Twitter. With that you can iterate over the results and page forward and backward as needed:

let mut list = egg_mode::user::followers_of("rustlang", &token).with_page_size(20);
let resp = list.call().await.unwrap();

for user in resp.response.users {
    println!("{} (@{})", user.name, user.screen_name);
}

list.next_cursor = resp.response.next_cursor;
let resp = list.call().await.unwrap();

for user in resp.response.users {
    println!("{} (@{})", user.name, user.screen_name);
}

Fields

page_size: Option<i32>

The number of results returned in one network call.

Certain calls set their own minimums and maximums for what this value can be. Furthermore, some calls don't allow you to set the size of the pages at all. Refer to the individual methods' documentation for specifics.

previous_cursor: i64

Numeric reference to the previous page of results. A value of zero indicates that the current page of results is the first page of the cursor.

This value is intended to be automatically set and used as part of this struct's Iterator implementation. It is made available for those who wish to manually manage network calls and pagination.

next_cursor: i64

Numeric reference to the next page of results. A value of zero indicates that the current page of results is the last page of the cursor.

This value is intended to be automatically set and used as part of this struct's Iterator implementation. It is made available for those who wish to manually manage network calls and pagination.

Implementations

impl<T> CursorIter<T> where
    T: Cursor + DeserializeOwned
[src]

pub fn with_page_size(self, page_size: i32) -> CursorIter<T>[src]

Sets the number of results returned in a single network call.

Certain calls set their own minimums and maximums for what this value can be. Furthermore, some calls don't allow you to set the size of the pages at all. Refer to the individual methods' documentation for specifics. If this method is called for a response that does not accept changing the page size, no change to the underlying struct will occur.

Calling this function will invalidate any current results, if any were previously loaded.

pub fn call(&self) -> impl Future<Output = Result<Response<T>>>[src]

Loads the next page of results.

This is intended to be used as part of this struct's Iterator implementation. It is provided as a convenience for those who wish to manage network calls and pagination manually.

Trait Implementations

impl<T> Stream for CursorIter<T> where
    T: Cursor + DeserializeOwned + 'static,
    T::Item: Unpin
[src]

type Item = Result<Response<T::Item>>

Values yielded by the stream.

Auto Trait Implementations

impl<T> !RefUnwindSafe for CursorIter<T>

impl<T> !Send for CursorIter<T>

impl<T> !Sync for CursorIter<T>

impl<T> Unpin for CursorIter<T>

impl<T> !UnwindSafe for CursorIter<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> StreamExt for T where
    T: Stream + ?Sized
[src]

impl<St> StreamExt for St where
    St: Stream + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<S> TryStreamExt for S where
    S: TryStream + ?Sized
[src]

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