Skip to content

Commit eb8605a

Browse files
committed
Instances extension
1 parent 6f45560 commit eb8605a

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

node-graph/gcore/src/instances.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,24 @@ pub struct Instances<T> {
1818

1919
impl<T> Instances<T> {
2020
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 {
2134
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),
2639
}
2740
}
2841

@@ -121,7 +134,7 @@ impl<T> Default for Instances<T> {
121134
}
122135
}
123136

124-
impl<T: Hash> core::hash::Hash for Instances<T> {
137+
impl<T: Hash> Hash for Instances<T> {
125138
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
126139
for instance in &self.instance {
127140
instance.hash(state);
@@ -140,6 +153,29 @@ unsafe impl<T: StaticType + 'static> StaticType for Instances<T> {
140153
type Static = Instances<T>;
141154
}
142155

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+
143179
fn one_daffine2_default() -> Vec<DAffine2> {
144180
vec![DAffine2::IDENTITY]
145181
}
@@ -189,6 +225,15 @@ pub struct Instance<T> {
189225
}
190226

191227
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+
192237
pub fn to_graphic_element<U>(self) -> Instance<U>
193238
where
194239
T: Into<U>,

0 commit comments

Comments
 (0)