Skip to content

Commit 0c9cb22

Browse files
core: downcast::<Option<_>>().take() hack in userspace
1 parent 07ac2f0 commit 0c9cb22

File tree

6 files changed

+501
-35
lines changed

6 files changed

+501
-35
lines changed

crates/binario/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ impl<T: Decode + Type + Send + 'static> ResolverInput for TypedBinarioInput<T> {
5050
T::inline(types, Generics::Definition)
5151
}
5252

53-
fn from_input(input: DynInput) -> Result<Self, ProcedureError> {
54-
Ok(Self(input.value()?, PhantomData))
53+
fn from_input(mut input: DynInput) -> Result<Self, ProcedureError> {
54+
Ok(Self(
55+
input.value::<Option<_>>()?.take().expect("unreachable"),
56+
PhantomData,
57+
))
5558
}
5659
}
5760

crates/procedure/src/dyn_input.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ enum Repr<'a, 'de> {
2121
}
2222

2323
impl<'a, 'de> DynInput<'a, 'de> {
24-
// TODO: Explain invariant on `Option` + enforce it
25-
pub fn new_value<T: Send + 'static>(value: &'a mut Option<T>) -> Self {
24+
// TODO: Discuss using `Option` as a workaround for ownership
25+
pub fn new_value<T: Send + 'static>(value: &'a mut T) -> Self {
2626
Self {
2727
inner: Repr::Value(value),
2828
type_name: type_name::<T>(),
@@ -55,23 +55,18 @@ impl<'a, 'de> DynInput<'a, 'de> {
5555
}
5656

5757
/// TODO
58-
pub fn value<T: 'static>(self) -> Result<T, ProcedureError> {
59-
let Repr::Value(value) = self.inner else {
58+
pub fn value<T: 'static>(&mut self) -> Result<&mut T, ProcedureError> {
59+
let Repr::Value(ref mut value) = self.inner else {
6060
return Err(DowncastError {
6161
from: None,
6262
to: type_name::<T>(),
6363
}
6464
.into());
6565
};
66-
Ok(value
67-
.downcast_mut::<Option<T>>()
68-
.ok_or(DowncastError {
69-
from: Some(self.type_name),
70-
to: type_name::<T>(),
71-
})?
72-
.take()
73-
// This takes method takes `self` and it's not `Clone` so it's not possible to double take the value.
74-
.expect("unreachable"))
66+
Ok(value.downcast_mut::<T>().ok_or(DowncastError {
67+
from: Some(self.type_name),
68+
to: type_name::<T>(),
69+
})?)
7570
}
7671
}
7772

crates/procedure/src/procedure.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ impl<TCtx> Procedure<TCtx> {
5050
.map_err(|err| ProcedureError::Unwind(err).into());
5151
v
5252
}
53-
54-
pub fn exec_with_value<T: Send + 'static>(&self, ctx: TCtx, input: T) -> ProcedureStream {
55-
let mut input = Some(input);
56-
let value = DynInput::new_value(&mut input);
57-
58-
let (Ok(v) | Err(v)) = catch_unwind(AssertUnwindSafe(|| (self.handler)(ctx, value)))
59-
.map_err(|err| ProcedureError::Unwind(err).into());
60-
v
61-
}
6253
}
6354

6455
impl<TCtx> Clone for Procedure<TCtx> {

0 commit comments

Comments
 (0)