Skip to content

Allow trait-less impls on primitive types when in the corresponding libcore module for each type #6065

Closed
@bstrie

Description

@bstrie
Contributor

I filed #6045 before I realized that you have to make useless traits to implement methods on primitives. However, we already use lang items to identify core::char as the "canonical" char module, yes? Is it possible then to allow impls directly upon char from within core::char, since we know that it won't have anonymous impls anywhere else? This is what I'd like to be able to do:

impl char {
    fn anything() { ... }
}

without having to make a useless trait first:

trait UselessCharTrait {
    fn anything();
}

impl UselessCharTrait for char {
    fn anything() { ... }
}

This is somewhat more pertinent now that #3048 is WONTFIX.

Activity

emberian

emberian commented on Jul 7, 2013

@emberian
Member

Still relevant. I thought a potential solution was to move the builtin types to std::unstable, and do like struct char(std::unstable::builtin_char), but @thestinger pointed out newtypes were still buggy. He came up with the idea of #[lang="char"] struct char {priv c: u32}

thestinger

thestinger commented on Sep 9, 2013

@thestinger
Contributor

This would be incredibly nice to have, in order to clear out a bunch of useless one use traits.

eddyb

eddyb commented on Dec 29, 2013

@eddyb
Member

I would rather have the compiler provide the type, but it could be "owned" by the crate which defines it.

#[lang="char"] struct char;
// `char` is now a type defined in the same crate,
// but behaves just like the current built-in type.
impl char {...}
rust-highfive

rust-highfive commented on Sep 24, 2014

@rust-highfive
Contributor

This issue has been moved to the RFCs repo: rust-lang/rfcs#286

added a commit that references this issue on Sep 24, 2020

Auto merge of rust-lang#6069 - alex-700:redundant-pattern-matching-in…

29b12f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @eddyb@emberian@bstrie@thestinger@rust-highfive

        Issue actions

          Allow trait-less impls on primitive types when in the corresponding libcore module for each type · Issue #6065 · rust-lang/rust