util: Add ArcWrapped
This structure is necessary to wrap Send data structures which are actually moved between threads.
This commit is contained in:
35
src/util.rs
35
src/util.rs
@ -16,6 +16,7 @@ pub mod c {
|
||||
use std::os::raw::c_char;
|
||||
use std::rc::Rc;
|
||||
use std::str::Utf8Error;
|
||||
use std::sync::{ Arc, Mutex };
|
||||
|
||||
// traits
|
||||
|
||||
@ -129,6 +130,40 @@ pub mod c {
|
||||
}
|
||||
|
||||
impl<T> COpaquePtr for Wrapped<T> {}
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct ArcWrapped<T>(*const Mutex<T>);
|
||||
|
||||
impl<T> ArcWrapped<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self::wrap(Arc::new(Mutex::new(value)))
|
||||
}
|
||||
pub fn wrap(state: Arc<Mutex<T>>) -> Self {
|
||||
Self(Arc::into_raw(state))
|
||||
}
|
||||
|
||||
/// Extracts the reference to the data.
|
||||
/// It may cause problems if attempted in more than one place
|
||||
pub unsafe fn unwrap(self) -> Arc<Mutex<T>> {
|
||||
Arc::from_raw(self.0)
|
||||
}
|
||||
|
||||
/// Creates a new reference to the same data.
|
||||
/// Use for accessing the underlying data as a reference.
|
||||
pub fn clone_ref(&self) -> Arc<Mutex<T>> {
|
||||
// A bit dangerous: the Arc may be in use elsewhere
|
||||
let used_rc = unsafe { Arc::from_raw(self.0) };
|
||||
let rc = used_rc.clone();
|
||||
Arc::into_raw(used_rc); // prevent dropping the original reference
|
||||
rc
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for ArcWrapped<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self::wrap(self.clone_ref())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clones the underlying data structure, like ToOwned.
|
||||
|
||||
Reference in New Issue
Block a user