Skip to content

Commit c0ceefd

Browse files
committed
Use OwnedSlice instead of owning_ref
1 parent 689beda commit c0ceefd

File tree

6 files changed

+27
-36
lines changed

6 files changed

+27
-36
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use object::{
1313
use snap::write::FrameEncoder;
1414

1515
use rustc_data_structures::memmap::Mmap;
16-
use rustc_data_structures::owning_ref::OwningRef;
17-
use rustc_data_structures::rustc_erase_owner;
16+
use rustc_data_structures::owned_slice::try_slice_owned;
1817
use rustc_data_structures::sync::MetadataRef;
1918
use rustc_metadata::fs::METADATA_FILENAME;
2019
use rustc_metadata::EncodedMetadata;
@@ -38,14 +37,14 @@ pub struct DefaultMetadataLoader;
3837

3938
fn load_metadata_with(
4039
path: &Path,
41-
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
40+
f: impl for<'a> Fn(&'a [u8]) -> Result<&'a [u8], String>,
4241
) -> Result<MetadataRef, String> {
4342
let file =
4443
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
45-
let data = unsafe { Mmap::map(file) }
46-
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))?;
47-
let metadata = OwningRef::new(data).try_map(f)?;
48-
return Ok(rustc_erase_owner!(metadata.map_owner_box()));
44+
45+
unsafe { Mmap::map(file) }
46+
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))
47+
.and_then(|mmap| try_slice_owned(mmap, |mmap| f(mmap)))
4948
}
5049

5150
impl MetadataLoader for DefaultMetadataLoader {

compiler/rustc_data_structures/src/sync.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//!
4343
//! [^2] `MTLockRef` is a typedef.
4444
45-
use crate::owning_ref::{Erased, OwningRef};
45+
use crate::owned_slice::OwnedSlice;
4646
use std::collections::HashMap;
4747
use std::hash::{BuildHasher, Hash};
4848
use std::ops::{Deref, DerefMut};
@@ -188,7 +188,7 @@ cfg_if! {
188188
}
189189
}
190190

191-
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
191+
pub type MetadataRef = OwnedSlice;
192192

193193
pub use std::rc::Rc as Lrc;
194194
pub use std::rc::Weak as Weak;
@@ -371,7 +371,7 @@ cfg_if! {
371371
});
372372
}
373373

374-
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
374+
pub type MetadataRef = OwnedSlice;
375375

376376
/// This makes locks panic if they are already held.
377377
/// It is only useful when you are running in a single thread

compiler/rustc_metadata/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ extern crate proc_macro;
2222
extern crate rustc_macros;
2323
#[macro_use]
2424
extern crate rustc_middle;
25-
#[macro_use]
26-
extern crate rustc_data_structures;
2725

2826
#[macro_use]
2927
extern crate tracing;

compiler/rustc_metadata/src/locator.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
218218

219219
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
220220
use rustc_data_structures::memmap::Mmap;
221-
use rustc_data_structures::owning_ref::OwningRef;
221+
use rustc_data_structures::owned_slice::slice_owned;
222222
use rustc_data_structures::svh::Svh;
223223
use rustc_data_structures::sync::MetadataRef;
224224
use rustc_errors::{DiagnosticArgValue, FatalError, IntoDiagnosticArg};
@@ -236,6 +236,7 @@ use rustc_target::spec::{Target, TargetTriple};
236236
use snap::read::FrameDecoder;
237237
use std::borrow::Cow;
238238
use std::io::{Read, Result as IoResult, Write};
239+
use std::ops::Deref;
239240
use std::path::{Path, PathBuf};
240241
use std::{cmp, fmt};
241242

@@ -814,15 +815,14 @@ fn get_metadata_section<'p>(
814815
// Assume the decompressed data will be at least the size of the compressed data, so we
815816
// don't have to grow the buffer as much.
816817
let mut inflated = Vec::with_capacity(compressed_bytes.len());
817-
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
818-
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
819-
Err(_) => {
820-
return Err(MetadataError::LoadFailure(format!(
821-
"failed to decompress metadata: {}",
822-
filename.display()
823-
)));
824-
}
825-
}
818+
FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated).map_err(|_| {
819+
MetadataError::LoadFailure(format!(
820+
"failed to decompress metadata: {}",
821+
filename.display()
822+
))
823+
})?;
824+
825+
slice_owned(inflated, Deref::deref)
826826
}
827827
CrateFlavor::Rmeta => {
828828
// mmap the file, because only a small fraction of it is read.
@@ -840,7 +840,7 @@ fn get_metadata_section<'p>(
840840
))
841841
})?;
842842

843-
rustc_erase_owner!(OwningRef::new(mmap).map_owner_box())
843+
slice_owned(mmap, Deref::deref)
844844
}
845845
};
846846
let blob = MetadataBlob::new(raw_bytes);

compiler/rustc_metadata/src/rmeta/decoder.rs

-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ mod cstore_impl;
5151
#[derive(Clone)]
5252
pub(crate) struct MetadataBlob(Lrc<MetadataRef>);
5353

54-
// This is needed so we can create an OwningRef into the blob.
55-
// The data behind a `MetadataBlob` has a stable address because it is
56-
// contained within an Rc/Arc.
57-
unsafe impl rustc_data_structures::owning_ref::StableAddress for MetadataBlob {}
58-
59-
// This is needed so we can create an OwningRef into the blob.
6054
impl std::ops::Deref for MetadataBlob {
6155
type Target = [u8];
6256

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::rmeta::DecodeContext;
22
use crate::rmeta::EncodeContext;
3-
use crate::rmeta::MetadataBlob;
4-
use rustc_data_structures::owning_ref::OwningRef;
3+
use rustc_data_structures::owned_slice::slice_owned;
4+
use rustc_data_structures::owned_slice::OwnedSlice;
55
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
66
use rustc_middle::parameterized_over_tcx;
77
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
88
use rustc_span::def_id::{DefIndex, DefPathHash};
99

1010
pub(crate) enum DefPathHashMapRef<'tcx> {
11-
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwningRef<MetadataBlob, [u8]>>),
11+
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice>),
1212
BorrowedFromTcx(&'tcx DefPathHashMap),
1313
}
1414

@@ -50,11 +50,11 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static>
5050

5151
let len = d.read_usize();
5252
let pos = d.position();
53-
let o = OwningRef::new(d.blob().clone()).map(|x| &x[pos..pos + len]);
53+
let o = slice_owned(d.blob().clone(), |blob| &blob[pos..pos + len]);
5454

55-
// Although we already have the data we need via the OwningRef, we still need
56-
// to advance the DecodeContext's position so it's in a valid state after
57-
// the method. We use read_raw_bytes() for that.
55+
// Although we already have the data we need via the `OwnedSlice`, we still need
56+
// to advance the `DecodeContext`'s position so it's in a valid state after
57+
// the method. We use `read_raw_bytes()` for that.
5858
let _ = d.read_raw_bytes(len);
5959

6060
let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {

0 commit comments

Comments
 (0)