use crate::HostType;
use std::marker::PhantomData;
pub struct Value<Type, Context = MissingValueContext> {
context: Context,
type_: PhantomData<fn() -> Type>,
}
#[doc(hidden)]
pub struct MissingValueContext;
#[doc(hidden)]
pub struct ProviderValueContext<'call, Host>
where
Host: HostType,
{
value: Host::Value<'call>,
}
impl<'call, Type, Host> Value<Type, ProviderValueContext<'call, Host>>
where
Host: HostType,
{
#[doc(hidden)]
pub fn from_host(value: Host::Value<'call>) -> Self {
Self {
context: ProviderValueContext { value },
type_: PhantomData,
}
}
#[doc(hidden)]
pub fn into_host(self) -> Host::Value<'call> {
self.context.value
}
pub(crate) fn host(&self) -> Host::Value<'call>
where
Host::Value<'call>: Clone,
{
self.context.value.clone()
}
}
#[cfg(test)]
mod tests {
use super::{ProviderValueContext, Value};
use crate::host::{HostTypeParameter, HostValue, HostValueFamily, HostValueToken};
#[test]
fn provider_value_preserves_the_exact_call_scoped_host_handle() {
type Parameter = HostTypeParameter<0>;
let host = HostValue::<Parameter>::new(HostValueToken {
family: HostValueFamily::String,
index: 4,
});
let value = Value::<Parameter, ProviderValueContext<'_, Parameter>>::from_host(host);
assert_eq!(value.host().token, host.token);
assert_eq!(value.into_host().token, host.token);
}
}