Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PDB to impl Send. #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/msf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ enum StreamTable<'s> {

// Given the table location, we can access the stream table itself
Available {
stream_table_view: Box<dyn SourceView<'s>>,
stream_table_view: Box<dyn SourceView<'s> + Send>,
},
}

fn view<'s>(source: &mut dyn Source<'s>, page_list: &PageList) -> Result<Box<dyn SourceView<'s>>> {
fn view<'s>(
source: &mut dyn Source<'s>,
page_list: &PageList,
) -> Result<Box<dyn SourceView<'s> + Send + Sync>> {
// view it
let view = source.view(page_list.source_slices())?;

Expand Down Expand Up @@ -120,7 +123,10 @@ mod big {
}

impl<'s, S: Source<'s>> BigMSF<'s, S> {
pub fn new(source: S, header_view: Box<dyn SourceView<'_>>) -> Result<BigMSF<'s, S>> {
pub fn new(
source: S,
header_view: Box<dyn SourceView<'_> + Send>,
) -> Result<BigMSF<'s, S>> {
let mut buf = ParseBuffer::from(header_view.as_slice());
let header: RawHeader = buf.parse()?;

Expand Down Expand Up @@ -345,7 +351,7 @@ mod small {
/// Represents a single Stream within the multi-stream file.
#[derive(Debug)]
pub struct Stream<'s> {
source_view: Box<dyn SourceView<'s>>,
source_view: Box<dyn SourceView<'s> + Send + Sync>,
}

impl<'s> Stream<'s> {
Expand Down Expand Up @@ -380,7 +386,9 @@ fn header_matches(actual: &[u8], expected: &[u8]) -> bool {
actual.len() >= expected.len() && &actual[0..expected.len()] == expected
}

pub fn open_msf<'s, S: Source<'s> + 's>(mut source: S) -> Result<Box<dyn Msf<'s, S> + 's>> {
pub fn open_msf<'s, S: Source<'s> + Send + 's>(
mut source: S,
) -> Result<Box<dyn Msf<'s, S> + Send + 's>> {
// map the header
let mut header_location = PageList::new(4096);
header_location.push(0);
Expand Down
15 changes: 13 additions & 2 deletions src/pdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const IPI_STREAM: u32 = 4;
#[derive(Debug)]
pub struct PDB<'s, S> {
/// `msf` provides access to the underlying data streams
msf: Box<dyn Msf<'s, S> + 's>,
msf: Box<dyn Msf<'s, S> + Send + 's>,

/// Memoize the `dbi::Header`, since it contains stream numbers we sometimes need
dbi_header: Option<DBIHeader>,
Expand All @@ -43,6 +43,14 @@ pub struct PDB<'s, S> {
dbi_extra_streams: Option<DBIExtraStreams>,
}

// Assert that the PDB type is Send.
const _: fn() = || {
fn assert<T: ?Sized + Send>() {}
// Use a dummy *const () for `S` as that will be !Send and !Sync.
// In practice (e.g. to make a PDB) `S` will need to be Send, but it doesn't matter here.
assert::<PDB<*const ()>>();
};

impl<'s, S: Source<'s> + 's> PDB<'s, S> {
/// Create a new `PDB` for a `Source`.
///
Expand All @@ -56,7 +64,10 @@ impl<'s, S: Source<'s> + 's> PDB<'s, S> {
/// * `Error::UnrecognizedFileFormat` if the `Source` does not appear to be a PDB file
/// * `Error::IoError` if returned by the `Source`
/// * `Error::PageReferenceOutOfRange`, `Error::InvalidPageSize` if the PDB file seems corrupt
pub fn open(source: S) -> Result<PDB<'s, S>> {
pub fn open(source: S) -> Result<PDB<'s, S>>
where
S: Send,
Comment on lines +68 to +69
Copy link
Collaborator

@mstange mstange May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried that not all consumers can provide a Send object as the parameter to PDB::open.

It would be nice if we could find a way to make PDB send whenever S is Send.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've seen ways of doing this with trait objects, though (if I am remembering correctly) they are understandably hacky/cumbersome. It would be easier if this didn't store a dyn Msf. There's only one implementation of that, so we could avoid the trait object fairly easily...

Copy link
Author

@afranchuk afranchuk May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH as of these changes, Source must be able to provide Send + Sync SourceViews, so I'd be surprised if Source was not at least Send too.

{
Ok(PDB {
msf: msf::open_msf(source)?,
dbi_header: None,
Expand Down
10 changes: 8 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ pub trait Source<'s>: fmt::Debug {
///
/// Note that the SourceView's as_slice() method cannot fail, so `view()` is the time to raise
/// IO errors.
fn view(&mut self, slices: &[SourceSlice]) -> Result<Box<dyn SourceView<'s>>, io::Error>;
fn view(
&mut self,
slices: &[SourceSlice],
) -> Result<Box<dyn SourceView<'s> + Send + Sync>, io::Error>;
}

/// An owned, droppable, read-only view of the source file which can be referenced as a byte slice.
Expand Down Expand Up @@ -81,7 +84,10 @@ impl<'s, T> Source<'s> for T
where
T: io::Read + io::Seek + fmt::Debug + 's,
{
fn view(&mut self, slices: &[SourceSlice]) -> Result<Box<dyn SourceView<'s>>, io::Error> {
fn view(
&mut self,
slices: &[SourceSlice],
) -> Result<Box<dyn SourceView<'s> + Send + Sync>, io::Error> {
let len = slices.iter().fold(0, |acc, s| acc + s.size);

let mut v = ReadView {
Expand Down