From 08c93f2af08fd18f1b0d0223b54c54bd5d195515 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 10 Feb 2023 13:31:55 +0000 Subject: [PATCH] MappedFile: Added generic into_inner(), inner_mut(), and inner() accessors. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for mapped-file's current commit: Small blessing − 小吉 --- Cargo.toml | 2 +- src/lib.rs | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2f87f0..0c801bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "mapped-file" description = "Construct a memory mapping over any file object" keywords = ["unix", "mmap", "generic", "file", "fd"] -version = "0.0.6" +version = "0.0.7" edition = "2021" repository="https://github.com/notflan/mapped-file" license="MIT" diff --git a/src/lib.rs b/src/lib.rs index 870fe31..b88bd8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,36 @@ pub fn get_page_size() -> usize v as usize } +impl MappedFile { + /// A reference to the mapped backing file + #[inline] + pub fn inner(&self) -> &T + { + &self.file + } + /// A mutable reference to the mapped backing file + /// + /// # Note + /// Behaviour of faulted or unfaulted pages is not specified if the backing file is modified another way. Likewise, if the backing file is read from another way, the data mapped is not guaranteed to have been synced unless a `flush()` has completed. Be careful with this. + #[inline] + pub fn inner_mut(&mut self) -> &mut T + { + &mut self.file + } + + /// Unmap the memory contained in `T` and return it. + /// + /// # Warning + /// If the map is shared, or refers to a persistent file on disk, you should call `flush()` + /// first or use `into_inner_synced()` + #[inline] + pub fn into_inner(self) -> T + { + drop(self.map); + self.file + } +} + impl MappedFile { /// Map the file `file` to `len` bytes with memory protection as provided by `perm`, and mapping flags provided by `flags`. /// # Mapping flags @@ -429,18 +459,6 @@ impl MappedFile { }, file) } - /// Unmap the memory contained in `T` and return it. - /// - /// # Warning - /// If the map is shared, or refers to a persistent file on disk, you should call `flush()` - /// first or use `into_inner_synced()` - #[inline] - pub fn into_inner(self) -> T - { - drop(self.map); - self.file - } - /// The size of the mapped memory #[inline] pub fn len(&self) -> usize