use std::{
ptr::{null_mut, NonNull},
sync::atomic::{AtomicPtr, Ordering},
};
#[doc(hidden)]
pub struct UniffiForeignPointerCell<T>(AtomicPtr<T>);
impl<T> UniffiForeignPointerCell<T> {
pub const fn new() -> Self {
Self(AtomicPtr::new(null_mut()))
}
pub fn set(&self, callback: NonNull<T>) {
self.0.store(callback.as_ptr(), Ordering::Relaxed);
}
pub fn get(&self) -> &T {
unsafe {
NonNull::new(self.0.load(Ordering::Relaxed))
.expect("Foreign pointer not set. This is likely a uniffi bug.")
.as_mut()
}
}
}
impl<T> Default for UniffiForeignPointerCell<T> {
fn default() -> Self {
Self::new()
}
}
unsafe impl<T> Send for UniffiForeignPointerCell<T> {}
unsafe impl<T> Sync for UniffiForeignPointerCell<T> {}