Skip to content

Commit

Permalink
Merge pull request #1538 from lann/dhc-handle
Browse files Browse the repository at this point in the history
spin-app: Make add_dynamic_host_component return handle
  • Loading branch information
lann authored May 23, 2023
2 parents f3a251e + c74951e commit 2bdf0ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
12 changes: 9 additions & 3 deletions crates/app/src/host_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{any::Any, sync::Arc};

use anyhow::Context;
use spin_core::{AnyHostComponentDataHandle, EngineBuilder, HostComponent, HostComponentsData};
use spin_core::{
AnyHostComponentDataHandle, EngineBuilder, HostComponent, HostComponentDataHandle,
HostComponentsData,
};

use crate::{App, AppComponent};

Expand Down Expand Up @@ -71,7 +74,7 @@ impl DynamicHostComponents {
&mut self,
engine_builder: &mut EngineBuilder<T>,
host_component: DHC,
) -> anyhow::Result<()> {
) -> anyhow::Result<HostComponentDataHandle<DHC>> {
let host_component = Arc::new(host_component);
let handle = engine_builder
.add_host_component(host_component.clone())?
Expand All @@ -80,7 +83,10 @@ impl DynamicHostComponents {
host_component,
handle,
});
Ok(())
unsafe {
// Safe because `EngineBuilder::add_host_component` must have correct type.
Ok(HostComponentDataHandle::from_any(handle))
}
}

pub fn update_data(
Expand Down
37 changes: 17 additions & 20 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod values;

use ouroboros::self_referencing;
use serde::Deserialize;
use spin_core::{wasmtime, Engine, EngineBuilder, StoreBuilder};
use spin_core::{wasmtime, Engine, EngineBuilder, HostComponentDataHandle, StoreBuilder};

use host_component::DynamicHostComponents;
use locked::{ContentPath, LockedApp, LockedComponent, LockedComponentSource, LockedTrigger};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl AppLoader {
&mut self,
engine_builder: &mut EngineBuilder<T>,
host_component: DHC,
) -> anyhow::Result<()> {
) -> anyhow::Result<HostComponentDataHandle<DHC>> {
self.dynamic_host_components
.add_dynamic_host_component(engine_builder, host_component)
}
Expand Down Expand Up @@ -361,35 +361,32 @@ impl<'a, L: MaybeLoader> AppTrigger<'a, L> {
&self.locked.trigger_type
}

/// Deserializes this trigger's configuration into a typed value.
pub fn typed_config<Config: Deserialize<'a>>(&self) -> Result<Config> {
Ok(Config::deserialize(&self.locked.trigger_config)?)
}

/// Returns a reference to the [`AppComponent`] configured for this trigger.
///
/// This is a convenience wrapper that looks up the component based on the
/// 'component' metadata value which is conventionally a component ID.
pub fn component(&self) -> Result<AppComponent<'a, L>> {
let component_id = self.locked.trigger_config.get("component").ok_or_else(|| {
Error::MetadataError(format!(
"trigger {:?} missing 'component' config field",
self.locked.id
))
let id = &self.locked.id;
let common_config: CommonTriggerConfig = self.typed_config()?;
let component_id = common_config.component.ok_or_else(|| {
Error::MetadataError(format!("trigger {id:?} missing 'component' config field"))
})?;
let component_id = component_id.as_str().ok_or_else(|| {
self.app.get_component(&component_id).ok_or_else(|| {
Error::MetadataError(format!(
"trigger {:?} 'component' field has unexpected value {:?}",
self.locked.id, component_id
))
})?;
self.app.get_component(component_id).ok_or_else(|| {
Error::MetadataError(format!(
"missing component {:?} configured for trigger {:?}",
component_id, self.locked.id
"missing component {component_id:?} configured for trigger {id:?}"
))
})
}
}

/// Deserializes this trigger's configuration into a typed value.
pub fn typed_config<Config: Deserialize<'a>>(&self) -> Result<Config> {
Ok(Config::deserialize(&self.locked.trigger_config)?)
}
#[derive(Deserialize)]
struct CommonTriggerConfig {
component: Option<String>,
}

/// Type alias for a [`Result`]s with [`Error`].
Expand Down
14 changes: 14 additions & 0 deletions crates/core/src/host_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ pub struct HostComponentDataHandle<HC: HostComponent> {
_phantom: PhantomData<fn() -> HC::Data>,
}

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 {
Self {
inner: handle,
_phantom: PhantomData,
}
}
}

impl<HC: HostComponent> Clone for HostComponentDataHandle<HC> {
fn clone(&self) -> Self {
Self {
Expand Down

0 comments on commit 2bdf0ba

Please sign in to comment.