diff --git a/editor/src/messages/portfolio/portfolio_message_handler.rs b/editor/src/messages/portfolio/portfolio_message_handler.rs index e341d8fe..70b153e3 100644 --- a/editor/src/messages/portfolio/portfolio_message_handler.rs +++ b/editor/src/messages/portfolio/portfolio_message_handler.rs @@ -685,11 +685,7 @@ impl PortfolioMessageHandler { .0; } // If the input is just a value, return that value - NodeInput::Value { tagged_value, .. } => { - return dyn_any::downcast::(tagged_value.clone().to_value().up_box()) - .map(|v| *v) - .ok_or_else(|| "Incorrectly typed value".to_string()) - } + NodeInput::Value { tagged_value, .. } => return dyn_any::downcast::(tagged_value.clone().to_value().up_box()).map(|v| *v), // If the input is from a node, set the node to be the output (so that is what is evaluated) NodeInput::Node(n) => { inner_network.output = *n; @@ -717,7 +713,7 @@ impl PortfolioMessageHandler { let boxed = unsafe { stack.get().last().unwrap().eval(image.into_owned().into_dyn()) }; - dyn_any::downcast::(boxed).map(|v| *v).ok_or_else(|| "Incorrectly typed output".to_string()) + dyn_any::downcast::(boxed).map(|v| *v) } /// Encodes an image into a format using the image crate diff --git a/libraries/dyn-any/src/lib.rs b/libraries/dyn-any/src/lib.rs index 4266c3b2..16a45335 100644 --- a/libraries/dyn-any/src/lib.rs +++ b/libraries/dyn-any/src/lib.rs @@ -82,22 +82,25 @@ pub fn downcast_ref<'a, V: StaticType>(i: &'a dyn DynAny<'a>) -> Option<&'a V> { } #[cfg(feature = "alloc")] -pub fn downcast<'a, V: StaticType>(i: Box + 'a>) -> Option> { +pub fn downcast<'a, V: StaticType>(i: Box + 'a>) -> Result, String> { let type_id = DynAny::type_id(i.as_ref()); if type_id == core::any::TypeId::of::<::Static>() { // SAFETY: caller guarantees that T is the correct type let ptr = Box::into_raw(i) as *mut dyn DynAny<'a> as *mut V; - Some(unsafe { Box::from_raw(ptr) }) + Ok(unsafe { Box::from_raw(ptr) }) } else { - #[cfg(feature = "log-bad-types")] - { - log::error!("Tried to downcast a {} to a {}", DynAny::type_name(i.as_ref()), core::any::type_name::()); - } - if type_id == core::any::TypeId::of::<&dyn DynAny<'static>>() { panic!("downcast error: type_id == core::any::TypeId::of::>()"); } - None + #[cfg(feature = "log-bad-types")] + { + Err(format!("Incorrect type, expected {} but found {}", core::any::type_name::(), DynAny::type_name(i.as_ref()))) + } + + #[cfg(not(feature = "log-bad-types"))] + { + Err(format!("Incorrect type, expected {}", core::any::type_name::())) + } } } diff --git a/node-graph/graph-craft/src/imaginate_input.rs b/node-graph/graph-craft/src/imaginate_input.rs index ea736867..31d7226f 100644 --- a/node-graph/graph-craft/src/imaginate_input.rs +++ b/node-graph/graph-craft/src/imaginate_input.rs @@ -18,7 +18,7 @@ pub enum ImaginateStatus { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ImaginateBaseImage { pub mime: String, - #[serde(rename = "imageData")] + #[cfg_attr(feature = "serde", serde(rename = "imageData"))] pub image_data: Vec, pub size: DVec2, } diff --git a/node-graph/gstd/src/any.rs b/node-graph/gstd/src/any.rs index 01b0a4ff..ebcecc4b 100644 --- a/node-graph/gstd/src/any.rs +++ b/node-graph/gstd/src/any.rs @@ -2,10 +2,6 @@ use dyn_any::{DynAny, StaticType, StaticTypeSized}; pub use graphene_core::{generic, ops /*, structural*/, Node, RefNode}; use std::marker::PhantomData; -fn fmt_error() -> String { - format!("DynAnyNode: input is not of correct type, expected {}", std::any::type_name::()) -} - pub struct DynAnyNode(pub N, pub PhantomData<(I, O, ORef)>); /*impl<'n, I: StaticType, N: RefNode<'n, &'n I, Output = O> + 'n, O: 'n + StaticType> Node<&'n dyn DynAny<'n>> for DynAnyNode<'n, N, I> { type Output = Box + 'n>; @@ -35,7 +31,7 @@ where { type Output = Any<'n>; fn eval(self, input: Any<'n>) -> Self::Output { - let input: Box = dyn_any::downcast(input).unwrap_or_else(|| panic!("{}", fmt_error::())); + let input: Box = dyn_any::downcast(input).expect("DynAnyNode Input"); Box::new(self.0.eval(*input)) } } @@ -45,7 +41,7 @@ where { type Output = Any<'n>; fn eval(self, input: Any<'n>) -> Self::Output { - let input: Box = dyn_any::downcast(input).unwrap_or_else(|| panic!("{}", fmt_error::())); + let input: Box = dyn_any::downcast(input).expect("DynAnyNode Input"); Box::new((&self.0).eval_ref(*input)) } } @@ -132,7 +128,7 @@ where type Output = O; fn eval(self, input: Any<'n>) -> Self::Output { let output = self.0.eval(input); - *dyn_any::downcast(output).unwrap_or_else(|| panic!("DowncastNode: {}", fmt_error::())) + *dyn_any::downcast(output).expect("DowncastNode Output") } } impl<'n, N, I: StaticType> DowncastNode @@ -162,7 +158,7 @@ where fn eval(self, input: I) -> Self::Output { let input = Box::new(input) as Box; let output = self.0.eval(input); - *dyn_any::downcast(output).unwrap_or_else(|| panic!("DowncastBothNode Output: {}", fmt_error::())) + *dyn_any::downcast(output).expect("DowncastBothNode Output") } } impl<'n, N, I: StaticType, O: StaticType> DowncastBothNode