2018-10-22 23:15:18 +00:00
|
|
|
use std::ops::{Index, IndexMut};
|
|
|
|
use std::ptr::NonNull;
|
2018-10-14 11:59:11 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct UncheckedSlice<T> {
|
2018-10-22 23:15:18 +00:00
|
|
|
pub ptr: NonNull<T>,
|
2018-10-14 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> UncheckedSlice<T> {
|
|
|
|
#[inline]
|
2018-10-22 23:15:18 +00:00
|
|
|
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
|
2018-10-14 11:59:11 +00:00
|
|
|
let ptr = self.ptr.as_ptr();
|
|
|
|
&*ptr.add(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-10-22 23:15:18 +00:00
|
|
|
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
|
2018-10-14 11:59:11 +00:00
|
|
|
let ptr = self.ptr.as_ptr();
|
2018-10-14 21:48:59 +00:00
|
|
|
&mut *(ptr.add(index) as *mut _)
|
2018-10-14 11:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn dangling() -> UncheckedSlice<T> {
|
|
|
|
UncheckedSlice {
|
|
|
|
ptr: NonNull::dangling(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ptr(&self) -> *const T {
|
|
|
|
self.ptr.as_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut T {
|
|
|
|
self.ptr.as_ptr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> From<&'a [T]> for UncheckedSlice<T> {
|
|
|
|
fn from(slice: &[T]) -> UncheckedSlice<T> {
|
|
|
|
let ptr: NonNull<[T]> = slice.into();
|
2018-10-14 21:48:59 +00:00
|
|
|
UncheckedSlice { ptr: ptr.cast() }
|
2018-10-14 11:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BoundedSlice<T> {
|
|
|
|
data: UncheckedSlice<T>,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BoundedSlice<T> {
|
|
|
|
pub fn get(&self, index: usize) -> Option<&T> {
|
|
|
|
if index < self.len {
|
2018-10-14 21:48:59 +00:00
|
|
|
unsafe { Some(self.data.get_unchecked(index)) }
|
2018-10-14 11:59:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
|
|
|
|
if index < self.len {
|
2018-10-14 21:48:59 +00:00
|
|
|
unsafe { Some(self.data.get_unchecked_mut(index)) }
|
2018-10-14 11:59:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Index<usize> for BoundedSlice<T> {
|
|
|
|
type Output = T;
|
|
|
|
fn index(&self, index: usize) -> &T {
|
|
|
|
self.get(index)
|
|
|
|
.expect(&format!("index: {} was out of bounds.", index))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<usize> for BoundedSlice<T> {
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut T {
|
|
|
|
self.get_mut(index)
|
|
|
|
.expect(&format!("index: {} was out of bounds.", index))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> From<&'a [T]> for BoundedSlice<T> {
|
|
|
|
fn from(slice: &[T]) -> BoundedSlice<T> {
|
|
|
|
BoundedSlice {
|
|
|
|
data: slice.into(),
|
|
|
|
len: slice.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|