-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get_vtable returns opt instd of unwrpping
- Loading branch information
Showing
6 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::ops::Deref; | ||
trait MyTrait: Deref<Target = u32> {} | ||
struct MyStruct(u32); | ||
impl MyTrait for MyStruct {} | ||
impl Deref for MyStruct { | ||
type Target = u32; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
fn get_concrete_value(i: u32) -> MyStruct { | ||
MyStruct(i) | ||
} | ||
fn get_boxed_value(i: u32) -> Box<dyn MyTrait> { | ||
Box::new(get_concrete_value(i)) | ||
} | ||
fn main() { | ||
let v = [1, 2, 3] | ||
.iter() | ||
.map(|i| get_boxed_value(*i)) | ||
.collect::<Vec<_>>(); | ||
|
||
let el = &v[0]; | ||
|
||
for _ in v { | ||
//~^ ERROR cannot move out of `v` because it is borrowed | ||
println!("{}", ***el > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0505]: cannot move out of `v` because it is borrowed | ||
--> $DIR/issue-97381.rs:26:14 | ||
| | ||
LL | let el = &v[0]; | ||
| - borrow of `v` occurs here | ||
LL | | ||
LL | for _ in v { | ||
| ^ move out of `v` occurs here | ||
LL | | ||
LL | println!("{}", ***el > 0); | ||
| ---- borrow later used here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0505`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// revisions: lib usage | ||
//[lib] compile-flags: --crate-type=lib | ||
//[lib] build-pass | ||
|
||
use std::ops::Sub; | ||
trait Vector2 { | ||
type ScalarType; | ||
|
||
fn from_values(x: Self::ScalarType, y: Self::ScalarType) -> Self | ||
where | ||
Self: Sized; | ||
|
||
fn x(&self) -> Self::ScalarType; | ||
fn y(&self) -> Self::ScalarType; | ||
} | ||
|
||
impl<T> Sub for dyn Vector2<ScalarType = T> | ||
where | ||
T: Sub<Output = T>, | ||
(dyn Vector2<ScalarType = T>): Sized, | ||
{ | ||
type Output = dyn Vector2<ScalarType = T>; | ||
|
||
fn sub(self, rhs: Self) -> Self::Output { | ||
Self::from_values(self.x() - rhs.x(), self.y() - rhs.y()) | ||
} | ||
} | ||
|
||
struct Vec2 { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
impl Vector2 for Vec2 { | ||
type ScalarType = i32; | ||
|
||
fn from_values(x: Self::ScalarType, y: Self::ScalarType) -> Self | ||
where | ||
Self: Sized, | ||
{ | ||
Self { x, y } | ||
} | ||
|
||
fn x(&self) -> Self::ScalarType { | ||
self.x | ||
} | ||
fn y(&self) -> Self::ScalarType { | ||
self.y | ||
} | ||
} | ||
|
||
#[cfg(usage)] | ||
fn main() { | ||
let hey: Box<dyn Vector2<ScalarType = i32>> = Box::new(Vec2 { x: 1, y: 2 }); | ||
let word: Box<dyn Vector2<ScalarType = i32>> = Box::new(Vec2 { x: 1, y: 2 }); | ||
|
||
let bar = *hey - *word; | ||
//[usage]~^ ERROR cannot subtract | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0369]: cannot subtract `(dyn Vector2<ScalarType = i32> + 'static)` from `dyn Vector2<ScalarType = i32>` | ||
--> $DIR/type-unsatisfiable.rs:57:20 | ||
| | ||
LL | let bar = *hey - *word; | ||
| ---- ^ ----- (dyn Vector2<ScalarType = i32> + 'static) | ||
| | | ||
| dyn Vector2<ScalarType = i32> | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0369`. |