Skip to content

Overriding default ToStr impl for @A where A: ToStr #4851

@pkgw

Description

@pkgw

I'm running into an issue with an interaction between ToStr and pointers that I'm not sure how to handle. I have a struct with a ToStr, and I want to call that method on a @-pointer to the struct. However, to_str.rs provides a generic implementation:

impl<A: ToStr> @A: ToStr { ... }

that returns the to_str prefixed with "@". I do not want this behavior, but I can't figure out how to override it. My naive implementation is:

#[crate_type = "bin"];

pub struct Foo {
    v: uint,
}

impl Foo: to_str::ToStr {
    pure fn to_str (&self) -> ~str { ~"stacky" }
}

impl @Foo: to_str::ToStr {
    pure fn to_str (&self) -> ~str { ~"at-pointer" }
}

fn main () {
    let q = @Foo { v: 0 };
    io::print (q.to_str () + "\n");
}

But this gives a compile error about multiple implementations. Is there a way around this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions