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

Arc vs & in AddressSpace #7

Open
rileyshahar opened this issue Feb 20, 2023 · 1 comment
Open

Arc vs & in AddressSpace #7

rileyshahar opened this issue Feb 20, 2023 · 1 comment

Comments

@rileyshahar
Copy link
Collaborator

The methods of AddressSpace take &dyn DataSource, e.g.:

pub fn add_mapping(
&self,
source: &dyn DataSource,
offset: usize,
span: usize,
) -> Result<VirtualAddress, &str> {
todo!()
}

But the struct itself stores Arc<dyn DataSource>:

source: Arc<dyn DataSource>,

My instinct is that add_mapping should just take an Arc. The broader issue is that the methods of DataSource only have shared access to self, and there's no way to safely upcast &self to Arc<self>. So as I see it, either:

  1. The DataSource methods to have a stronger reference to self.
  2. MapType needs to store &dyn DataSource (maybe fine! The DataSource API is immutable anyway).
  3. There's some clever invariant that will make it ok to unsafely mutate &dyn DataSource into Arc<dyn DataSource>. This seems pretty unlikely to me.
@dylanmc
Copy link
Owner

dylanmc commented Feb 20, 2023

Dyn trait objects in Rust are new to me - so these are great insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants