-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[difftest] move DPI call logic to DpiTarget
- Loading branch information
Showing
3 changed files
with
78 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::sync::Mutex; | ||
|
||
pub struct DpiTarget<T> { | ||
target: Mutex<Option<T>>, | ||
} | ||
|
||
impl<T> DpiTarget<T> { | ||
pub const fn new() -> Self { | ||
Self { target: Mutex::new(None) } | ||
} | ||
|
||
#[track_caller] | ||
pub fn init(&self, init_fn: impl FnOnce() -> T) { | ||
let mut target = self.target.lock().unwrap(); | ||
if target.is_some() { | ||
panic!("DpiTarget is already initialized"); | ||
} | ||
*target = Some(init_fn()); | ||
} | ||
|
||
#[track_caller] | ||
pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R { | ||
let mut target = self.target.lock().unwrap(); | ||
let target = target.as_mut().expect("DpiTarget is not initialized"); | ||
f(target) | ||
} | ||
|
||
#[track_caller] | ||
pub fn with_optional<R>(&self, f: impl FnOnce(Option<&mut T>) -> R) -> R { | ||
let mut target = self.target.lock().unwrap(); | ||
f(target.as_mut()) | ||
} | ||
|
||
#[track_caller] | ||
pub fn dispose(&self) { | ||
let mut target = self.target.lock().unwrap(); | ||
if target.is_none() { | ||
panic!("DpiTarget is not initialized"); | ||
} | ||
*target = None; | ||
} | ||
} |
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