Browse Source

Remove allocations from `HostDefined::get_many_mut` (#3606)

pull/3609/head
José Julián Espina 9 months ago committed by GitHub
parent
commit
2ebcbab80a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      core/engine/src/host_defined.rs

12
core/engine/src/host_defined.rs

@ -87,11 +87,7 @@ impl HostDefined {
T: NativeTuple<SIZE>, T: NativeTuple<SIZE>,
{ {
let ids = T::as_type_ids(); let ids = T::as_type_ids();
let refs: [&TypeId; SIZE] = ids let refs: [&TypeId; SIZE] = std::array::from_fn(|i| &ids[i]);
.iter()
.collect::<Vec<_>>()
.try_into()
.expect("tuple should be of size `SIZE`");
self.types.get_many_mut(refs).and_then(T::mut_ref_from_anys) self.types.get_many_mut(refs).and_then(T::mut_ref_from_anys)
} }
@ -110,7 +106,7 @@ impl HostDefined {
pub trait NativeTuple<const SIZE: usize> { pub trait NativeTuple<const SIZE: usize> {
type NativeTupleMutRef<'a>; type NativeTupleMutRef<'a>;
fn as_type_ids() -> Vec<TypeId>; fn as_type_ids() -> [TypeId; SIZE];
fn mut_ref_from_anys( fn mut_ref_from_anys(
anys: [&'_ mut Box<dyn NativeObject>; SIZE], anys: [&'_ mut Box<dyn NativeObject>; SIZE],
@ -122,8 +118,8 @@ macro_rules! impl_native_tuple {
impl<$($name: NativeObject,)*> NativeTuple<$size> for ($($name,)*) { impl<$($name: NativeObject,)*> NativeTuple<$size> for ($($name,)*) {
type NativeTupleMutRef<'a> = ($(&'a mut $name,)*); type NativeTupleMutRef<'a> = ($(&'a mut $name,)*);
fn as_type_ids() -> Vec<TypeId> { fn as_type_ids() -> [TypeId; $size] {
vec![$(TypeId::of::<$name>(),)*] [$(TypeId::of::<$name>(),)*]
} }
fn mut_ref_from_anys( fn mut_ref_from_anys(

Loading…
Cancel
Save