#![no_std]
use core::{
cell::{Cell, UnsafeCell},
cmp::Ordering,
};
#[repr(transparent)]
pub struct ReadCell<T: ?Sized> {
value: UnsafeCell<T>,
}
impl<T: Copy> Clone for ReadCell<T> {
#[inline]
fn clone(&self) -> ReadCell<T> {
ReadCell::new(self.get())
}
}
impl<T: Default> Default for ReadCell<T> {
#[inline]
fn default() -> ReadCell<T> {
ReadCell::new(Default::default())
}
}
impl<T: PartialEq + Copy> PartialEq for ReadCell<T> {
#[inline]
fn eq(&self, other: &ReadCell<T>) -> bool {
self.get() == other.get()
}
}
impl<T: Eq + Copy> Eq for ReadCell<T> {}
impl<T: PartialOrd + Copy> PartialOrd for ReadCell<T> {
#[inline]
fn partial_cmp(&self, other: &ReadCell<T>) -> Option<Ordering> {
self.get().partial_cmp(&other.get())
}
#[inline]
fn lt(&self, other: &ReadCell<T>) -> bool {
self.get() < other.get()
}
#[inline]
fn le(&self, other: &ReadCell<T>) -> bool {
self.get() <= other.get()
}
#[inline]
fn gt(&self, other: &ReadCell<T>) -> bool {
self.get() > other.get()
}
#[inline]
fn ge(&self, other: &ReadCell<T>) -> bool {
self.get() >= other.get()
}
}
impl<T: Ord + Copy> Ord for ReadCell<T> {
#[inline]
fn cmp(&self, other: &ReadCell<T>) -> Ordering {
self.get().cmp(&other.get())
}
}
impl<T> From<T> for ReadCell<T> {
fn from(t: T) -> ReadCell<T> {
ReadCell::new(t)
}
}
impl<T> ReadCell<T> {
#[inline]
pub const fn new(value: T) -> ReadCell<T> {
ReadCell {
value: UnsafeCell::new(value),
}
}
pub fn into_inner(self) -> T {
self.value.into_inner()
}
}
impl<T: Copy> ReadCell<T> {
#[inline]
pub fn get(&self) -> T {
unsafe { *self.value.get() }
}
}
impl<T: ?Sized> ReadCell<T> {
#[inline]
pub const fn as_ptr(&self) -> *mut T {
self.value.get()
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
self.value.get_mut()
}
#[inline]
pub fn from_ref(t: &T) -> &ReadCell<T> {
unsafe { &*(t as *const T as *const ReadCell<T>) }
}
#[inline]
pub fn from_cell(t: &Cell<T>) -> &ReadCell<T> {
unsafe { &*(t.as_ptr() as *const ReadCell<T>) }
}
}
impl<T> ReadCell<[T]> {
pub fn as_slice_of_cells(&self) -> &[ReadCell<T>] {
unsafe { &*(self as *const ReadCell<[T]> as *const [ReadCell<T>]) }
}
}
impl<T, const N: usize> ReadCell<[T; N]> {
pub fn as_array_of_cells(&self) -> &[ReadCell<T>; N] {
unsafe { &*(self as *const ReadCell<[T; N]> as *const [ReadCell<T>; N]) }
}
}