diff --git a/Cargo.toml b/Cargo.toml index cb6931bb..4973a3b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "memmap" # NB: When modifying, also modify html_root_url in lib.rs -version = "0.7.0" +version = "0.7.1" authors = ["Dan Burkert "] license = "MIT/Apache-2.0" repository = "https://github.com/danburkert/memmap-rs" @@ -13,6 +13,9 @@ keywords = ["mmap", "memory-map", "io", "file"] travis-ci = { repository = "danburkert/memmap-rs" } appveyor = { repository = "danburkert/mmap" } +[dependencies] +stable_deref_trait = "1.2.0" + [target.'cfg(unix)'.dependencies] libc = "0.2" @@ -21,3 +24,4 @@ winapi = { version = "0.3", features = ["basetsd", "handleapi", "memoryapi", "mi [dev-dependencies] tempdir = "0.3" +owning_ref = "0.4.1" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 24b6d422..91ca62ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ //! A cross-platform Rust API for memory mapped buffers. -#![doc(html_root_url = "https://docs.rs/memmap/0.7.0")] +#![doc(html_root_url = "https://docs.rs/memmap/0.7.1")] #[cfg(windows)] extern crate winapi; @@ -21,6 +21,9 @@ use std::ops::{Deref, DerefMut}; use std::slice; use std::usize; +extern crate stable_deref_trait; +use stable_deref_trait::StableDeref; + /// A memory map builder, providing advanced options and flags for specifying memory map behavior. /// /// `MmapOptions` can be used to create an anonymous memory map using [`map_anon()`], or a @@ -411,6 +414,8 @@ impl Mmap { } } +unsafe impl StableDeref for Mmap {} + impl Deref for Mmap { type Target = [u8]; @@ -640,6 +645,8 @@ impl MmapMut { } } +unsafe impl StableDeref for MmapMut {} + impl Deref for MmapMut { type Target = [u8]; @@ -1057,4 +1064,21 @@ mod test { let mmap = mmap.make_exec().expect("make_exec"); drop(mmap); } + + /// Something that relies on StableDeref + #[test] + fn owning_ref() { + extern crate owning_ref; + + let mut map = MmapMut::map_anon(128).unwrap(); + map[10] = 42; + let owning = owning_ref::OwningRef::new(map); + let sliced = owning.map(|map| &map[10..20]); + assert_eq!(42, sliced[0]); + + let map = sliced.into_owner().make_read_only().unwrap(); + let owning = owning_ref::OwningRef::new(map); + let sliced = owning.map(|map| &map[10..20]); + assert_eq!(42, sliced[0]); + } }