use super::atomic::{Atomic, IntCast}; use crate::types::ValueType; use std::{cell::Cell, marker::PhantomData, ops::Deref, slice}; pub trait Atomicity {} pub struct Atomically; impl Atomicity for Atomically {} pub struct NonAtomically; impl Atomicity for NonAtomically {} pub struct MemoryView<'a, T: 'a, A = NonAtomically> { ptr: *mut T, length: usize, _phantom: PhantomData<(&'a [Cell], A)>, } impl<'a, T> MemoryView<'a, T, NonAtomically> where T: ValueType, { pub(super) unsafe fn new(ptr: *mut T, length: u32) -> Self { Self { ptr, length: length as usize, _phantom: PhantomData, } } } impl<'a, T: IntCast> MemoryView<'a, T, NonAtomically> { pub fn atomically(&self) -> MemoryView<'a, T, Atomically> { MemoryView { ptr: self.ptr, length: self.length, _phantom: PhantomData, } } } impl<'a, T> Deref for MemoryView<'a, T, NonAtomically> { type Target = [Cell]; fn deref(&self) -> &[Cell] { unsafe { slice::from_raw_parts(self.ptr as *const Cell, self.length) } } } impl<'a, T: IntCast> Deref for MemoryView<'a, T, Atomically> { type Target = [Atomic]; fn deref(&self) -> &[Atomic] { unsafe { slice::from_raw_parts(self.ptr as *const Atomic, self.length) } } }