@@ -18,11 +18,24 @@ pub struct Instances<T> {
18
18
19
19
impl < T > Instances < T > {
20
20
pub fn new ( instance : T ) -> Self {
21
+ Self :: from ( Instance :: new ( instance) )
22
+ }
23
+
24
+ pub fn empty ( ) -> Self {
25
+ Self {
26
+ instance : Vec :: new ( ) ,
27
+ transform : Vec :: new ( ) ,
28
+ alpha_blending : Vec :: new ( ) ,
29
+ source_node_id : Vec :: new ( ) ,
30
+ }
31
+ }
32
+
33
+ pub fn with_capacity ( capacity : usize ) -> Self {
21
34
Self {
22
- instance : vec ! [ instance ] ,
23
- transform : vec ! [ DAffine2 :: IDENTITY ] ,
24
- alpha_blending : vec ! [ AlphaBlending :: default ( ) ] ,
25
- source_node_id : vec ! [ None ] ,
35
+ instance : Vec :: with_capacity ( capacity ) ,
36
+ transform : Vec :: with_capacity ( capacity ) ,
37
+ alpha_blending : Vec :: with_capacity ( capacity ) ,
38
+ source_node_id : Vec :: with_capacity ( capacity ) ,
26
39
}
27
40
}
28
41
@@ -121,7 +134,7 @@ impl<T> Default for Instances<T> {
121
134
}
122
135
}
123
136
124
- impl < T : Hash > core :: hash :: Hash for Instances < T > {
137
+ impl < T : Hash > Hash for Instances < T > {
125
138
fn hash < H : core:: hash:: Hasher > ( & self , state : & mut H ) {
126
139
for instance in & self . instance {
127
140
instance. hash ( state) ;
@@ -140,6 +153,29 @@ unsafe impl<T: StaticType + 'static> StaticType for Instances<T> {
140
153
type Static = Instances < T > ;
141
154
}
142
155
156
+ impl < T > From < Instance < T > > for Instances < T > {
157
+ fn from ( instance : Instance < T > ) -> Self {
158
+ Self {
159
+ instance : vec ! [ instance. instance] ,
160
+ transform : vec ! [ instance. transform] ,
161
+ alpha_blending : vec ! [ instance. alpha_blending] ,
162
+ source_node_id : vec ! [ instance. source_node_id] ,
163
+ }
164
+ }
165
+ }
166
+
167
+ impl < T > FromIterator < Instance < T > > for Instances < T > {
168
+ fn from_iter < I : IntoIterator < Item = Instance < T > > > ( iter : I ) -> Self {
169
+ let iter = iter. into_iter ( ) ;
170
+ let ( lower, _) = iter. size_hint ( ) ;
171
+ let mut instances = Self :: with_capacity ( lower) ;
172
+ for instance in iter {
173
+ instances. push ( instance) ;
174
+ }
175
+ instances
176
+ }
177
+ }
178
+
143
179
fn one_daffine2_default ( ) -> Vec < DAffine2 > {
144
180
vec ! [ DAffine2 :: IDENTITY ]
145
181
}
@@ -189,6 +225,15 @@ pub struct Instance<T> {
189
225
}
190
226
191
227
impl < T > Instance < T > {
228
+ pub fn new ( instance : T ) -> Self {
229
+ Self {
230
+ instance,
231
+ transform : DAffine2 :: IDENTITY ,
232
+ alpha_blending : AlphaBlending :: default ( ) ,
233
+ source_node_id : None ,
234
+ }
235
+ }
236
+
192
237
pub fn to_graphic_element < U > ( self ) -> Instance < U >
193
238
where
194
239
T : Into < U > ,
0 commit comments