{"id":31592416,"url":"https://github.com/komar007/option_into_controlflow","last_synced_at":"2025-10-06T03:13:06.640Z","repository":{"id":270493007,"uuid":"910454702","full_name":"komar007/option_into_controlflow","owner":"komar007","description":"Option into ControlFlow conversion","archived":false,"fork":false,"pushed_at":"2024-12-31T18:40:18.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-22T05:25:40.329Z","etag":null,"topics":["rust","rust-controlflow","rust-no-std","rust-option","rust-patterns"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/komar007.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","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,"publiccode":null,"codemeta":null}},"created_at":"2024-12-31T10:14:24.000Z","updated_at":"2025-01-02T16:56:41.000Z","dependencies_parsed_at":"2024-12-31T16:37:50.844Z","dependency_job_id":null,"html_url":"https://github.com/komar007/option_into_controlflow","commit_stats":null,"previous_names":["komar007/option_into_controlflow"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/komar007/option_into_controlflow","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komar007%2Foption_into_controlflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komar007%2Foption_into_controlflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komar007%2Foption_into_controlflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komar007%2Foption_into_controlflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/komar007","download_url":"https://codeload.github.com/komar007/option_into_controlflow/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komar007%2Foption_into_controlflow/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278551676,"owners_count":26005424,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["rust","rust-controlflow","rust-no-std","rust-option","rust-patterns"],"created_at":"2025-10-06T03:09:03.626Z","updated_at":"2025-10-06T03:13:06.635Z","avatar_url":"https://github.com/komar007.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `option_into_controlflow`\n\n![Crates.io License](https://img.shields.io/crates/l/option_into_controlflow)\n[![Crates.io Version](https://img.shields.io/crates/v/option_into_controlflow)](https://crates.io/crates/option_into_controlflow/)\n![GitHub branch check runs](https://img.shields.io/github/check-runs/komar007/option_into_controlflow/main)\n[![docs.rs](https://img.shields.io/docsrs/option_into_controlflow)](https://docs.rs/option_into_controlflow)\n\nConvert `Option\u003cT\u003e` into `ControlFlow\u003cT, _\u003e` or `ControlFlow\u003c_, T\u003e`.\n\n## Overview\n\nAnalogically to `ok_or` and `ok_or_else` for converting into `Result`, this crate introduces:\n- `break_or`/`continue_or`,\n- `break_or_else`/`continue_or_else` and\n- `break_or_default`/`continue_or_default` for converting into `ControlFlow`.\n\nSince `ControlFlow` is more symmetrical than `Result`, functions exist for converting the `Some`\nvariant both into `Break` and `Continue`.\n\n## Why?\n\nSuppose you are receiving some messages:\n\n```rust\nasync fn process_messages(mut msgs: mpsc::Receiver\u003ci32\u003e) {\n    while let Some(msg) = msgs.recv().await {\n        println!(\"msg = {}\", msg)\n    }\n}\n```\n\nbut now you need to support cancellation, so you do this:\n\n```rust\nasync fn process_messages(mut msgs: mpsc::Receiver\u003ci32\u003e, token: CancellationToken) {\n    while let Some(msg) = select! { biased;\n        _ = token.cancelled() =\u003e None,\n        m = msgs.recv() =\u003e m,\n    } {\n        println!(\"msg = {}\", msg)\n    }\n}\n```\n\nand that's fine, but now you're using `Option` for controlling flow. `None` used to mean there will\nbe no more messages. This is semantically correct for a receiver and it's fine to pattern-match on\nit in a loop condition. But now, `None` means \"there will be no more messages OR processing is\ncancelled\". As the logic becomes more complicated, you might want to pattern-match on\n`ControlFlow::Continue` instead, which is there for a reason - it conveys whether to continue or to\nbreak.\n\nSo you can do this:\n\n```rust\nasync fn process_messages(mut msgs: mpsc::Receiver\u003ci32\u003e, token: CancellationToken) {\n    while let ControlFlow::Continue(msg) = select! { biased;\n        _ = token.cancelled() =\u003e ControlFlow::Break(()),\n        m = msgs.recv() =\u003e {\n            if let Some(m) = m {\n                ControlFlow::Continue(m)\n            } else {\n                ControlFlow::Break(())\n            }\n        },\n    } {\n        println!(\"msg = {}\", msg)\n    }\n}\n```\n\nbut it's so verbose, so, of course, you use `option_into_controlflow`:\n\n```rust\nasync fn process_messages(mut msgs: mpsc::Receiver\u003ci32\u003e, token: CancellationToken) {\n    while let ControlFlow::Continue(msg) = select! { biased;\n        _ = token.cancelled() =\u003e ControlFlow::Break(()),\n        m = msgs.recv() =\u003e m.continue_or(()),\n    } {\n        println!(\"msg = {}\", msg)\n    }\n}\n```\n\nwhich is the best of both worlds - you no longer use `Option` for controlling the loop, but your\ncode becomes semantic, idiomatic and understandable.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkomar007%2Foption_into_controlflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkomar007%2Foption_into_controlflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkomar007%2Foption_into_controlflow/lists"}