regorus/utils/limits/
error.rs1#![allow(dead_code)]
5
6use core::{fmt, time::Duration};
7
8#[derive(Clone, Copy, PartialEq, Eq)]
10pub enum LimitError {
11 TimeLimitExceeded {
13 elapsed: Duration,
15 limit: Duration,
17 },
18 MemoryLimitExceeded {
20 usage: u64,
22 limit: u64,
24 },
25}
26
27impl fmt::Debug for LimitError {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 Self::TimeLimitExceeded { elapsed, limit } => f
31 .debug_struct("TimeLimitExceeded")
32 .field("elapsed", elapsed)
33 .field("limit", limit)
34 .finish(),
35 Self::MemoryLimitExceeded { usage, limit } => f
36 .debug_struct("MemoryLimitExceeded")
37 .field("usage", usage)
38 .field("limit", limit)
39 .finish(),
40 }
41 }
42}
43
44impl fmt::Display for LimitError {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::TimeLimitExceeded { elapsed, limit } => {
48 let elapsed_ns = elapsed.as_nanos();
49 let limit_ns = limit.as_nanos();
50 write!(
51 f,
52 "execution exceeded time limit (elapsed={}ns, limit={}ns)",
53 elapsed_ns, limit_ns
54 )
55 }
56 Self::MemoryLimitExceeded { usage, limit } => {
57 write!(
58 f,
59 "execution exceeded memory limit (usage={} bytes, limit={} bytes)",
60 usage, limit
61 )
62 }
63 }
64 }
65}
66
67impl core::error::Error for LimitError {}