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,
120    BorrowedAttributeValue, Callback, CapturedError, Component, ComponentBuilder,
121    ComponentBuilderOutput, ComponentBuilderRender, ComponentFunction, ComponentFunctionExt,
122    DynamicNode, DynamicValues, Element, ElementId, ErrorBoundary, ErrorContext, Event,
123    EventHandler, Fragment, HasAttributes, IntoAttributeValue, IntoDynNode, IntoVNode,
124    LaunchConfig, ListenerCallback, MarkerWrapper, MountedVNode, MultiWriter, Mutation, Mutations,
125    NoOpMutations, OptionArgumentsFromMarker, OptionCallbackMarker, OptionStringFromMarker, Portal,
126    PortalProps, Properties, ReactiveContext, RenderError, RenderTargetId, Result, Runtime,
127    RuntimeGuard, ScopeId, ScopeState, SpawnIfAsync, SubscriberList, Subscribers, SuperFrom,
128    SuperInto, SuspendedFuture, SuspenseBoundary, SuspenseBoundaryProps, SuspenseContext, Task,
129    Template, UnitClosure, VComponent, VNode, VText, VirtualDom, WriteMutations, anyhow,
130    consume_context, consume_context_from_scope, current_owner, current_scope_id, generation,
131    has_context, needs_update, needs_update_any, parent_scope, provide_context,
132    provide_create_error_boundary, provide_root_context, queue_effect, remove_future,
133    schedule_update, schedule_update_any, spawn, spawn_forever, spawn_isomorphic, suspend,
134    throw_error, try_consume_context, use_after_render, use_before_render, use_drop, use_hook,
135    use_hook_with_cleanup, with_owner,
136};
137pub use diff::template::{
138    DynamicAnchor, DynamicAttrSlot, DynamicNodeSlot, StaticElement, StaticText, VNodeChild,
139};
140
141/// Equivalent to `Ok::<_, dioxus::CapturedError>(value)`.
142///
143/// This simplifies creation of an `dioxus::Result` in places where type
144/// inference cannot deduce the `E` type of the result &mdash; without needing
145/// to write`Ok::<_, dioxus::CapturedError>(value)`.
146///
147/// One might think that `dioxus::Result::Ok(value)` would work in such cases
148/// but it does not.
149///
150/// ```console
151/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
152///   --> src/main.rs:11:13
153///    |
154/// 11 |     let _ = dioxus::Result::Ok(1);
155///    |         -   ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
156///    |         |
157///    |         consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
158/// ```
159#[allow(non_snake_case)]
160pub fn Ok<T>(value: T) -> Result<T, CapturedError> {
161    Result::Ok(value)
162}
163
164pub use const_format;