Handle::from_untyped

This commit is contained in:
Carter Anderson 2020-03-28 14:51:24 -07:00
parent ed9eb88835
commit 2d0bff97a8
2 changed files with 21 additions and 6 deletions

View file

@ -22,6 +22,14 @@ impl<T> Handle<T> {
marker: PhantomData,
}
}
pub fn from_untyped(untyped_handle: HandleUntyped) -> Option<Handle<T>> where T: 'static {
if TypeId::of::<T>() == untyped_handle.type_id {
Some(Handle::new(untyped_handle.id))
} else {
None
}
}
}
impl<T> Hash for Handle<T> {
@ -88,11 +96,7 @@ where
T: 'static,
{
fn from(handle: HandleUntyped) -> Self {
if TypeId::of::<T>() != handle.type_id {
panic!("attempted to convert untyped handle to incorrect typed handle");
}
Handle::new(handle.id)
Handle::from_untyped(handle).expect("attempted to convert untyped handle to incorrect typed handle")
}
}

View file

@ -1,4 +1,7 @@
use crate::{asset::HandleUntyped, render::render_resource::RenderResourceAssignments};
use crate::{
asset::{Handle, HandleUntyped},
render::render_resource::RenderResourceAssignments,
};
use legion::prelude::Entity;
use std::collections::{HashMap, HashSet};
@ -23,4 +26,12 @@ impl Batch {
self.current_instanced_entity_index += 1;
}
}
pub fn get_handle<T>(&self) -> Option<Handle<T>> where T: 'static {
self.handles
.iter()
.map(|h| Handle::from_untyped(*h))
.find(|h| h.is_some())
.map(|h| h.unwrap())
}
}