Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HostComponentDataHandle::from_any non-pub & non-unsafe #1542

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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