5 use crate::owning_ref::StableAddress;
7 /// A trivial wrapper for [`memmap2::Mmap`] that implements [`StableAddress`].
8 #[cfg(not(target_arch = "wasm32"))]
9 pub struct Mmap(memmap2::Mmap);
11 #[cfg(target_arch = "wasm32")]
12 pub struct Mmap(Vec<u8>);
14 #[cfg(not(target_arch = "wasm32"))]
17 pub unsafe fn map(file: File) -> io::Result<Self> {
18 memmap2::Mmap::map(&file).map(Mmap)
22 #[cfg(target_arch = "wasm32")]
25 pub unsafe fn map(mut file: File) -> io::Result<Self> {
28 let mut data = Vec::new();
29 file.read_to_end(&mut data)?;
38 fn deref(&self) -> &[u8] {
43 // SAFETY: On architectures other than WASM, mmap is used as backing storage. The address of this
44 // memory map is stable. On WASM, `Vec<u8>` is used as backing storage. The `Mmap` type doesn't
45 // export any function that can cause the `Vec` to be re-allocated. As such the address of the
46 // bytes inside this `Vec` is stable.
47 unsafe impl StableAddress for Mmap {}