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

Can Copy types own memory (that this should track)? #22

Open
Aeledfyr opened this issue Nov 14, 2021 · 1 comment
Open

Can Copy types own memory (that this should track)? #22

Aeledfyr opened this issue Nov 14, 2021 · 1 comment

Comments

@Aeledfyr
Copy link
Owner

A blanket impl of DeepSize for all copy types would cover a significant amount of the library comparability code in this library. Can Copy types own memory, in the sense that this library tracks?

The only type that I can think of that that conflicts with this are references; they are currently defined to not measure the referenced structure, but other structures may want special logic for that.

One way to solve it would be with specialization and a default impl of DeepSize for types with Copy, but that's still a long way off. The other would be to just make a normal impl (which would be a breaking change) and hope that no one runs into cases where a specialized implementation is needed.

@matklad
Copy link

matklad commented Nov 23, 2021

Yeah, I think

impl<T: Copy> DeepSizeOf for T {
    fn deep_size_of_children(&self, context: &mut Context) -> usize {
        0
    }
}

would be really great. The problem here is that this, sadly, doesn't work due to those pesky coherence rules:

error[E0119]: conflicting implementations of trait `DeepSizeOf` for type `std::path::PathBuf`
  --> src/lib.rs:51:1
   |
51 | impl<T: Copy> DeepSizeOf for T {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::path::PathBuf`
   |
  ::: src/default_impls.rs:90:5
   |
90 |     impl DeepSizeOf for PathBuf {
   |     --------------------------- first implementation here
   |
   = note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::path::PathBuf` in future versions

BUT! It seems we can use auroref specialization in the deref macro to a similar effect!

https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md

See poc here: matklad@a8b43bd

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