Skip to content

Commit d63125c

Browse files
authored
Remove the downcast-rs dependency from wasmi_core (#1517)
* remove the downcast-rs dependency from wasmi_core * simplify downcast code
1 parent f4c321e commit d63125c

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ exclude.workspace = true
1515

1616
[dependencies]
1717
libm = { version = "0.2.11", default-features = false }
18-
downcast-rs = { version = "2.0.1", default-features = false, features = ["sync"] }
1918

2019
[features]
2120
default = ["std"]
2221
# Use `no-default-features` for a `no_std` build.
23-
std = ["downcast-rs/std"]
22+
std = []
2423
# Enables the Wasm `simd` proposal.
2524
#
2625
# This also changes the size of `UntypedVal` from 64-bit to 128-bit

crates/core/src/host_error.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use core::fmt::{Debug, Display};
2-
use downcast_rs::{impl_downcast, DowncastSync};
1+
use alloc::boxed::Box;
2+
use core::{
3+
any::{type_name, Any},
4+
fmt::{Debug, Display},
5+
};
36

47
/// Trait that allows the host to return custom error.
58
///
@@ -56,5 +59,47 @@ use downcast_rs::{impl_downcast, DowncastSync};
5659
/// _ => panic!(),
5760
/// }
5861
/// ```
59-
pub trait HostError: 'static + Display + Debug + DowncastSync {}
60-
impl_downcast!(HostError);
62+
pub trait HostError: 'static + Display + Debug + Any + Send + Sync {}
63+
64+
impl dyn HostError {
65+
/// Returns `true` if `self` is of type `T`.
66+
pub fn is<T: HostError>(&self) -> bool {
67+
(self as &dyn Any).is::<T>()
68+
}
69+
70+
/// Downcasts the [`HostError`] into a shared reference to a `T` if possible.
71+
///
72+
/// Returns `None` otherwise.
73+
#[inline]
74+
pub fn downcast_ref<T: HostError>(&self) -> Option<&T> {
75+
(self as &dyn Any).downcast_ref::<T>()
76+
}
77+
78+
/// Downcasts the [`HostError`] into an exclusive reference to a `T` if possible.
79+
///
80+
/// Returns `None` otherwise.
81+
#[inline]
82+
pub fn downcast_mut<T: HostError>(&mut self) -> Option<&mut T> {
83+
(self as &mut dyn Any).downcast_mut::<T>()
84+
}
85+
86+
/// Consumes `self` to downcast the [`HostError`] into the `T` if possible.
87+
///
88+
/// # Errors
89+
///
90+
/// If `self` cannot be downcast to `T`.
91+
#[inline]
92+
pub fn downcast<T: HostError>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
93+
if self.is::<T>() {
94+
let Ok(value) = (self as Box<dyn Any>).downcast::<T>() else {
95+
unreachable!(
96+
"failed to downcast `HostError` to T (= {})",
97+
type_name::<T>()
98+
);
99+
};
100+
Ok(value)
101+
} else {
102+
Err(self)
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)