Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compio-buf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bumpalo = { version = "3.14.0", optional = true }
arrayvec = { version = "0.7.4", optional = true }
bytes = { workspace = true, optional = true }
smallvec = { workspace = true, optional = true }
memmap2 = { version = "0.9.9", optional = true }

[target.'cfg(unix)'.dependencies]
libc = { workspace = true }
Expand All @@ -29,6 +30,7 @@ arrayvec = ["dep:arrayvec"]
smallvec = ["dep:smallvec"]
bumpalo = ["dep:bumpalo"]
bytes = ["dep:bytes"]
memmap2 = ["dep:memmap2"]

# Nightly features
allocator_api = []
Expand Down
62 changes: 62 additions & 0 deletions compio-buf/src/io_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ where
}
}

#[cfg(feature = "memmap2")]
impl IoBuf for memmap2::Mmap {
fn as_init(&self) -> &[u8] {
self
}
}

#[cfg(feature = "memmap2")]
impl IoBuf for memmap2::MmapMut {
fn as_init(&self) -> &[u8] {
self
}
}

/// An error indicating that reserving capacity for a buffer failed.
#[must_use]
#[derive(Debug)]
Expand Down Expand Up @@ -690,6 +704,14 @@ where
}
}

#[cfg(feature = "memmap2")]
impl IoBufMut for memmap2::MmapMut {
fn as_uninit(&mut self) -> &mut [MaybeUninit<u8>] {
// Safety: &mut [u8] is valid &mut [MaybeUninit<u8>]
unsafe { std::mem::transmute(self.as_mut_slice()) }
}
}

/// A helper trait for `set_len` like methods.
pub trait SetLen {
/// Set the buffer length.
Expand Down Expand Up @@ -830,6 +852,13 @@ where
}
}

#[cfg(feature = "memmap2")]
impl SetLen for memmap2::MmapMut {
unsafe fn set_len(&mut self, len: usize) {
debug_assert!(len <= self.len())
}
}

impl<T: IoBufMut> SetLen for [T] {
unsafe fn set_len(&mut self, len: usize) {
unsafe { default_set_len(self.iter_mut(), len) }
Expand Down Expand Up @@ -920,6 +949,39 @@ mod test {
assert!(buf.capacity() >= 10);
}

#[test]
#[cfg(feature = "memmap2")]
fn tests_memmap2() {
use std::{
fs::{OpenOptions, remove_file},
io::{Seek, SeekFrom, Write},
};

use memmap2::MmapOptions;

use super::*;

let path = std::env::temp_dir().join("compio_buf_mmap_mut_test");

let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&path)
.unwrap();
let data = b"hello memmap2";
file.write_all(data).unwrap();
file.flush().unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };

let uninit_slice = mmap.as_init();
assert_eq!(uninit_slice, data);

remove_file(path).unwrap();
}

#[test]
fn test_other_reserve() {
let mut buf = [1, 1, 4, 5, 1, 4];
Expand Down
2 changes: 2 additions & 0 deletions compio-buf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub use arrayvec;
pub use bumpalo;
#[cfg(feature = "bytes")]
pub use bytes;
#[cfg(feature = "memmap2")]
pub use memmap2;

mod io;
pub use io::*;
Expand Down
2 changes: 2 additions & 0 deletions compio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ all = [
arrayvec = ["compio-buf/arrayvec"]
bumpalo = ["compio-buf/bumpalo"]
bytes = ["compio-buf/bytes"]
memmap2 = ["compio-buf/memmap2"]

criterion = ["compio-runtime?/criterion"]

sync = ["compio-driver/sync", "compio-quic?/sync", "compio-io?/sync"]
Expand Down
Loading