routing/bedrock/
weak_instance_token_ext.rsuse crate::component_instance::{
ComponentInstanceInterface, ExtendedInstanceInterface, WeakComponentInstanceInterface,
WeakExtendedInstanceInterface,
};
use crate::error::ComponentInstanceError;
use moniker::ExtendedMoniker;
use sandbox::WeakInstanceToken;
use std::sync::Arc;
pub trait WeakInstanceTokenExt<C: ComponentInstanceInterface + 'static> {
fn to_instance(self) -> WeakExtendedInstanceInterface<C>;
fn as_ref(&self) -> &WeakExtendedInstanceInterface<C>;
fn upgrade(&self) -> Result<ExtendedInstanceInterface<C>, ComponentInstanceError>;
fn moniker(&self) -> ExtendedMoniker;
}
impl<C: ComponentInstanceInterface + 'static> WeakInstanceTokenExt<C> for WeakInstanceToken {
fn to_instance(self) -> WeakExtendedInstanceInterface<C> {
self.as_ref().clone()
}
fn as_ref(&self) -> &WeakExtendedInstanceInterface<C> {
match self.inner.as_any().downcast_ref::<WeakExtendedInstanceInterface<C>>() {
Some(instance) => &instance,
None => panic!(),
}
}
fn upgrade(&self) -> Result<ExtendedInstanceInterface<C>, ComponentInstanceError> {
self.as_ref().upgrade()
}
fn moniker(&self) -> ExtendedMoniker {
WeakInstanceTokenExt::<C>::as_ref(self).extended_moniker()
}
}
pub fn test_invalid_instance_token<C: ComponentInstanceInterface + 'static>() -> WeakInstanceToken {
WeakComponentInstanceInterface::<C>::invalid().into()
}
impl<C: ComponentInstanceInterface + 'static> From<WeakExtendedInstanceInterface<C>>
for WeakInstanceToken
{
fn from(instance: WeakExtendedInstanceInterface<C>) -> Self {
Self { inner: Arc::new(instance) }
}
}
impl<C: ComponentInstanceInterface + 'static> From<WeakComponentInstanceInterface<C>>
for WeakInstanceToken
{
fn from(instance: WeakComponentInstanceInterface<C>) -> Self {
Self::from(WeakExtendedInstanceInterface::<C>::Component(instance))
}
}