Closed Wireless4024 closed 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?;
I need custom alignment when reading file with
O_DIRECT
but I didn't see any public api that capable to allocateBytesMut
with custom alignment (e.g. 4096). I did have my api to allocateVec<u8>
with custom alignment butBytesMut::from_vec
is private :(