Skip to content

Commit 25668f8

Browse files
committed
Use 'dyn' keyword with trait objects
1 parent 5f274fc commit 25668f8

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ matrix:
1414
- rustup target add wasm32-unknown-unknown
1515
- cargo build --target=wasm32-unknown-unknown
1616
# minimum rustc version
17-
- rust: 1.26.0
17+
- rust: 1.30.0
1818
script: cargo build
1919

2020
script:

src/error.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ impl Error {
5454
}
5555

5656
/// Return a reference to the lower level, inner error.
57-
#[allow(warnings)]
58-
pub fn get_ref(&self) -> &(error::Error + 'static) {
57+
pub fn get_ref(&self) -> &(dyn error::Error + 'static) {
5958
use self::ErrorKind::*;
6059

6160
match self.inner {
@@ -91,9 +90,8 @@ impl error::Error for Error {
9190

9291
// Return any available cause from the inner error. Note the inner error is
9392
// not itself the cause.
94-
#[allow(warnings)]
95-
fn cause(&self) -> Option<&error::Error> {
96-
self.get_ref().cause()
93+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
94+
self.get_ref().source()
9795
}
9896
}
9997

src/extensions.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use std::collections::HashMap;
33
use std::hash::{BuildHasherDefault, Hasher};
44
use std::fmt;
55

6-
// `Box<Any>` is now `Box<dyn Any>`, but we can't change yet (minimum Rust)
7-
#[allow(warnings)]
8-
type AnyMap = HashMap<TypeId, Box<Any + Send + Sync>, BuildHasherDefault<IdHasher>>;
6+
type AnyMap = HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>;
97

108
// With TypeIds as keys, there's no need to hash them. They are already hashes
119
// themselves, coming from the compiler. The IdHasher just holds the u64 of
@@ -71,13 +69,10 @@ impl Extensions {
7169
.get_or_insert_with(|| Box::new(HashMap::default()))
7270
.insert(TypeId::of::<T>(), Box::new(val))
7371
.and_then(|boxed| {
74-
#[allow(warnings)]
75-
{
76-
(boxed as Box<Any + 'static>)
72+
(boxed as Box<dyn Any + 'static>)
7773
.downcast()
7874
.ok()
7975
.map(|boxed| *boxed)
80-
}
8176
})
8277
}
8378

@@ -98,12 +93,7 @@ impl Extensions {
9893
.map
9994
.as_ref()
10095
.and_then(|map| map.get(&TypeId::of::<T>()))
101-
.and_then(|boxed| {
102-
#[allow(warnings)]
103-
{
104-
(&**boxed as &(Any + 'static)).downcast_ref()
105-
}
106-
})
96+
.and_then(|boxed| (&**boxed as &(dyn Any + 'static)).downcast_ref())
10797
}
10898

10999
/// Get a mutable reference to a type previously inserted on this `Extensions`.
@@ -123,12 +113,7 @@ impl Extensions {
123113
.map
124114
.as_mut()
125115
.and_then(|map| map.get_mut(&TypeId::of::<T>()))
126-
.and_then(|boxed| {
127-
#[allow(warnings)]
128-
{
129-
(&mut **boxed as &mut (Any + 'static)).downcast_mut()
130-
}
131-
})
116+
.and_then(|boxed| (&mut **boxed as &mut (dyn Any + 'static)).downcast_mut())
132117
}
133118

134119

@@ -151,13 +136,10 @@ impl Extensions {
151136
.as_mut()
152137
.and_then(|map| map.remove(&TypeId::of::<T>()))
153138
.and_then(|boxed| {
154-
#[allow(warnings)]
155-
{
156-
(boxed as Box<Any + 'static>)
139+
(boxed as Box<dyn Any + 'static>)
157140
.downcast()
158141
.ok()
159142
.map(|boxed| *boxed)
160-
}
161143
})
162144
}
163145

0 commit comments

Comments
 (0)