Skip to main content

fetch_capped

Function fetch_capped 

Source
pub async fn fetch_capped<T, F, Fut>(
    strategy: FetchStrategy,
    cap: u32,
    fetch: F,
) -> Result<Paged<T>>
where F: Fn(u32, u32) -> Fut, Fut: Future<Output = Result<Vec<T>>>,
Expand description

按 strategy 抓取至多 cap 条,并诚实报告是否还有更多。

内部向下游请求 cap + 1 条(N+1 探测):只要 cap 条时无法区分 「恰好 cap 条」与「还有更多」。收到多于 cap 条即截断并置 Paged::truncated。

fetch 的两个参数是 (page, limit):

§Errors

fetch 是返回 Future 的普通闭包(Fn(u32, u32) -> Fut),而非原生 AsyncFn:在 #[async_trait] 装箱的方法体内调用一个捕获引用的 async move |..| {..} 闭包会触发 rustc 的 HRTB Send 检查缺陷 (implementation of Send is not general enough)。返回具名 Future 的 普通闭包不受此限制影响。

§Examples

从一个自身借用了 self 字段的方法体里调用时,借用要捕获的字段 (而非 .clone() 后 move 进闭包),再让每次调用返回的 async move 块去移动那个借用——借用是 Copy,因此闭包仍满足 Fn:

use gitflow_core::{FetchStrategy, Result, fetch_capped};

struct Source {
    repo: String,
}

impl Source {
    async fn list(&self, cap: u32) -> Result<Vec<String>> {
        let repo = &self.repo;
        let paged = fetch_capped(FetchStrategy::SingleShot, cap, |_page, limit| async move {
            // `repo` 是 `&String`(`Copy`),可在每次调用中重新借出,
            // 无需 `.clone()`;此处仅返回不超过 `limit` 条的假数据。
            let all: Vec<String> = (0..3).map(|i| format!("{repo}#{i}")).collect();
            Ok(all.into_iter().take(limit as usize).collect())
        })
        .await?;
        Ok(paged.items)
    }
}

let source = Source { repo: "octocat/hello-world".to_string() };
let items = source.list(10).await.expect("fetch should succeed");
assert_eq!(items.len(), 3);