Trait objects
This commit is contained in:
parent
efd0483949
commit
6469fba44e
3 changed files with 104 additions and 0 deletions
45
src/test/ui/const-generics/defaults/trait_objects.rs
Normal file
45
src/test/ui/const-generics/defaults/trait_objects.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// run-pass
|
||||
#![feature(const_generics_defaults)]
|
||||
|
||||
trait Trait<const N: u8 = 12> {
|
||||
fn uwu(&self) -> u8 {
|
||||
N
|
||||
}
|
||||
}
|
||||
|
||||
impl Trait for u32 {}
|
||||
|
||||
impl Trait<12> for u64 {
|
||||
fn uwu(&self) -> u8 {
|
||||
*self as u8
|
||||
}
|
||||
}
|
||||
|
||||
fn foo(arg: &dyn Trait) -> u8 {
|
||||
arg.uwu()
|
||||
}
|
||||
|
||||
trait Traitor<const N: u8 = 1, const M: u8 = N> {
|
||||
fn owo(&self) -> u8 {
|
||||
M
|
||||
}
|
||||
}
|
||||
|
||||
impl Traitor<2> for bool { }
|
||||
impl Traitor for u8 {
|
||||
fn owo(&self) -> u8 {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {
|
||||
arg.owo()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(foo(&10_u32), 12);
|
||||
assert_eq!(foo(&3_u64), 3);
|
||||
|
||||
assert_eq!(bar(&true), 2);
|
||||
assert_eq!(bar(&1_u8), 1);
|
||||
}
|
||||
32
src/test/ui/const-generics/defaults/trait_objects_fail.rs
Normal file
32
src/test/ui/const-generics/defaults/trait_objects_fail.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#![feature(const_generics_defaults)]
|
||||
|
||||
trait Trait<const N: u8 = 12> {
|
||||
fn uwu(&self) -> u8 {
|
||||
N
|
||||
}
|
||||
}
|
||||
|
||||
impl Trait<2> for u32 {}
|
||||
|
||||
fn foo(arg: &dyn Trait) -> u8 {
|
||||
arg.uwu()
|
||||
}
|
||||
|
||||
trait Traitor<const N: u8 = 1, const M: u8 = N> {
|
||||
fn owo(&self) -> u8 {
|
||||
M
|
||||
}
|
||||
}
|
||||
|
||||
impl Traitor<2, 3> for bool { }
|
||||
|
||||
fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {
|
||||
arg.owo()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
foo(&10_u32);
|
||||
//~^ error: the trait bound `u32: Trait` is not satisfied
|
||||
bar(&true);
|
||||
//~^ error: the trait bound `bool: Traitor<{_: u8}, {_: u8}>` is not satisfied
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
error[E0277]: the trait bound `u32: Trait` is not satisfied
|
||||
--> $DIR/trait_objects_fail.rs:28:9
|
||||
|
|
||||
LL | foo(&10_u32);
|
||||
| --- ^^^^^^^ the trait `Trait` is not implemented for `u32`
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: the following implementations were found:
|
||||
<u32 as Trait<2_u8>>
|
||||
= note: required for the cast to the object type `dyn Trait`
|
||||
|
||||
error[E0277]: the trait bound `bool: Traitor<{_: u8}, {_: u8}>` is not satisfied
|
||||
--> $DIR/trait_objects_fail.rs:30:9
|
||||
|
|
||||
LL | bar(&true);
|
||||
| --- ^^^^^ the trait `Traitor<{_: u8}, {_: u8}>` is not implemented for `bool`
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: the following implementations were found:
|
||||
<bool as Traitor<2_u8, 3_u8>>
|
||||
= note: required for the cast to the object type `dyn Traitor<{_: u8}, {_: u8}>`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue