Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature gate mmap #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ let t: [usize; 1000] =
assert_eq!(s, t);

// In this case we map the data structure into memory
//
// Note: requires the `mmap` feature.
let u: MemCase<&[usize; 1000]> =
<[usize; 1000]>::mmap(&file, Flags::empty())?;

Expand Down
7 changes: 4 additions & 3 deletions epserde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ anyhow = "1.0.79"
sealed = "0.5.0"
maligned = "0.2.1"
common_traits = "0.10.2"
mem_dbg = {version="0.2.1", features=["maligned", "mmap-rs"]}
mem_dbg = { version="0.2.4", features=["maligned", "derive"], default-features=false }

[features]
default = ["std", "mmap-rs", "derive"]
default = ["std", "mmap", "derive"]
derive = ["epserde-derive"]
std = ["alloc"]
std = ["alloc", "mem_dbg/std"]
alloc = []
mmap = ["mmap-rs", "mem_dbg/mmap-rs"]
3 changes: 3 additions & 0 deletions epserde/src/deser/mem_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl core::default::Default for Flags {

impl Flags {
/// Translates internal flags to `mmap_rs` flags.
#[cfg(feature = "mmap")]
pub(crate) fn mmap_flags(&self) -> mmap_rs::MmapFlags {
let mut flags: mmap_rs::MmapFlags = mmap_rs::MmapFlags::empty();
if self.contains(Self::SEQUENTIAL) {
Expand Down Expand Up @@ -76,6 +77,7 @@ pub enum MemBackend {
Memory(Box<[MemoryAlignment]>),
/// The backend is the result to a call to `mmap()`.
/// This variant is returned by [`crate::deser::Deserialize::load_mmap`] and [`crate::deser::Deserialize::mmap`].
#[cfg(feature = "mmap")]
Mmap(mmap_rs::Mmap),
}

Expand All @@ -89,6 +91,7 @@ impl MemBackend {
mem.len() * size_of::<MemoryAlignment>(),
)
}),
#[cfg(feature = "mmap")]
MemBackend::Mmap(mmap) => Some(mmap),
}
}
Expand Down
6 changes: 6 additions & 0 deletions epserde/src/deser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pub trait Deserialize: TypeHash + ReprHash + DeserializeInner {
///
/// The behavior of `mmap()` can be modified by passing some [`Flags`]; otherwise,
/// just pass `Flags::empty()`.
///
/// Requires the `mmap` feature.
#[cfg(feature = "mmap")]
#[allow(clippy::uninit_vec)]
fn load_mmap<'a>(
path: impl AsRef<Path>,
Expand Down Expand Up @@ -171,6 +174,9 @@ pub trait Deserialize: TypeHash + ReprHash + DeserializeInner {
///
/// The behavior of `mmap()` can be modified by passing some [`Flags`]; otherwise,
/// just pass `Flags::empty()`.
///
/// Requires the `mmap` feature.
#[cfg(feature = "mmap")]
#[allow(clippy::uninit_vec)]
fn mmap<'a>(
path: impl AsRef<Path>,
Expand Down
1 change: 1 addition & 0 deletions epserde/tests/test_memcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct Data<A> {

type Person = PersonVec<Vec<usize>, Data<Vec<u16>>>;

#[cfg(feature="mmap")]
#[test]
fn test_mem_case() {
// Create a new value to serialize
Expand Down