Skip to content

Commit

Permalink
Simplify static mut for err
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Oct 24, 2023
1 parent a2e6292 commit 4ab4d26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::DerefMut;

use ain_contracts::{
get_transfer_domain_contract, get_transferdomain_dst20_transfer_function,
get_transferdomain_native_transfer_function, FixedContract,
Expand Down Expand Up @@ -426,18 +424,13 @@ fn evm_try_unsafe_validate_transferdomain_tx_in_template(
}

fn block_template_err_wrapper() -> &'static mut BlockTemplateWrapper {
static CELL: std::sync::OnceLock<std::sync::Mutex<BlockTemplateWrapper>> =
std::sync::OnceLock::new();
let mut val = CELL
.get_or_init(|| std::sync::Mutex::new(BlockTemplateWrapper(BlockTemplate::default())))
.lock()
.unwrap();

// We don't really care if multiple thread reinitialize or use it as long as the refs live
// So we just use unsafe mut pattern since it's purely for err condition that is intented
// to never be used
static mut CELL: std::cell::OnceCell<BlockTemplateWrapper> = std::cell::OnceCell::new();
unsafe {
#[allow(mutable_transmutes)]
std::mem::transmute::<&mut BlockTemplateWrapper, &'static mut BlockTemplateWrapper>(
val.deref_mut(),
)
let _ = CELL.get_or_init(|| BlockTemplateWrapper(BlockTemplate::default()));
CELL.get_mut().unwrap()
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod prelude;
use crate::{core::*, evm::*};
use ain_evm::blocktemplate::BlockTemplate;

#[derive(Debug)]
pub struct BlockTemplateWrapper(BlockTemplate);

#[cxx::bridge]
Expand Down

0 comments on commit 4ab4d26

Please sign in to comment.