Skip to content

Commit

Permalink
Split const trait method test and impl ops::Add
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstatic-morse committed Feb 19, 2020
1 parent 323ff19 commit d6d6d25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Expand Up @@ -19,7 +19,7 @@ impl Plus for u32 {
}

pub const fn add_i32(a: i32, b: i32) -> i32 {
a.plus(b)
a.plus(b) // ok
}

pub const fn add_u32(a: u32, b: u32) -> u32 {
Expand Down
@@ -1,5 +1,5 @@
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/call-const-trait-method.rs:26:5
--> $DIR/call-const-trait-method-fail.rs:26:5
|
LL | a.plus(b)
| ^^^^^^^^^
Expand Down
@@ -0,0 +1,41 @@
// run-pass

#![allow(incomplete_features)]
#![feature(const_trait_impl)]
#![feature(const_fn)]

struct Int(i32);

impl const std::ops::Add for Int {
type Output = Int;

fn add(self, rhs: Self) -> Self {
Int(self.0.plus(rhs.0))
}
}

impl const PartialEq for Int {
fn eq(&self, rhs: &Self) -> bool {
self.0 == rhs.0
}
}

pub trait Plus {
fn plus(self, rhs: Self) -> Self;
}

impl const Plus for i32 {
fn plus(self, rhs: Self) -> Self {
self + rhs
}
}

pub const fn add_i32(a: i32, b: i32) -> i32 {
a.plus(b)
}

const ADD_INT: Int = Int(1i32) + Int(2i32);

fn main() {
assert!(ADD_INT == Int(3i32));
}

0 comments on commit d6d6d25

Please sign in to comment.