#![allow(dead_code)]
use core::fmt;
use core::time::Duration;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum LimitError {
TimeLimitExceeded {
elapsed: Duration,
limit: Duration,
},
MemoryLimitExceeded {
usage: u64,
limit: u64,
},
}
impl fmt::Debug for LimitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TimeLimitExceeded { elapsed, limit } => f
.debug_struct("TimeLimitExceeded")
.field("elapsed", elapsed)
.field("limit", limit)
.finish(),
Self::MemoryLimitExceeded { usage, limit } => f
.debug_struct("MemoryLimitExceeded")
.field("usage", usage)
.field("limit", limit)
.finish(),
}
}
}
impl fmt::Display for LimitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TimeLimitExceeded { elapsed, limit } => {
let elapsed_ns = elapsed.as_nanos();
let limit_ns = limit.as_nanos();
write!(
f,
"execution exceeded time limit (elapsed={}ns, limit={}ns)",
elapsed_ns, limit_ns
)
}
Self::MemoryLimitExceeded { usage, limit } => {
write!(
f,
"execution exceeded memory limit (usage={} bytes, limit={} bytes)",
usage, limit
)
}
}
}
}
impl core::error::Error for LimitError {}