use std::cell::UnsafeCell;
use std::slice;
use std::sync::Arc;
pub struct OutputWindow {
ptr: *mut f32,
capacity: usize,
}
impl OutputWindow {
pub fn new(ptr: *mut f32, len: usize) -> Self {
Self { ptr, capacity: len }
}
pub fn as_mut_slice(&mut self) -> &mut [f32] {
unsafe { slice::from_raw_parts_mut(self.ptr, self.capacity) }
}
pub fn capacity(&self) -> usize {
self.capacity
}
}
#[derive(Clone)]
pub struct OutputSlot(Arc<UnsafeCell<Option<OutputWindow>>>);
unsafe impl Send for OutputSlot {}
impl Default for OutputSlot {
#[allow(clippy::arc_with_non_send_sync)]
fn default() -> Self {
Self(Arc::new(UnsafeCell::new(None)))
}
}
impl OutputSlot {
#[allow(clippy::arc_with_non_send_sync)]
pub fn new() -> Self {
Self(Arc::new(UnsafeCell::new(None)))
}
pub unsafe fn set(&self, w: OutputWindow) {
*self.0.get() = Some(w);
}
pub unsafe fn clear(&self) {
*self.0.get() = None;
}
#[allow(clippy::mut_from_ref)]
pub unsafe fn as_mut(&self) -> Option<&mut OutputWindow> {
(*self.0.get()).as_mut()
}
}