13

Mark defaulted `PartialEq`/`PartialOrd` methods as const by ecstatic-morse · Pul...

 3 years ago
source link: https://github.com/rust-lang/rust/pull/91439
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Member

fee1-dead commented on Dec 2, 2021

cc @oli-obk per https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/default_method_body_is_const

Overview of #[default_method_body_is_const]

This attribute is currently a placeholder. When the const_trait_impl feature gets stabilized it should be changed to something else (preferably new syntax). When marked on a trait method with a default body, we perform checks on it as if it were a const function. This allows users to have traits that are easier to write const impls for.

trait PartialEq {
    fn eq(&self, other: &Self) -> bool;

    #[default_method_body_is_const]
    fn ne(&self, other: &Self) -> bool {
        !self.eq(other) // call to self.eq is allowed in const context because `eq` has a const implementation
    }
}

Note that we cannot use const fn as an indicator for default_method_body_is_const, because it can contradict with future extensions such as const trait fns which always require implementations to be const.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK