Skip to main content

wasmtime_internal_error/
lib.rs

1//! Wasmtime's universal error handling crate.
2//!
3//! 99% API-compatible with `anyhow`, but additionally handles out-of-memory
4//! errors, instead of aborting the process.
5//!
6//! See the [`Error`] documentation for more details.
7
8#![no_std]
9#![deny(missing_docs)]
10#![doc(test(attr(deny(warnings))))]
11#![doc(test(attr(allow(dead_code, unused_variables, unused_mut))))]
12#![cfg_attr(docsrs, feature(doc_cfg))]
13
14extern crate alloc;
15#[cfg(feature = "std")]
16extern crate std;
17
18/// Internal macro to mark a block as a slow path, pulling it out into its own
19/// cold function that is never inlined.
20///
21/// This should be applied to the whole consequent/alternative block for a
22/// conditional, never to a single expression within a larger block.
23///
24/// # Example
25///
26/// ```ignore
27/// fn hot_function(x: u32) -> Result<()> {
28///     if very_rare_condition(x) {
29///         return out_of_line_slow_path!({
30///             // Handle the rare case...
31///             //
32///             // This pulls the handling of the rare condition out into
33///             // its own, separate function, which keeps the generated code
34///             // tight, handling only the common cases inline.
35///             Ok(())
36///         });
37///     }
38///
39///     // Handle the common case inline...
40///     Ok(())
41/// }
42/// ```
43macro_rules! out_of_line_slow_path {
44    ( $e:expr ) => {{
45        #[cold]
46        #[inline(never)]
47        #[track_caller]
48        fn out_of_line_slow_path<T>(f: impl FnOnce() -> T) -> T {
49            f()
50        }
51
52        out_of_line_slow_path(|| $e)
53    }};
54}
55
56#[cfg(feature = "backtrace")]
57mod backtrace;
58mod boxed;
59mod context;
60mod error;
61mod oom;
62mod ptr;
63mod vtable;
64
65#[doc(hidden)]
66pub mod macros;
67
68#[cfg(feature = "backtrace")]
69pub use backtrace::disable_backtrace;
70pub use context::Context;
71pub use error::*;
72pub use oom::OutOfMemory;
73
74/// A result of either `Ok(T)` or an [`Err(Error)`][Error].
75pub type Result<T, E = Error> = core::result::Result<T, E>;
76
77/// Return `core::result::Result::<T, wasmtime::Error>::Ok(value)`.
78///
79/// Useful in situations where Rust's type inference cannot figure out that the
80/// `Result`'s error type is [`Error`].
81#[allow(non_snake_case, reason = "matching anyhow API")]
82pub fn Ok<T>(value: T) -> Result<T> {
83    core::result::Result::Ok(value)
84}