From 3630fddcb6d4327e0a44bcd7c00b286e3c39b51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=B6ln?= Date: Wed, 15 Jan 2025 15:46:13 +0100 Subject: [PATCH] Feature gate mmap --- README.md | 2 ++ epserde/Cargo.toml | 7 ++++--- epserde/src/deser/mem_case.rs | 3 +++ epserde/src/deser/mod.rs | 6 ++++++ epserde/tests/test_memcase.rs | 1 + 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92d6363..1163ffc 100644 --- a/README.md +++ b/README.md @@ -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())?; diff --git a/epserde/Cargo.toml b/epserde/Cargo.toml index 960c24b..e1774cd 100644 --- a/epserde/Cargo.toml +++ b/epserde/Cargo.toml @@ -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"] diff --git a/epserde/src/deser/mem_case.rs b/epserde/src/deser/mem_case.rs index 667c749..dabce95 100644 --- a/epserde/src/deser/mem_case.rs +++ b/epserde/src/deser/mem_case.rs @@ -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) { @@ -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), } @@ -89,6 +91,7 @@ impl MemBackend { mem.len() * size_of::(), ) }), + #[cfg(feature = "mmap")] MemBackend::Mmap(mmap) => Some(mmap), } } diff --git a/epserde/src/deser/mod.rs b/epserde/src/deser/mod.rs index 9e9c32a..811938f 100644 --- a/epserde/src/deser/mod.rs +++ b/epserde/src/deser/mod.rs @@ -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, @@ -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, diff --git a/epserde/tests/test_memcase.rs b/epserde/tests/test_memcase.rs index c5ccbbb..83e6ad3 100644 --- a/epserde/tests/test_memcase.rs +++ b/epserde/tests/test_memcase.rs @@ -23,6 +23,7 @@ struct Data { type Person = PersonVec, Data>>; +#[cfg(feature="mmap")] #[test] fn test_mem_case() { // Create a new value to serialize