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

deep_size_of_children() resolves to different implementations depending on which impl blocks deep_size_of() is called in #37

Open
wanyingluo opened this issue Aug 3, 2023 · 0 comments

Comments

@wanyingluo
Copy link

Hello, I ran into the following surprising behavior and wonder if it's intentional.

trait MyTrait {
    fn run(&mut self);
}
#[derive(deepsize::DeepSizeOf, Default)]
struct State {
    data: Vec<String>,
}
impl State {
    fn print_deepsize(&self) {
        println!(
            "[deepsize in impl block] {:p} {}",
            self,
            self.deep_size_of()
        );
    }
}
impl MyTrait for State {
    fn run(&mut self) {
        self.data.push(String::from("foobar"));
        println!(
            "[deepsize in impl XX for YY block] {:p} {}",
            self,
            self.deep_size_of()
        );
        self.print_deepsize();
    }
}
fn main() {
    let mut state = State::default();
    state.run();
}

Running this produces the following output:

[deepsize in impl XX for YY block] 0x7fff18ea0990 8
[deepsize in impl block] 0x7fff18ea0990 126

That's surprising. I expect both calls to deep_size_of() return the same result. It seems both calls resolve to the same deep_size_of() in the DeepSize library. However, the deep_size_of_children() call in it resolves to different ones. It resolves to the derived one when deep_size_of() is called in impl State, but resolves to the following one when when deep_size_of() is called in impl MyTrait for State.

impl<T> DeepSizeOf for &mut T
where
    T: DeepSizeOf +?Sized,
{
    fn deep_size_of_children(&self, _context: &mut Context) -> usize {
        0
    }
} 

Is this intended behavior or a bug in the library?

@wanyingluo wanyingluo changed the title deep_size_of_children() resolves to different implementations depending on which impl blocks it's in deep_size_of_children() resolves to different implementations depending on which impl blocks deep_size_of() is called Aug 3, 2023
@wanyingluo wanyingluo changed the title deep_size_of_children() resolves to different implementations depending on which impl blocks deep_size_of() is called deep_size_of_children() resolves to different implementations depending on which impl blocks deep_size_of() is called in Aug 4, 2023
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

1 participant