Skip to content

Fix name ambiguity in trait object diagram #1292

@mgeisler

Description

@mgeisler

So if <Dog as Pet>::name refers to the field name in the Dog struct, what does <Cat as Pet>::name refer to?

Oh, sorry, I misunderstood above. <Dog as Pet>::name is supposed to refer to the Dog::name method.

If you called the field dogname (in the Dog struct) I think it would be clearer what's going on. Then, presumably, the label would become <Dog as Pet>::dogname.

It would make more sense if the ::name refers to the function (though the function isn't really on heap, presumably and the name: Fido would then be dogname: Fido.

Yeah, that is what the picture was supposed to show. I think "the heap" as "somewhere in memory". I guess the vtable is generated at compile-time and put somewhere in the binary — and then memory-mapped into the address space when the program is loaded from disk.

Obviously, I'm confused, but I don't think adding this information to the notes would help, because the information still seems inconsistent to me. (Sorry.)

No, I actually think you got it!

I suggest we change the name method to talk or speak in the Pet trait. The implementations can then be something like

impl Pet for Cat {
    fn speak(&self) -> String {
        String::from("Miau!")
    }
}

impl Pet for Dog {
    fn speak(&self) -> String {
        format!("Woof, my name is {}!", self.name);
    }
}

That would remove the name ambiguity. We should then also write notes to explain each part of the diagram.

Originally posted by @mgeisler in #1270 (reply in thread)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomershelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions