Skip to content

Commit

Permalink
Merge pull request #1542 from lann/dhc-handle
Browse files Browse the repository at this point in the history
Make `HostComponentDataHandle::from_any` non-`pub` & non-`unsafe`
  • Loading branch information
lann authored May 26, 2023
2 parents 329e2e8 + cad1928 commit c2c6e81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
11 changes: 3 additions & 8 deletions crates/app/src/host_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,12 @@ impl DynamicHostComponents {
host_component: DHC,
) -> anyhow::Result<HostComponentDataHandle<DHC>> {
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(
Expand Down
28 changes: 18 additions & 10 deletions crates/core/src/host_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ pub struct HostComponentDataHandle<HC: HostComponent> {
}

impl<HC: HostComponent> HostComponentDataHandle<HC> {
/// 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,
Expand All @@ -106,15 +101,18 @@ impl<HC: HostComponent> HostComponentDataHandle<HC> {

impl<HC: HostComponent> Clone for HostComponentDataHandle<HC> {
fn clone(&self) -> Self {
Self {
inner: self.inner,
_phantom: PhantomData,
}
Self::from_any(self.inner)
}
}

impl<HC: HostComponent> Copy for HostComponentDataHandle<HC> {}

impl<HC: HostComponent> From<HostComponentDataHandle<Arc<HC>>> for HostComponentDataHandle<HC> {
fn from(value: HostComponentDataHandle<Arc<HC>>) -> Self {
Self::from_any(value.inner)
}
}

#[doc(hidden)]
pub trait DynSafeHostComponent {
fn build_data_box(&self) -> AnyData;
Expand Down Expand Up @@ -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<HC: HostComponent>(
&mut self,
handle: HostComponentDataHandle<HC>,
Expand All @@ -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())
Expand Down

0 comments on commit c2c6e81

Please sign in to comment.