{"id":20688766,"url":"https://github.com/71/async-if","last_synced_at":"2026-02-20T01:31:59.370Z","repository":{"id":221685893,"uuid":"744620553","full_name":"71/async-if","owner":"71","description":"Proof-of-concept generic async functions in Rust.","archived":false,"fork":false,"pushed_at":"2024-02-09T12:27:50.000Z","size":27,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T22:24:29.439Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/71.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-01-17T17:10:28.000Z","updated_at":"2024-02-12T14:32:23.000Z","dependencies_parsed_at":"2024-02-09T14:06:15.406Z","dependency_job_id":null,"html_url":"https://github.com/71/async-if","commit_stats":null,"previous_names":["71/async-if"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/71/async-if","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/71%2Fasync-if","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/71%2Fasync-if/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/71%2Fasync-if/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/71%2Fasync-if/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/71","download_url":"https://codeload.github.com/71/async-if/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/71%2Fasync-if/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29638632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T23:06:53.757Z","updated_at":"2026-02-20T01:31:59.351Z","avatar_url":"https://github.com/71.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# async-if\n\nAn experimental Rust library for generic async functions.\n\n## Warning\n\nThis library is a proof-of-concept I made out of curiosity. Its items are\ndocumented and it has some amount of tests, but the following points need\nadditional work:\n\n1. Safety: the crate uses `unsafe` in several positions. Most notably, pin\n   projections used internally may be incorrect.\n\n2. Trait bounds: none of the traits and types (un)implements `Sync` and `Send`.\n   This may be unnecessarily restrictive or, in the worst case, unsafe.\n\n3. Testing: more testing is needed, notably to figure out if inference of `A` in\n   `AsyncIf\u003cA\u003e` in macro-generated code is correct (and not too restrictive).\n\n## Examples\n\nDemonstrating the `#[async_if]` attribute:\n\n```rust\npub struct Std;\npub struct Tokio;\n\n#[async_if(Self::A)]\npub trait Sleep {\n    type A: IsAsync;\n\n    async fn sleep(duration: Duration);\n}\n\n#[async_if(Self::A)]\nimpl Sleep for Std {\n    type A = Synchronous;\n\n    async fn sleep(duration: Duration) { std::thread::sleep(duration) }\n}\n\n#[async_if(Self::A)]\nimpl Sleep for Tokio {\n    type A = Asynchronous;\n\n    async fn sleep(duration: Duration) { from_future(tokio::time::sleep(duration)).await }\n}\n\nasync_if::assert_sync(\u0026Std::sleep(Duration::from_millis(100)));\nasync_if::assert_async(\u0026Tokio::sleep(Duration::from_millis(100)));\n```\n\nSupport for recursion with no allocations for `Synchronous` futures:\n\n```rust\n#[async_if(A, alloc_with = bump)]\nasync fn factorial\u003cA: IsAsync\u003e(bump: \u0026bumpalo::Bump, n: u8) -\u003e u64 {\n    if n == 0 { 1 } else { n as u64 * factorial::\u003cA\u003e(bump, n - 1).await }\n}\n\nlet bump = bumpalo::Bump::new();  // Bumpalo support requires the `bumpalo` feature.\nassert_eq!(factorial::\u003cSynchronous\u003e(\u0026bump, 5).get(), 120);\nassert_eq!(bump.allocated_bytes(), 0);\n\nassert_eq!(factorial::\u003cAsynchronous\u003e(\u0026bump, 5).await, 120);\nassert_ne!(bump.allocated_bytes(), 0);\n```\n\nThe `choose()` helper to mix synchronous and asynchronous APIs:\n\n```rust\n#[async_if(A)]\nasync fn sleep\u003cA: IsAsync\u003e(duration: std::time::Duration) {\n    choose::\u003cA, _\u003e(\n        move || std::thread::sleep(duration),\n        move || Box::pin(tokio::time::sleep(duration)),\n    )\n    .await\n}\n```\n\nUsing `std`, `async-std` or `tokio` dynamically:\n\n```rust\nuse async_std_if::{Std, Time, Tokio};\n\n\u003cStd as Time\u003e::sleep(Duration::from_millis(100)).get();    // Synchronous!\n\u003cTokio as Time\u003e::sleep(Duration::from_millis(100)).await;  // Asynchronous!\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F71%2Fasync-if","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F71%2Fasync-if","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F71%2Fasync-if/lists"}