{"id":13438361,"url":"https://github.com/zonyitoo/coio-rs","last_synced_at":"2025-04-06T05:17:20.146Z","repository":{"id":36638716,"uuid":"40944985","full_name":"zonyitoo/coio-rs","owner":"zonyitoo","description":"Coroutine I/O for Rust","archived":false,"fork":false,"pushed_at":"2018-08-24T20:23:20.000Z","size":511,"stargazers_count":454,"open_issues_count":10,"forks_count":31,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-04-30T08:32:04.246Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zonyitoo.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}},"created_at":"2015-08-18T01:59:21.000Z","updated_at":"2024-04-22T21:00:40.000Z","dependencies_parsed_at":"2022-08-16T16:30:27.694Z","dependency_job_id":null,"html_url":"https://github.com/zonyitoo/coio-rs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zonyitoo%2Fcoio-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zonyitoo%2Fcoio-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zonyitoo%2Fcoio-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zonyitoo%2Fcoio-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zonyitoo","download_url":"https://codeload.github.com/zonyitoo/coio-rs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247436289,"owners_count":20938533,"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","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-07-31T03:01:04.861Z","updated_at":"2025-04-06T05:17:20.126Z","avatar_url":"https://github.com/zonyitoo.png","language":"Rust","funding_links":[],"categories":["Libraries","库 Libraries","Rust","库","异步（Asynchronous）"],"sub_categories":["Concurrency","异步 Asynchronous","Asynchronous","并发","并发 Concurrency","天文（Astronomy）"],"readme":"# Coroutine I/O\n\n[![Build Status](https://travis-ci.org/zonyitoo/coio-rs.svg?branch=master)](https://travis-ci.org/zonyitoo/coio-rs)\n[![Build status](https://ci.appveyor.com/api/projects/status/vdhv46h90ffbfxsi?svg=true)](https://ci.appveyor.com/project/zonyitoo/coio-rs)\n[![License](https://img.shields.io/github/license/zonyitoo/coio-rs.svg)](https://github.com/zonyitoo/coio-rs)\n\nCoroutine scheduling with work-stealing algorithm.\n\n**WARN**: Possibly crash because of TLS inline, check https://github.com/zonyitoo/coio-rs/issues/56 for more detail!\n\n## Feature\n\n* Non-blocking I/O\n* Work-stealing coroutine scheduling\n* Asynchronous computing APIs\n\n## Usage\n\nNote: You must use [Nightly Rust](https://doc.rust-lang.org/book/nightly-rust.html) to build this Project.\n\n```toml\n[dependencies.coio]\ngit = \"https://github.com/zonyitoo/coio-rs.git\"\n```\n\n### Basic Coroutines\n\n```rust\nextern crate coio;\n\nuse coio::Scheduler;\n\nfn main() {\n    Scheduler::new()\n        .run(|| {\n            for _ in 0..10 {\n                println!(\"Heil Hydra\");\n                Scheduler::sched(); // Yields the current coroutine\n            }\n        })\n        .unwrap();\n}\n```\n\n### TCP Echo Server\n\n```rust\nextern crate coio;\n\nuse std::io::{Read, Write};\n\nuse coio::net::TcpListener;\nuse coio::{spawn, Scheduler};\n\nfn main() {\n    // Spawn a coroutine for accepting new connections\n    Scheduler::new().with_workers(4).run(move|| {\n        let acceptor = TcpListener::bind(\"127.0.0.1:8080\").unwrap();\n        println!(\"Waiting for connection ...\");\n\n        for stream in acceptor.incoming() {\n            let (mut stream, addr) = stream.unwrap();\n\n            println!(\"Got connection from {:?}\", addr);\n\n            // Spawn a new coroutine to handle the connection\n            spawn(move|| {\n                let mut buf = [0; 1024];\n\n                loop {\n                    match stream.read(\u0026mut buf) {\n                        Ok(0) =\u003e {\n                            println!(\"EOF\");\n                            break;\n                        },\n                        Ok(len) =\u003e {\n                            println!(\"Read {} bytes, echo back\", len);\n                            stream.write_all(\u0026buf[0..len]).unwrap();\n                        },\n                        Err(err) =\u003e {\n                            println!(\"Error occurs: {:?}\", err);\n                            break;\n                        }\n                    }\n                }\n\n                println!(\"Client closed\");\n            });\n        }\n    }).unwrap();\n}\n```\n\n### Exit the main function\n\nWill cause all pending coroutines to be killed.\n\n```rust\nextern crate coio;\n\nuse std::sync::Arc;\nuse std::sync::atomic::{AtomicUsize, Ordering};\nuse std::time::Duration;\n\nuse coio::Scheduler;\n\nfn main() {\n    let counter = Arc::new(AtomicUsize::new(0));\n    let cloned_counter = counter.clone();\n\n    let result = Scheduler::new().run(move|| {\n        // Spawn a new coroutine\n        Scheduler::spawn(move|| {\n            struct Guard(Arc\u003cAtomicUsize\u003e);\n\n            impl Drop for Guard {\n                fn drop(\u0026mut self) {\n                    self.0.store(1, Ordering::SeqCst);\n                }\n            }\n\n            // If the _guard is dropped, it will store 1 to the counter\n            let _guard = Guard(cloned_counter);\n\n            coio::sleep(Duration::from_secs(10));\n            println!(\"Not going to run this line\");\n        });\n\n        // Exit right now, which will cause the coroutine to be destroyed.\n        panic!(\"Exit right now!!\");\n    });\n\n    // The coroutine's stack is unwound properly\n    assert!(result.is_err() \u0026\u0026 counter.load(Ordering::SeqCst) == 1);\n}\n```\n\n## Basic Benchmarks\n\nSee [benchmarks](benchmarks) for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzonyitoo%2Fcoio-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzonyitoo%2Fcoio-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzonyitoo%2Fcoio-rs/lists"}