1
Fork 0

Use OwnedSlice instead of owning_ref

This commit is contained in:
Maybe Waffle 2023-04-05 13:26:26 +00:00
parent 689beda166
commit c0ceefdfaf
6 changed files with 27 additions and 36 deletions

View file

@ -13,8 +13,7 @@ use object::{
use snap::write::FrameEncoder; use snap::write::FrameEncoder;
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owned_slice::try_slice_owned;
use rustc_data_structures::rustc_erase_owner;
use rustc_data_structures::sync::MetadataRef; use rustc_data_structures::sync::MetadataRef;
use rustc_metadata::fs::METADATA_FILENAME; use rustc_metadata::fs::METADATA_FILENAME;
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
@ -38,14 +37,14 @@ pub struct DefaultMetadataLoader;
fn load_metadata_with( fn load_metadata_with(
path: &Path, path: &Path,
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>, f: impl for<'a> Fn(&'a [u8]) -> Result<&'a [u8], String>,
) -> Result<MetadataRef, String> { ) -> Result<MetadataRef, String> {
let file = let file =
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?; File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
let data = unsafe { Mmap::map(file) }
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))?; unsafe { Mmap::map(file) }
let metadata = OwningRef::new(data).try_map(f)?; .map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))
return Ok(rustc_erase_owner!(metadata.map_owner_box())); .and_then(|mmap| try_slice_owned(mmap, |mmap| f(mmap)))
} }
impl MetadataLoader for DefaultMetadataLoader { impl MetadataLoader for DefaultMetadataLoader {

View file

@ -42,7 +42,7 @@
//! //!
//! [^2] `MTLockRef` is a typedef. //! [^2] `MTLockRef` is a typedef.
use crate::owning_ref::{Erased, OwningRef}; use crate::owned_slice::OwnedSlice;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::{BuildHasher, Hash}; use std::hash::{BuildHasher, Hash};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -188,7 +188,7 @@ cfg_if! {
} }
} }
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>; pub type MetadataRef = OwnedSlice;
pub use std::rc::Rc as Lrc; pub use std::rc::Rc as Lrc;
pub use std::rc::Weak as Weak; pub use std::rc::Weak as Weak;
@ -371,7 +371,7 @@ cfg_if! {
}); });
} }
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>; pub type MetadataRef = OwnedSlice;
/// This makes locks panic if they are already held. /// This makes locks panic if they are already held.
/// It is only useful when you are running in a single thread /// It is only useful when you are running in a single thread

View file

@ -22,8 +22,6 @@ extern crate proc_macro;
extern crate rustc_macros; extern crate rustc_macros;
#[macro_use] #[macro_use]
extern crate rustc_middle; extern crate rustc_middle;
#[macro_use]
extern crate rustc_data_structures;
#[macro_use] #[macro_use]
extern crate tracing; extern crate tracing;

View file

@ -218,7 +218,7 @@ use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owned_slice::slice_owned;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef; use rustc_data_structures::sync::MetadataRef;
use rustc_errors::{DiagnosticArgValue, FatalError, IntoDiagnosticArg}; use rustc_errors::{DiagnosticArgValue, FatalError, IntoDiagnosticArg};
@ -236,6 +236,7 @@ use rustc_target::spec::{Target, TargetTriple};
use snap::read::FrameDecoder; use snap::read::FrameDecoder;
use std::borrow::Cow; use std::borrow::Cow;
use std::io::{Read, Result as IoResult, Write}; use std::io::{Read, Result as IoResult, Write};
use std::ops::Deref;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{cmp, fmt}; use std::{cmp, fmt};
@ -814,15 +815,14 @@ fn get_metadata_section<'p>(
// Assume the decompressed data will be at least the size of the compressed data, so we // Assume the decompressed data will be at least the size of the compressed data, so we
// don't have to grow the buffer as much. // don't have to grow the buffer as much.
let mut inflated = Vec::with_capacity(compressed_bytes.len()); let mut inflated = Vec::with_capacity(compressed_bytes.len());
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) { FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated).map_err(|_| {
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()), MetadataError::LoadFailure(format!(
Err(_) => {
return Err(MetadataError::LoadFailure(format!(
"failed to decompress metadata: {}", "failed to decompress metadata: {}",
filename.display() filename.display()
))); ))
} })?;
}
slice_owned(inflated, Deref::deref)
} }
CrateFlavor::Rmeta => { CrateFlavor::Rmeta => {
// mmap the file, because only a small fraction of it is read. // mmap the file, because only a small fraction of it is read.
@ -840,7 +840,7 @@ fn get_metadata_section<'p>(
)) ))
})?; })?;
rustc_erase_owner!(OwningRef::new(mmap).map_owner_box()) slice_owned(mmap, Deref::deref)
} }
}; };
let blob = MetadataBlob::new(raw_bytes); let blob = MetadataBlob::new(raw_bytes);

View file

@ -51,12 +51,6 @@ mod cstore_impl;
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct MetadataBlob(Lrc<MetadataRef>); pub(crate) struct MetadataBlob(Lrc<MetadataRef>);
// This is needed so we can create an OwningRef into the blob.
// The data behind a `MetadataBlob` has a stable address because it is
// contained within an Rc/Arc.
unsafe impl rustc_data_structures::owning_ref::StableAddress for MetadataBlob {}
// This is needed so we can create an OwningRef into the blob.
impl std::ops::Deref for MetadataBlob { impl std::ops::Deref for MetadataBlob {
type Target = [u8]; type Target = [u8];

View file

@ -1,14 +1,14 @@
use crate::rmeta::DecodeContext; use crate::rmeta::DecodeContext;
use crate::rmeta::EncodeContext; use crate::rmeta::EncodeContext;
use crate::rmeta::MetadataBlob; use rustc_data_structures::owned_slice::slice_owned;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owned_slice::OwnedSlice;
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap}; use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
use rustc_middle::parameterized_over_tcx; use rustc_middle::parameterized_over_tcx;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::def_id::{DefIndex, DefPathHash}; use rustc_span::def_id::{DefIndex, DefPathHash};
pub(crate) enum DefPathHashMapRef<'tcx> { pub(crate) enum DefPathHashMapRef<'tcx> {
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwningRef<MetadataBlob, [u8]>>), OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice>),
BorrowedFromTcx(&'tcx DefPathHashMap), BorrowedFromTcx(&'tcx DefPathHashMap),
} }
@ -50,11 +50,11 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static>
let len = d.read_usize(); let len = d.read_usize();
let pos = d.position(); let pos = d.position();
let o = OwningRef::new(d.blob().clone()).map(|x| &x[pos..pos + len]); let o = slice_owned(d.blob().clone(), |blob| &blob[pos..pos + len]);
// Although we already have the data we need via the OwningRef, we still need // Although we already have the data we need via the `OwnedSlice`, we still need
// to advance the DecodeContext's position so it's in a valid state after // to advance the `DecodeContext`'s position so it's in a valid state after
// the method. We use read_raw_bytes() for that. // the method. We use `read_raw_bytes()` for that.
let _ = d.read_raw_bytes(len); let _ = d.read_raw_bytes(len);
let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| { let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {