wasmi/memory/
error.rs

1use super::MemoryType;
2use core::{fmt, fmt::Display};
3
4/// An error that may occur upon operating with virtual or linear memory.
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum MemoryError {
8    /// Tried to allocate more virtual memory than technically possible.
9    OutOfBoundsAllocation,
10    /// Tried to grow linear memory out of its set bounds.
11    OutOfBoundsGrowth,
12    /// Tried to access linear memory out of bounds.
13    OutOfBoundsAccess,
14    /// Tried to create an invalid linear memory type.
15    InvalidMemoryType,
16    /// Occurs when `ty` is not a subtype of `other`.
17    InvalidSubtype {
18        /// The [`MemoryType`] which is not a subtype of `other`.
19        ty: MemoryType,
20        /// The [`MemoryType`] which is supposed to be a supertype of `ty`.
21        other: MemoryType,
22    },
23    /// Tried to create too many memories
24    TooManyMemories,
25    /// Tried to create memory with invalid static buffer size
26    InvalidStaticBufferSize,
27}
28
29impl Display for MemoryError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        match self {
32            Self::OutOfBoundsAllocation => {
33                write!(f, "out of bounds memory allocation")
34            }
35            Self::OutOfBoundsGrowth => {
36                write!(f, "out of bounds memory growth")
37            }
38            Self::OutOfBoundsAccess => {
39                write!(f, "out of bounds memory access")
40            }
41            Self::InvalidMemoryType => {
42                write!(f, "tried to create an invalid virtual memory type")
43            }
44            Self::InvalidSubtype { ty, other } => {
45                write!(f, "memory type {ty:?} is not a subtype of {other:?}",)
46            }
47            Self::TooManyMemories => {
48                write!(f, "too many memories")
49            }
50            Self::InvalidStaticBufferSize => {
51                write!(f, "tried to use too small static buffer")
52            }
53        }
54    }
55}