Skip to main content

wasmtime_internal_error/
oom.rs

1use super::OomOrDynError;
2use core::fmt;
3
4/// Out-of-memory error.
5///
6/// This error is the sentinel for allocation failure due to memory exhaustion.
7///
8/// Constructing an [`Error`][crate::Error] from an `OutOfMemory` does not
9/// allocate.
10///
11/// Allocation failure inside any `Error` method that must allocate
12/// (e.g. [`Error::context`][crate::Error::context]) will propagate an
13/// `OutOfMemory` error.
14#[derive(Clone, Copy, Default)]
15pub struct OutOfMemory {
16    _private: (),
17}
18
19impl fmt::Debug for OutOfMemory {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        f.write_str("OutOfMemory")
22    }
23}
24
25impl fmt::Display for OutOfMemory {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        f.write_str("out of memory")
28    }
29}
30
31impl core::error::Error for OutOfMemory {
32    #[inline]
33    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
34        None
35    }
36}
37
38impl OutOfMemory {
39    /// Construct a new `OutOfMemory` error.
40    ///
41    /// This operation does not allocate.
42    #[inline]
43    pub const fn new() -> Self {
44        Self { _private: () }
45    }
46}
47
48impl From<OutOfMemory> for OomOrDynError {
49    fn from(OutOfMemory { _private: () }: OutOfMemory) -> Self {
50        OomOrDynError::OOM
51    }
52}