winit-core/as_any: fix Box<AsAny> casting
The casting was doing an incorrect check on the `ref` instead of actually trying to downcast a ref as `cast_ref` does. So use `cast_ref` to check whether we can safely `cast` to owned type.
This commit is contained in:
parent
e1bccb68d8
commit
0b21c55b72
1 changed files with 23 additions and 2 deletions
|
|
@ -55,8 +55,7 @@ macro_rules! impl_dyn_casting {
|
|||
///
|
||||
/// Returns `Err` with `self` if the object was not from that backend.
|
||||
pub fn cast<T: $trait>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
|
||||
let reference: &dyn std::any::Any = self.__as_any();
|
||||
if reference.is::<T>() {
|
||||
if self.cast_ref::<T>().is_some() {
|
||||
let this: Box<dyn std::any::Any> = self.__into_any();
|
||||
// Unwrap is okay, we just checked the type of `self` is `T`.
|
||||
Ok(this.downcast::<T>().unwrap())
|
||||
|
|
@ -69,3 +68,25 @@ macro_rules! impl_dyn_casting {
|
|||
}
|
||||
|
||||
pub use impl_dyn_casting;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::AsAny;
|
||||
|
||||
struct Foo;
|
||||
trait FooTrait: AsAny {}
|
||||
impl FooTrait for Foo {}
|
||||
impl_dyn_casting!(FooTrait);
|
||||
|
||||
#[test]
|
||||
fn dyn_casting() {
|
||||
let foo_owned: Box<dyn FooTrait> = Box::new(Foo);
|
||||
assert!(foo_owned.cast::<Foo>().is_ok());
|
||||
|
||||
let mut foo = Foo;
|
||||
let foo_ref: &mut dyn FooTrait = &mut foo;
|
||||
assert!((foo_ref).cast_ref::<Foo>().is_some());
|
||||
assert!((&&&&foo_ref).cast_ref::<Foo>().is_some());
|
||||
assert!(foo_ref.cast_mut::<Foo>().is_some());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue