1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![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, 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, 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 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, 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>;
fn map_writable<T: Copy>(&'a mut self, Self::RawMapping, usize)
-> Writable<T, R, Self>;
fn map_read_write<T: Copy>(&'a mut self, Self::RawMapping, usize)
-> RW<T, R, Self>;
}
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,
}
}
}