Crate zbin

Crate zbin 

Source
Expand description

Binary data trait for compio async IO.

compio uses io_uring/IOCP for async IO, which requires buffer ownership transfer. The kernel holds the buffer during IO, so we must pass owned data, not references.

§compio IoBuf constraint

compio requires IoBuf trait for async write. Types that impl IoBuf:

  • Vec<u8>, Box<[u8]>, &'static [u8], &'static str
  • Rc<[u8]>, Arc<[u8]> (zero-copy, compio native support)
  • Bytes (with bytes feature)

§Bin trait

This trait bridges various input types to compio’s ownership model:

  • as_slice(): for sync operations (CRC calc, inline write)
  • io(): transfer ownership to kernel, returns Self::Io: IoBuf

§Zero-copy Table

TypeIoNote
Vec<u8>, Box<[u8]>selfzero-copy
Rc<[u8]>, Arc<[u8]>selfzero-copy (compio impl)
Bytesselfzero-copy
&Rc, &Arc, &BytescloneO(1) ref-count +1
StringVec<u8>into_bytes()
&[u8], &str, &[u8; N]Box<[u8]>copy
Cow<[u8]>, Cow<str>Vec<u8>into_owned

用于 compio 异步 IO 的二进制数据 trait。

compio 使用 io_uring/IOCP 实现异步 IO,需要转移缓冲区所有权。 内核在 IO 期间持有缓冲区,因此必须传递拥有所有权的数据,而非引用。

§compio IoBuf 约束

compio 异步写入需要 IoBuf trait。实现了 IoBuf 的类型:

  • Vec<u8>, Box<[u8]>, &'static [u8], &'static str
  • Rc<[u8]>, Arc<[u8]>(零拷贝,compio 原生支持)
  • Bytes(需 bytes feature)

§Bin trait

此 trait 将各种输入类型桥接到 compio 的所有权模型:

  • as_slice(): 用于同步操作(CRC 计算、内联写入)
  • io(): 将所有权转移给内核,返回 Self::Io: IoBuf

§零拷贝表

类型Io说明
Vec<u8>, Box<[u8]>self零拷贝
Rc<[u8]>, Arc<[u8]>self零拷贝(compio 实现)
Bytesself零拷贝
&Rc, &Arc, &BytescloneO(1) 引用计数 +1
StringVec<u8>into_bytes()
&[u8], &str, &[u8; N]Box<[u8]>拷贝
Cow<[u8]>, Cow<str>Vec<u8>into_owned

Traits§

Bin
Binary data trait for async IO / 异步 IO 二进制数据 trait