use CapturedError as dioxus::Ok() so resources still function as expected (#4851)

This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-10-27 15:06:21 -07:00
1 parent 9f0bacc85c
commit 281f68faf5
14 files changed
+200 -54

No files matched your search

+29 -4
View File
@@ -77,8 +77,11 @@ pub(crate) mod innerlude {
pub use anyhow::anyhow;
pub use anyhow::Context as AnyhowContext;
pub use anyhow::Error;
pub use anyhow::Result;
// pub use anyhow::Error as AnyhowError;
// pub type Error = CapturedError;
/// A result type with a default error of [`CapturedError`].
pub type Result<T, E = CapturedError> = std::result::Result<T, E>;
/// An [`Element`] is a possibly-none [`VNode`] created by calling `render` on [`ScopeId`] or [`ScopeState`].
///
@@ -97,7 +100,7 @@ pub use crate::innerlude::{
suspend, throw_error, try_consume_context, use_after_render, use_before_render, use_drop,
use_hook, use_hook_with_cleanup, with_owner, AnyValue, AnyhowContext, Attribute,
AttributeValue, Callback, CapturedError, Component, ComponentFunction, DynamicNode, Element,
ElementId, Error, ErrorBoundary, ErrorContext, Event, EventHandler, Fragment, HasAttributes,
ElementId, ErrorBoundary, ErrorContext, Event, EventHandler, Fragment, HasAttributes,
IntoAttributeValue, IntoDynNode, LaunchConfig, ListenerCallback, MarkerWrapper, Mutation,
Mutations, NoOpMutations, OptionStringFromMarker, Properties, ReactiveContext, RenderError,
Result, Runtime, RuntimeGuard, ScopeId, ScopeState, SpawnIfAsync, SubscriberList, Subscribers,
@@ -106,5 +109,27 @@ pub use crate::innerlude::{
VNodeInner, VPlaceholder, VText, VirtualDom, WriteMutations,
};
pub use anyhow::Ok;
/// Equivalent to `Ok::<_, dioxus::CapturedError>(value)`.
///
/// This simplifies creation of an `dioxus::Result` in places where type
/// inference cannot deduce the `E` type of the result &mdash; without needing
/// to write`Ok::<_, dioxus::CapturedError>(value)`.
///
/// One might think that `dioxus::Result::Ok(value)` would work in such cases
/// but it does not.
///
/// ```console
/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
/// --> src/main.rs:11:13
/// |
/// 11 | let _ = dioxus::Result::Ok(1);
/// | - ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
/// | |
/// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
/// ```
#[allow(non_snake_case)]
pub fn Ok<T>(value: T) -> Result<T, CapturedError> {
Result::Ok(value)
}
pub use const_format;
+35 -4
View File
@@ -51,7 +51,7 @@ impl From<CapturedError> for RenderError {
}
}
impl<E: Into<Error>> From<E> for RenderError {
impl<E: Into<anyhow::Error>> From<E> for RenderError {
fn from(e: E) -> Self {
let anyhow_err = e.into();
@@ -69,13 +69,44 @@ impl<E: Into<Error>> From<E> for RenderError {
/// An `anyhow::Error` wrapped in an `Arc` so it can be cheaply cloned and passed around.
#[derive(Debug, Clone)]
pub struct CapturedError(pub Arc<Error>);
pub struct CapturedError(pub Arc<anyhow::Error>);
impl CapturedError {
/// Create a `CapturedError` from anything that implements `Display`.
pub fn from_display(t: impl Display) -> Self {
Self(Arc::new(anyhow::anyhow!(t.to_string())))
}
/// Create a `CapturedError` from anything that implements `std::error::Error`.
pub fn new<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
anyhow::Error::new(error).into()
}
/// Create a `CapturedError` from anything that implements `Display` and `Debug`.
pub fn msg<M>(t: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
anyhow::Error::msg(t).into()
}
/// Create a `CapturedError` from a boxed `std::error::Error`.
pub fn from_boxed(boxed_error: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
anyhow::Error::from_boxed(boxed_error).into()
}
/// Returns the strong count of the underlying error.
pub fn _strong_count(&self) -> usize {
std::sync::Arc::strong_count(&self.0)
}
/// Try to unwrap the underlying error if this is the only reference to it.
pub fn into_inner(self) -> Option<anyhow::Error> {
Arc::try_unwrap(self.0).ok()
}
}
#[cfg(feature = "serialize")]
@@ -100,14 +131,14 @@ impl<'de> serde::Deserialize<'de> for CapturedError {
}
impl std::ops::Deref for CapturedError {
type Target = Error;
type Target = anyhow::Error;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<E: Into<Error>> From<E> for CapturedError {
impl<E: Into<anyhow::Error>> From<E> for CapturedError {
fn from(e: E) -> Self {
Self(Arc::new(e.into()))
}