Split const trait method test and impl ops::Add
This commit is contained in:
parent
323ff193b8
commit
d6d6d25c34
3 changed files with 43 additions and 2 deletions
|
@ -19,7 +19,7 @@ impl Plus for u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn add_i32(a: i32, b: i32) -> i32 {
|
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 {
|
pub const fn add_u32(a: u32, b: u32) -> u32 {
|
|
@ -1,5 +1,5 @@
|
||||||
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
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)
|
LL | a.plus(b)
|
||||||
| ^^^^^^^^^
|
| ^^^^^^^^^
|
|
@ -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));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue