From cad192882616f162fc0dbdd46ca32262713e6e23 Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Thu, 25 May 2023 14:36:47 -0400 Subject: [PATCH] Make `HostComponentDataHandle::from_any` non-pub, non-unsafe Not only was this not really `unsafe`, it doesn't really need to be `pub`. Also added notes about panicing to docs. Signed-off-by: Lann Martin --- crates/app/src/host_component.rs | 11 +++-------- crates/core/src/host_component.rs | 28 ++++++++++++++++++---------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/app/src/host_component.rs b/crates/app/src/host_component.rs index d33bfceff..e422dd52f 100644 --- a/crates/app/src/host_component.rs +++ b/crates/app/src/host_component.rs @@ -76,17 +76,12 @@ impl DynamicHostComponents { host_component: DHC, ) -> anyhow::Result> { let host_component = Arc::new(host_component); - let handle = engine_builder - .add_host_component(host_component.clone())? - .into(); + let handle = engine_builder.add_host_component(host_component.clone())?; self.host_components.push(DynamicHostComponentWithHandle { host_component, - handle, + handle: handle.into(), }); - unsafe { - // Safe because `EngineBuilder::add_host_component` must have correct type. - Ok(HostComponentDataHandle::from_any(handle)) - } + Ok(handle.into()) } pub fn update_data( diff --git a/crates/core/src/host_component.rs b/crates/core/src/host_component.rs index 065902f17..f476961c7 100644 --- a/crates/core/src/host_component.rs +++ b/crates/core/src/host_component.rs @@ -91,12 +91,7 @@ pub struct HostComponentDataHandle { } impl HostComponentDataHandle { - /// Return a typed handle to some [`HostComponent::Data`]. - /// - /// # Safety - /// Caller must ensure that the handle is associated with the - /// given type or use of the handle will panic. - pub unsafe fn from_any(handle: AnyHostComponentDataHandle) -> Self { + fn from_any(handle: AnyHostComponentDataHandle) -> Self { Self { inner: handle, _phantom: PhantomData, @@ -106,15 +101,18 @@ impl HostComponentDataHandle { impl Clone for HostComponentDataHandle { fn clone(&self) -> Self { - Self { - inner: self.inner, - _phantom: PhantomData, - } + Self::from_any(self.inner) } } impl Copy for HostComponentDataHandle {} +impl From>> for HostComponentDataHandle { + fn from(value: HostComponentDataHandle>) -> Self { + Self::from_any(value.inner) + } +} + #[doc(hidden)] pub trait DynSafeHostComponent { fn build_data_box(&self) -> AnyData; @@ -201,6 +199,11 @@ impl HostComponentsData { /// Retrieves a mutable reference to [`HostComponent::Data`] for the given `handle`. /// /// If unset, the data will be initialized with [`HostComponent::build_data`]. + /// + /// # Panics + /// + /// If the given handle was not obtained from the same [`HostComponentsBuilder`] that + /// was the source of this [`HostComponentsData`], this function may panic. pub fn get_or_insert( &mut self, handle: HostComponentDataHandle, @@ -212,6 +215,11 @@ impl HostComponentsData { /// Retrieves a mutable reference to [`HostComponent::Data`] for the given `handle`. /// /// If unset, the data will be initialized with [`HostComponent::build_data`]. + /// + /// # Panics + /// + /// If the given handle was not obtained from the same [`HostComponentsBuilder`] that + /// was the source of this [`HostComponentsData`], this function may panic. pub fn get_or_insert_any(&mut self, handle: AnyHostComponentDataHandle) -> &mut AnyData { let idx = handle.0; self.data[idx].get_or_insert_with(|| self.host_components[idx].build_data_box())