-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor a bit further and prepare monitors
- Loading branch information
Showing
13 changed files
with
304 additions
and
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Support for storing Rust data in Erlang terms. | ||
//! | ||
//! A NIF resource allows you to safely store Rust structs in a term, and therefore keep it across | ||
//! NIF calls. The struct will be automatically dropped when the BEAM GC decides that there are no | ||
//! more references to the resource. | ||
|
||
mod handle; | ||
mod monitor; | ||
mod registration; | ||
mod traits; | ||
mod util; | ||
|
||
use std::mem::MaybeUninit; | ||
|
||
use super::{Decoder, Error, NifResult, Term}; | ||
|
||
pub use handle::ResourceArc; | ||
pub use monitor::Monitor; | ||
pub use registration::ResourceRegistration; | ||
use rustler_sys::c_void; | ||
use traits::ResourceExt; | ||
pub use traits::{MonitorResource, Resource}; | ||
use util::align_alloced_mem_for_struct; | ||
|
||
impl<'a> Term<'a> { | ||
unsafe fn get_resource_ptrs<T: Resource>(&self) -> Option<(*const c_void, *mut T)> { | ||
let typ = T::get_resource_type()?; | ||
let mut ret_obj = MaybeUninit::uninit(); | ||
let res = rustler_sys::enif_get_resource( | ||
self.get_env().as_c_arg(), | ||
self.as_c_arg(), | ||
typ, | ||
ret_obj.as_mut_ptr(), | ||
); | ||
|
||
if res == 0 { | ||
None | ||
} else { | ||
let res = ret_obj.assume_init(); | ||
Some((res, align_alloced_mem_for_struct::<T>(res) as *mut T)) | ||
} | ||
} | ||
|
||
pub fn get_resource<T: Resource>(&self) -> Option<&'a T> { | ||
unsafe { self.get_resource_ptrs().map(|(_, ptr)| &*ptr) } | ||
} | ||
|
||
pub unsafe fn get_mut_resource<T: Resource>(&self) -> Option<&'a mut T> { | ||
self.get_resource_ptrs().map(|(_, ptr)| &mut *ptr) | ||
} | ||
} | ||
|
||
impl<'a, T> Decoder<'a> for &'a T | ||
where | ||
T: Resource + 'a, | ||
{ | ||
fn decode(term: Term<'a>) -> NifResult<Self> { | ||
term.get_resource().ok_or(Error::BadArg) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! resource { | ||
($struct_name:ty, $env: ident) => {{ | ||
impl $crate::Resource for $struct_name {} | ||
|
||
let tuple = $crate::codegen_runtime::ResourceRegistration::new::<$struct_name>( | ||
stringify!(#name) | ||
).register($env); | ||
}}; | ||
} |
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,22 @@ | ||
use rustler_sys::ErlNifMonitor; | ||
|
||
#[derive(Clone)] | ||
pub struct Monitor { | ||
inner: ErlNifMonitor, | ||
} | ||
|
||
impl Monitor { | ||
pub fn as_c_arg(&self) -> &ErlNifMonitor { | ||
&self.inner | ||
} | ||
|
||
pub fn from_c_arg(erl_nif_mon: ErlNifMonitor) -> Self { | ||
Monitor { inner: erl_nif_mon } | ||
} | ||
} | ||
|
||
impl PartialEq for Monitor { | ||
fn eq(&self, other: &Self) -> bool { | ||
unsafe { rustler_sys::enif_compare_monitors(&self.inner, &other.inner) == 0 } | ||
} | ||
} |
Oops, something went wrong.