pub unsafe trait RawPixelSized<T>: Sized {
const CHANNELS: usize;
}
unsafe impl<T> RawPixelSized<T> for [T; 1] {
const CHANNELS: usize = 1;
}
unsafe impl<T> RawPixelSized<T> for [T; 2] {
const CHANNELS: usize = 2;
}
unsafe impl<T> RawPixelSized<T> for [T; 3] {
const CHANNELS: usize = 3;
}
unsafe impl<T> RawPixelSized<T> for [T; 4] {
const CHANNELS: usize = 4;
}
pub unsafe trait RawPixel<T> {
fn channels(&self) -> usize;
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self;
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self;
fn as_ptr(&self) -> *const T;
fn as_mut_ptr(&mut self) -> *mut T;
}
unsafe impl<P: RawPixelSized<T>, T> RawPixel<T> for P {
#[inline]
fn channels(&self) -> usize {
P::CHANNELS
}
#[inline]
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self {
assert_eq!(length, Self::CHANNELS);
&*(pointer as *const Self)
}
#[inline]
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self {
assert_eq!(length, Self::CHANNELS);
&mut *(pointer as *mut Self)
}
#[inline]
fn as_ptr(&self) -> *const T {
self as *const Self as *const T
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
self as *mut Self as *mut T
}
}
unsafe impl<T> RawPixel<T> for [T] {
#[inline]
fn channels(&self) -> usize {
self.len()
}
#[inline]
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self {
::core::slice::from_raw_parts(pointer, length)
}
#[inline]
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self {
::core::slice::from_raw_parts_mut(pointer, length)
}
#[inline]
fn as_ptr(&self) -> *const T {
self.as_ptr()
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
self.as_mut_ptr()
}
}