tokio-rs / bytes

Utilities for working with bytes
MIT License
1.91k stars 288 forks source link

Please allow to construct BytesMut with custom alignment #600

Closed Wireless4024 closed 1 year ago

Wireless4024 commented 1 year ago

I need custom alignment when reading file with O_DIRECT but I didn't see any public api that capable to allocate BytesMut with custom alignment (e.g. 4096). I did have my api to allocate Vec<u8> with custom alignment but BytesMut::from_vec is private :(

Wireless4024 commented 1 year ago

Here's snippet if someone using O_DIRECT with bytes

let path = "Cargo.lock";
let meta = metadata(path)?;
let mut bytes = BytesMut::with_capacity(meta.st_blksize() * 2); // minimum is st_blksize+st_blksize
let ptr = bytes.as_ptr() as usize;
let blksz = (meta.st_blksize() - 1) as usize;
let shift = (ptr + blksz) & !blksz; // round it up
let blksz = meta.st_blksize() as usize;
let mut bytes = bytes.split_off(shift - ptr);
let _ = bytes.split_off(blksz);
let file = OpenOptions::new()
    .read(true)
    .custom_flags(libc::O_DIRECT)
    .open(path)?;
let file = tokio_uring::fs::File::from_std(file);
let (len, bytes) = file.read_at(bytes, 0).await;
let len = len?;