#![deny(missing_docs, missing_copy_implementations)]
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use super::{Resources, Factory};
pub trait Raw {
unsafe fn set<T>(&self, index: usize, val: T);
unsafe fn to_slice<T>(&self, len: usize) -> &[T];
unsafe fn to_mut_slice<T>(&self, len: usize) -> &mut [T];
}
pub struct Readable<'a, T: Copy + 'a, R: 'a + Resources, F: 'a + Factory<R>> where
F::Mapper: 'a
{
raw: F::Mapper,
len: usize,
factory: &'a mut F,
phantom_t: PhantomData<T>
}
impl<'a, T: Copy, R: Resources, F: Factory<R>> Deref for Readable<'a, T, R, F> where
F::Mapper: 'a
{
type Target = [T];
fn deref(&self) -> &[T] {
unsafe { self.raw.to_slice(self.len) }
}
}
impl<'a, T: Copy, R: Resources, F: Factory<R>> Drop for Readable<'a, T, R, F> where
F::Mapper: 'a
{
fn drop(&mut self) {
self.factory.unmap_buffer_raw(self.raw.clone())
}
}
pub struct Writable<'a, T: Copy, R: 'a + Resources, F: 'a + Factory<R>> where
F::Mapper: 'a
{
raw: F::Mapper,
len: usize,
factory: &'a mut F,
phantom_t: PhantomData<T>
}
impl<'a, T: Copy, R: Resources, F: Factory<R>> Writable<'a, T, R, F> where
F::Mapper: 'a
{
pub fn set(&mut self, idx: usize, val: T) {
if idx >= self.len {
panic!("Tried to write out of bounds to a WritableMapping!")
}
unsafe { self.raw.set(idx, val); }
}
}
impl<'a, T: Copy, R: Resources, F: Factory<R>> Drop for Writable<'a, T, R, F> where
F::Mapper: 'a
{
fn drop(&mut self) {
self.factory.unmap_buffer_raw(self.raw.clone())
}
}
pub struct RW<'a, T: Copy + 'a, R: 'a + Resources, F: 'a + Factory<R>> where
F::Mapper: 'a
{
raw: F::Mapper,
len: usize,
factory: &'a mut F,
phantom_t: PhantomData<T>
}
impl<'a, T: Copy + 'a, R: Resources, F: Factory<R>> Deref for RW<'a, T, R, F> where
F::Mapper: 'a
{
type Target = [T];
fn deref(&self) -> &[T] {
unsafe { self.raw.to_slice(self.len) }
}
}
impl<'a, T: Copy + 'a, R: Resources, F: Factory<R>> DerefMut for RW<'a, T, R, F> where
F::Mapper: 'a
{
fn deref_mut(&mut self) -> &mut [T] {
unsafe { self.raw.to_mut_slice(self.len) }
}
}
impl<'a, T: Copy, R: Resources, F: Factory<R>> Drop for RW<'a, T, R, F> where
F::Mapper: 'a
{
fn drop(&mut self) {
self.factory.unmap_buffer_raw(self.raw.clone())
}
}
#[allow(missing_docs)]
pub trait Builder<'a, R: Resources> {
type RawMapping: Raw;
fn map_readable<T: Copy>(&'a mut self, Self::RawMapping, usize) -> Readable<T, R, Self> where
Self: Sized + Factory<R>;
fn map_writable<T: Copy>(&'a mut self, Self::RawMapping, usize) -> Writable<T, R, Self> where
Self: Sized + Factory<R>;
fn map_read_write<T: Copy>(&'a mut self, Self::RawMapping, usize) -> RW<T, R, Self> where
Self: Sized + Factory<R>;
}
impl<'a, R: Resources, F: Factory<R>> Builder<'a, R> for F where
F::Mapper: 'a
{
type RawMapping = F::Mapper;
fn map_readable<T: Copy>(&'a mut self, map: F::Mapper,
length: usize) -> Readable<T, R, Self> {
Readable {
raw: map,
len: length,
factory: self,
phantom_t: PhantomData,
}
}
fn map_writable<T: Copy>(&'a mut self, map: F::Mapper,
length: usize) -> Writable<T, R, Self> {
Writable {
raw: map,
len: length,
factory: self,
phantom_t: PhantomData,
}
}
fn map_read_write<T: Copy>(&'a mut self, map: F::Mapper,
length: usize) -> RW<T, R, Self> {
RW {
raw: map,
len: length,
factory: self,
phantom_t: PhantomData,
}
}
}