pub trait ErrorCode:
Copy
+ Send
+ Sync
+ 'static {
// Required methods
fn to_i32(self) -> i32;
fn from_i32(value: i32) -> Option<Self>;
fn name(self) -> &'static str;
}Expand description
A type whose values are error codes.
A contract declares the codes its callers may branch on as a protobuf
enum next to its service. Every enum that protoc-gen-buffa
generates is an ErrorCode already, through buffa::Enumeration. A
Rust enum implements the three methods by hand.
use protocontract::{Error, ErrorCode};
#[derive(Clone, Copy, Debug, PartialEq)]
enum StoreCode {
Full = 1,
}
impl ErrorCode for StoreCode {
fn to_i32(self) -> i32 {
self as i32
}
fn from_i32(value: i32) -> Option<StoreCode> {
(value == 1).then_some(StoreCode::Full)
}
fn name(self) -> &'static str {
"STORE_FULL"
}
}
let error = Error::new(StoreCode::Full, "no room for the record");
assert_eq!(error.code_as::<StoreCode>(), Some(StoreCode::Full));Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".