Skip to content

Instantly share code, notes, and snippets.

@yejingchen
Last active December 21, 2020 17:21
Show Gist options
  • Save yejingchen/1f5e56941bca6463319b87b55b6cacee to your computer and use it in GitHub Desktop.
Save yejingchen/1f5e56941bca6463319b87b55b6cacee to your computer and use it in GitHub Desktop.
tokio/io-ring hello world
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