-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* odra reexports odra_macros * Generate execute_* functions * generate a module with execute_* functions * add get_name_arg() function `ContractContext` * update odra-vm * update odra-casper * add try-from-macro crate * add TryFromRef derive macro that generate TryFrom<&'_ SourceStruct> implementation * add UnwrapOrRevert
- Loading branch information
Showing
30 changed files
with
530 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::{ContractEnv, ExecutionError, OdraError}; | ||
|
||
/// A trait that allows safe unwrapping in the context of a smart contract. | ||
/// On failure the contract does not panic, but reverts calling [`ContractEnv::revert`](crate::ContractEnv::revert()). | ||
/// Works with `Result` and `Option`. | ||
pub trait UnwrapOrRevert<T> { | ||
/// On success, unwraps the value into its inner type, | ||
/// on failure, calls [`ContractEnv::revert`](crate::ContractEnv::revert()) with the passed error. | ||
fn unwrap_or_revert_with<E: Into<OdraError>>(self, env: &ContractEnv, err: E) -> T; | ||
/// On success, unwraps the value into its inner type, | ||
/// on failure, calls [`ContractEnv::revert`](crate::ContractEnv::revert()) with the default error. | ||
fn unwrap_or_revert(self, env: &ContractEnv) -> T; | ||
} | ||
|
||
impl<T, E: Into<OdraError>> UnwrapOrRevert<T> for Result<T, E> { | ||
fn unwrap_or_revert_with<F: Into<OdraError>>(self, env: &ContractEnv, err: F) -> T { | ||
self.unwrap_or_else(|_| env.revert(err)) | ||
} | ||
|
||
fn unwrap_or_revert(self, env: &ContractEnv) -> T { | ||
self.unwrap_or_else(|err| env.revert(err)) | ||
} | ||
} | ||
|
||
impl<T> UnwrapOrRevert<T> for Option<T> { | ||
fn unwrap_or_revert_with<E: Into<OdraError>>(self, env: &ContractEnv, err: E) -> T { | ||
self.unwrap_or_else(|| env.revert(err)) | ||
} | ||
|
||
fn unwrap_or_revert(self, env: &ContractEnv) -> T { | ||
self.unwrap_or_else(|| env.revert(ExecutionError::UnwrapError)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.