1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use casper_types::ApiError;
use crate::contract_api::runtime;
pub trait UnwrapOrRevert<T> {
fn unwrap_or_revert(self) -> T;
fn unwrap_or_revert_with<E: Into<ApiError>>(self, error: E) -> T;
}
impl<T, E: Into<ApiError>> UnwrapOrRevert<T> for Result<T, E> {
fn unwrap_or_revert(self) -> T {
self.unwrap_or_else(|error| runtime::revert(error.into()))
}
fn unwrap_or_revert_with<F: Into<ApiError>>(self, error: F) -> T {
self.unwrap_or_else(|_| runtime::revert(error.into()))
}
}
impl<T> UnwrapOrRevert<T> for Option<T> {
fn unwrap_or_revert(self) -> T {
self.unwrap_or_else(|| runtime::revert(ApiError::None))
}
fn unwrap_or_revert_with<E: Into<ApiError>>(self, error: E) -> T {
self.unwrap_or_else(|| runtime::revert(error.into()))
}
}