Last active
December 21, 2020 17:21
-
-
Save yejingchen/1f5e56941bca6463319b87b55b6cacee to your computer and use it in GitHub Desktop.
tokio/io-ring hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::os::unix::io::AsRawFd; | |
use io_uring::IoUring; | |
use io_uring::opcode::{Write, types::Fd}; | |
use thiserror::Error; | |
#[derive(Error, Debug)] | |
enum MyError { | |
#[error("创建 io_uring 失败")] RingCreation(#[from] io::Error), | |
#[error("提交 sqe 失败")] Submit, | |
} | |
fn main() -> Result<(), MyError> { | |
let mut ring = IoUring::new(10)?; | |
let s = String::from("hello, world!\n"); | |
let stdout = io::stdout(); | |
let stdout = stdout.lock(); | |
let out_fd = Fd(stdout.as_raw_fd()); | |
let sqe = Write::new(out_fd, s.as_ptr(), s.len() as u32).build(); | |
{ | |
let sq = ring.submission(); | |
let mut avail_sq = sq.available(); | |
unsafe { | |
// SAFETY: write syscall arguments lives long enough until program | |
// terminates. | |
// 怎么保证参数里的 s: String 所有权转移给了内核? | |
avail_sq.push(sqe).map_err(|_| MyError::Submit)?; | |
}; | |
} | |
let completed = ring.submit_and_wait(1)?; | |
eprintln!("{} tasks completed", completed); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment