Skip to main content

dioxus_core/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
3#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
4#![recursion_limit = "256"]
5#![warn(missing_docs)]
6// Coverage runs (`RUSTFLAGS="--cfg coverage_nightly" cargo +nightly fuzz coverage`)
7// opt into nightly's `#[coverage(off)]` attribute so unreachable-by-design
8// regions (typed-builder marker fn bodies that the dispatcher routes around)
9// don't drag the coverage metric down. Stable builds see no effect.
10#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
11
12// Lets the `Props`/`component` derive macros refer to this crate as `dioxus_core::` even when they
13// are expanded *inside* `dioxus-core` itself (e.g. for `ErrorBoundary`/`Fragment`), so those
14// components can use the derive instead of hand-written builders.
15extern crate self as dioxus_core;
16
17// Debug assertions document internal invariants, but the standard
18// `debug_assert*` macros leave unreachable `cfg!(debug_assertions)` branches in
19// coverage builds. These compile away completely under `coverage_nightly` so the
20// fuzzer coverage report tracks reachable runtime behavior.
21macro_rules! dioxus_debug_assert {
22    ($($arg:tt)*) => {{
23        #[cfg(all(debug_assertions, not(coverage_nightly)))]
24        {
25            debug_assert!($($arg)*);
26        }
27    }};
28}
29
30mod any_props;
31mod arena;
32mod diff;
33mod effect;
34mod error_boundary;
35mod events;
36mod fragment;
37mod generational_box;
38mod global_context;
39mod launch;
40mod mount;
41mod mutations;
42mod nodes;
43mod portal;
44mod properties;
45mod reactive_context;
46mod render_driver;
47mod render_error;
48mod root_wrapper;
49mod runtime;
50mod scheduler;
51mod scope_arena;
52mod scope_context;
53mod scopes;
54mod suspense;
55mod tasks;
56pub mod view;
57mod virtual_dom;
58
59mod hotreload_utils;
60
61/// Items exported from this module are used in macros and should not be used directly.
62#[doc(hidden)]
63pub mod internal {
64    pub use crate::hotreload_utils::{
65        DynamicLiteralPool, DynamicValuePool, FmtSegment, FmtedSegments, HotReloadAttributeValue,
66        HotReloadDynamicAttribute, HotReloadDynamicNode, HotReloadLiteral,
67        HotReloadTemplateWithLocation, HotReloadedTemplate, NamedAttribute, TemplateGlobalKey,
68    };
69
70    pub use anyhow::__anyhow;
71
72    pub use dioxus_core_template::{TemplateRawTree, TemplateStorage};
73
74    pub use generational_box;
75}
76
77pub(crate) mod innerlude {
78    pub(crate) use crate::any_props::*;
79    pub use crate::arena::*;
80    pub(crate) use crate::effect::*;
81    pub use crate::error_boundary::*;
82    pub use crate::events::*;
83    pub use crate::fragment::*;
84    pub use crate::generational_box::*;
85    pub use crate::global_context::*;
86    pub use crate::launch::*;
87    pub use crate::mutations::*;
88    pub use crate::nodes::*;
89    pub use crate::portal::*;
90    pub use crate::properties::*;
91    pub use crate::reactive_context::*;
92    pub use crate::render_error::*;
93    pub use crate::runtime::{Runtime, RuntimeGuard};
94    pub(crate) use crate::scheduler::*;
95    pub use crate::scopes::*;
96    pub use crate::suspense::*;
97    pub use crate::tasks::*;
98    pub use crate::virtual_dom::*;
99
100    pub use anyhow::Context as AnyhowContext;
101    pub use anyhow::anyhow;
102    pub use dioxus_core_template::Template;
103    // pub use anyhow::Error as AnyhowError;
104    // pub type Error = CapturedError;
105
106    /// A result type with a default error of [`CapturedError`].
107    pub type Result<T, E = CapturedError> = std::result::Result<T, E>;
108
109    /// An [`Element`] is a possibly-none [`VNode`] created by calling `render` on [`ScopeId`] or [`ScopeState`].
110    ///
111    /// An Errored [`Element`] will propagate the error to the nearest error boundary.
112    pub type Element = std::result::Result<VNode, RenderError>;
113
114    /// A [`Component`] is a function that takes [`Properties`] and returns an [`Element`].
115    pub type Component<P = ()> = fn(P) -> Element;
116}
117
118pub use crate::innerlude::{
119    AnyValue, AnyhowContext, AsyncMarker, AsyncResultMarker, Attribute, AttributeValue, Callback,
120    CapturedError, Component, ComponentBuilder, ComponentBuilderOutput, ComponentBuilderRender,
121    ComponentFunction, ComponentFunctionExt, DynamicNode, DynamicValues, Element, ElementId,
122    ErrorBoundary, ErrorContext, Event, EventHandler, Fragment, HasAttributes, IntoAttributeValue,
123    IntoDynNode, IntoVNode, LaunchConfig, ListenerCallback, MarkerWrapper, MountedVNode,
124    MultiWriter, Mutation, Mutations, NoOpMutations, OptionArgumentsFromMarker,
125    OptionCallbackMarker, OptionStringFromMarker, Portal, PortalProps, Properties, ReactiveContext,
126    RenderError, RenderTargetId, Result, Runtime, RuntimeGuard, ScopeId, ScopeState, SpawnIfAsync,
127    SubscriberList, Subscribers, SuperFrom, SuperInto, SuspendedFuture, SuspenseBoundary,
128    SuspenseBoundaryProps, SuspenseContext, Task, Template, UnitClosure, VComponent, VNode, VText,
129    VirtualDom, WriteMutations, anyhow, consume_context, consume_context_from_scope, current_owner,
130    current_scope_id, generation, has_context, needs_update, needs_update_any, parent_scope,
131    provide_context, provide_create_error_boundary, provide_root_context, queue_effect,
132    remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, spawn_isomorphic,
133    suspend, throw_error, try_consume_context, use_after_render, use_before_render, use_drop,
134    use_hook, use_hook_with_cleanup, with_owner,
135};
136pub use diff::template::{
137    DynamicAnchor, DynamicAttrSlot, DynamicNodeSlot, StaticElement, StaticText, VNodeChild,
138};
139
140/// Equivalent to `Ok::<_, dioxus::CapturedError>(value)`.
141///
142/// This simplifies creation of an `dioxus::Result` in places where type
143/// inference cannot deduce the `E` type of the result &mdash; without needing
144/// to write`Ok::<_, dioxus::CapturedError>(value)`.
145///
146/// One might think that `dioxus::Result::Ok(value)` would work in such cases
147/// but it does not.
148///
149/// ```console
150/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
151///   --> src/main.rs:11:13
152///    |
153/// 11 |     let _ = dioxus::Result::Ok(1);
154///    |         -   ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
155///    |         |
156///    |         consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
157/// ```
158#[allow(non_snake_case)]
159pub fn Ok<T>(value: T) -> Result<T, CapturedError> {
160    Result::Ok(value)
161}
162
163pub use const_format;