]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_data_structures/src/memmap.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / compiler / rustc_data_structures / src / memmap.rs
CommitLineData
cdc7bbd5
XL
1use std::fs::File;
2use std::io;
064997fb 3use std::ops::{Deref, DerefMut};
cdc7bbd5
XL
4
5use crate::owning_ref::StableAddress;
6
7/// A trivial wrapper for [`memmap2::Mmap`] that implements [`StableAddress`].
8#[cfg(not(target_arch = "wasm32"))]
9pub struct Mmap(memmap2::Mmap);
10
11#[cfg(target_arch = "wasm32")]
12pub struct Mmap(Vec<u8>);
13
14#[cfg(not(target_arch = "wasm32"))]
15impl Mmap {
16 #[inline]
17 pub unsafe fn map(file: File) -> io::Result<Self> {
18 memmap2::Mmap::map(&file).map(Mmap)
19 }
20}
21
22#[cfg(target_arch = "wasm32")]
23impl Mmap {
24 #[inline]
25 pub unsafe fn map(mut file: File) -> io::Result<Self> {
26 use std::io::Read;
27
28 let mut data = Vec::new();
29 file.read_to_end(&mut data)?;
30 Ok(Mmap(data))
31 }
32}
33
34impl Deref for Mmap {
35 type Target = [u8];
36
37 #[inline]
38 fn deref(&self) -> &[u8] {
487cf647
FG
39 &self.0
40 }
41}
42
43impl AsRef<[u8]> for Mmap {
44 fn as_ref(&self) -> &[u8] {
cdc7bbd5
XL
45 &*self.0
46 }
47}
48
49// SAFETY: On architectures other than WASM, mmap is used as backing storage. The address of this
50// memory map is stable. On WASM, `Vec<u8>` is used as backing storage. The `Mmap` type doesn't
51// export any function that can cause the `Vec` to be re-allocated. As such the address of the
52// bytes inside this `Vec` is stable.
53unsafe impl StableAddress for Mmap {}
064997fb
FG
54
55#[cfg(not(target_arch = "wasm32"))]
56pub struct MmapMut(memmap2::MmapMut);
57
58#[cfg(target_arch = "wasm32")]
59pub struct MmapMut(Vec<u8>);
60
61#[cfg(not(target_arch = "wasm32"))]
62impl MmapMut {
63 #[inline]
64 pub fn map_anon(len: usize) -> io::Result<Self> {
65 let mmap = memmap2::MmapMut::map_anon(len)?;
66 Ok(MmapMut(mmap))
67 }
68
69 #[inline]
70 pub fn flush(&mut self) -> io::Result<()> {
71 self.0.flush()
72 }
73
74 #[inline]
75 pub fn make_read_only(self) -> std::io::Result<Mmap> {
76 let mmap = self.0.make_read_only()?;
77 Ok(Mmap(mmap))
78 }
79}
80
81#[cfg(target_arch = "wasm32")]
82impl MmapMut {
83 #[inline]
84 pub fn map_anon(len: usize) -> io::Result<Self> {
85 let data = Vec::with_capacity(len);
86 Ok(MmapMut(data))
87 }
88
89 #[inline]
90 pub fn flush(&mut self) -> io::Result<()> {
91 Ok(())
92 }
93
94 #[inline]
95 pub fn make_read_only(self) -> std::io::Result<Mmap> {
96 Ok(Mmap(self.0))
97 }
98}
99
100impl Deref for MmapMut {
101 type Target = [u8];
102
103 #[inline]
104 fn deref(&self) -> &[u8] {
487cf647 105 &self.0
064997fb
FG
106 }
107}
108
109impl DerefMut for MmapMut {
110 #[inline]
111 fn deref_mut(&mut self) -> &mut [u8] {
487cf647 112 &mut self.0
064997fb
FG
113 }
114}