Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Coroutine I/O for Rust

License

Apache-2.0 and 2 other licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Unknown
LICENSE-THIRD-PARTY
NotificationsYou must be signed in to change notification settings

zonyitoo/coio-rs

Build StatusBuild statusLicense

Coroutine scheduling with work-stealing algorithm.

WARN: Possibly crash because of TLS inline, check#56 for more detail!

Feature

  • Non-blocking I/O
  • Work-stealing coroutine scheduling
  • Asynchronous computing APIs

Usage

Note: You must useNightly Rust to build this Project.

[dependencies.coio]git ="https://github.com/zonyitoo/coio-rs.git"

Basic Coroutines

externcrate coio;use coio::Scheduler;fnmain(){Scheduler::new().run(||{for _in0..10{println!("Heil Hydra");Scheduler::sched();// Yields the current coroutine}}).unwrap();}

TCP Echo Server

externcrate coio;use std::io::{Read,Write};use coio::net::TcpListener;use coio::{spawn,Scheduler};fnmain(){// Spawn a coroutine for accepting new connectionsScheduler::new().with_workers(4).run(move||{let acceptor =TcpListener::bind("127.0.0.1:8080").unwrap();println!("Waiting for connection ...");for streamin acceptor.incoming(){let(mut stream, addr) = stream.unwrap();println!("Got connection from {:?}", addr);// Spawn a new coroutine to handle the connectionspawn(move||{letmut buf =[0;1024];loop{match stream.read(&mut buf){Ok(0) =>{println!("EOF");break;},Ok(len) =>{println!("Read {} bytes, echo back", len);                            stream.write_all(&buf[0..len]).unwrap();},Err(err) =>{println!("Error occurs: {:?}", err);break;}}}println!("Client closed");});}}).unwrap();}

Exit the main function

Will cause all pending coroutines to be killed.

externcrate coio;use std::sync::Arc;use std::sync::atomic::{AtomicUsize,Ordering};use std::time::Duration;use coio::Scheduler;fnmain(){let counter =Arc::new(AtomicUsize::new(0));let cloned_counter = counter.clone();let result =Scheduler::new().run(move||{// Spawn a new coroutineScheduler::spawn(move||{structGuard(Arc<AtomicUsize>);implDropforGuard{fndrop(&mutself){self.0.store(1,Ordering::SeqCst);}}// If the _guard is dropped, it will store 1 to the counterlet _guard =Guard(cloned_counter);            coio::sleep(Duration::from_secs(10));println!("Not going to run this line");});// Exit right now, which will cause the coroutine to be destroyed.panic!("Exit right now!!");});// The coroutine's stack is unwound properlyassert!(result.is_err() && counter.load(Ordering::SeqCst) ==1);}

Basic Benchmarks

Seebenchmarks for more details.

About

Coroutine I/O for Rust

Resources

License

Apache-2.0 and 2 other licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Unknown
LICENSE-THIRD-PARTY

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors8


[8]ページ先頭

©2009-2025 Movatter.jp