"));
+ }
props_docs.push(parse_quote! {
#[doc = #arg_doc]
diff --git a/packages/core-macro/src/props/mod.rs b/packages/core-macro/src/props/mod.rs
index 605983afc..d7a580705 100644
--- a/packages/core-macro/src/props/mod.rs
+++ b/packages/core-macro/src/props/mod.rs
@@ -243,10 +243,6 @@ mod field_info {
}
.into()
}
-
- pub fn type_from_inside_option(&self, check_option_name: bool) -> Option<&syn::Type> {
- type_from_inside_option(self.ty, check_option_name)
- }
}
#[derive(Debug, Default, Clone)]
@@ -551,18 +547,16 @@ mod struct_info {
let generics_with_empty = modify_types_generics_hack(&ty_generics, |args| {
args.insert(0, syn::GenericArgument::Type(empties_tuple.clone().into()));
});
- let phantom_generics = self.generics.params.iter().map(|param| match param {
+ let phantom_generics = self.generics.params.iter().filter_map(|param| match param {
syn::GenericParam::Lifetime(lifetime) => {
let lifetime = &lifetime.lifetime;
- quote!(::core::marker::PhantomData<lifetime ()>)
+ Some(quote!(::core::marker::PhantomData<lifetime ()>))
}
syn::GenericParam::Type(ty) => {
let ty = &ty.ident;
- quote!(::core::marker::PhantomData<#ty>)
- }
- syn::GenericParam::Const(_cnst) => {
- quote!()
+ Some(quote!(::core::marker::PhantomData<#ty>))
}
+ syn::GenericParam::Const(_cnst) => None,
});
let builder_method_doc = match self.builder_attr.builder_method_doc {
Some(ref doc) => quote!(#doc),
@@ -633,7 +627,7 @@ Finally, call `.build()` to create the instance of `{name}`.
Ok(quote! {
impl #impl_generics #name #ty_generics #where_clause {
#[doc = #builder_method_doc]
- #[allow(dead_code)]
+ #[allow(dead_code, clippy::type_complexity)]
#vis fn builder() -> #builder_name #generics_with_empty {
#builder_name {
fields: #empties_tuple,
@@ -785,31 +779,16 @@ Finally, call `.build()` to create the instance of `{name}`.
None => quote!(),
};
- // NOTE: both auto_into and strip_option affect `arg_type` and `arg_expr`, but the order of
- // nesting is different so we have to do this little dance.
- let arg_type = if field.builder_attr.strip_option {
- field.type_from_inside_option(false).ok_or_else(|| {
- Error::new_spanned(
- field_type,
- "can't `strip_option` - field is not `Option<...>`",
+ let arg_type = field_type;
+ let (arg_type, arg_expr) =
+ if field.builder_attr.auto_into || field.builder_attr.strip_option {
+ (
+ quote!(impl ::core::convert::Into<#arg_type>),
+ quote!(#field_name.into()),
)
- })?
- } else {
- field_type
- };
- let (arg_type, arg_expr) = if field.builder_attr.auto_into {
- (
- quote!(impl ::core::convert::Into<#arg_type>),
- quote!(#field_name.into()),
- )
- } else {
- (quote!(#arg_type), quote!(#field_name))
- };
- let arg_expr = if field.builder_attr.strip_option {
- quote!(Some(#arg_expr))
- } else {
- arg_expr
- };
+ } else {
+ (quote!(#arg_type), quote!(#field_name))
+ };
let repeated_fields_error_type_name = syn::Ident::new(
&format!(
@@ -825,6 +804,7 @@ Finally, call `.build()` to create the instance of `{name}`.
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl #impl_generics #builder_name < #( #ty_generics ),* > #where_clause {
#doc
+ #[allow(clippy::type_complexity)]
pub fn #field_name (self, #field_name: #arg_type) -> #builder_name < #( #target_generics ),* > {
let #field_name = (#arg_expr,);
let ( #(#descructuring,)* ) = self.fields;
@@ -843,6 +823,7 @@ Finally, call `.build()` to create the instance of `{name}`.
#[deprecated(
note = #repeated_fields_error_message
)]
+ #[allow(clippy::type_complexity)]
pub fn #field_name (self, _: #repeated_fields_error_type_name) -> #builder_name < #( #target_generics ),* > {
self
}
diff --git a/packages/core/src/arena.rs b/packages/core/src/arena.rs
index b7f768755..785d4b174 100644
--- a/packages/core/src/arena.rs
+++ b/packages/core/src/arena.rs
@@ -164,17 +164,11 @@ impl VirtualDom {
});
// Now that all the references are gone, we can safely drop our own references in our listeners.
- let mut listeners = scope.attributes_to_drop.borrow_mut();
+ let mut listeners = scope.attributes_to_drop_before_render.borrow_mut();
listeners.drain(..).for_each(|listener| {
let listener = unsafe { &*listener };
- match &listener.value {
- AttributeValue::Listener(l) => {
- _ = l.take();
- }
- AttributeValue::Any(a) => {
- _ = a.take();
- }
- _ => (),
+ if let AttributeValue::Listener(l) = &listener.value {
+ _ = l.take();
}
});
}
diff --git a/packages/core/src/bump_frame.rs b/packages/core/src/bump_frame.rs
index 0fe7b3867..6927bd8bc 100644
--- a/packages/core/src/bump_frame.rs
+++ b/packages/core/src/bump_frame.rs
@@ -1,10 +1,16 @@
use crate::nodes::RenderReturn;
+use crate::{Attribute, AttributeValue, VComponent};
use bumpalo::Bump;
+use std::cell::RefCell;
use std::cell::{Cell, UnsafeCell};
pub(crate) struct BumpFrame {
pub bump: UnsafeCell,
pub node: Cell<*const RenderReturn<'static>>,
+
+ // The bump allocator will not call the destructor of the objects it allocated. Attributes and props need to have there destructor called, so we keep a list of them to drop before the bump allocator is reset.
+ pub(crate) attributes_to_drop_before_reset: RefCell>>,
+ pub(crate) props_to_drop_before_reset: RefCell>>,
}
impl BumpFrame {
@@ -13,6 +19,8 @@ impl BumpFrame {
Self {
bump: UnsafeCell::new(bump),
node: Cell::new(std::ptr::null()),
+ attributes_to_drop_before_reset: Default::default(),
+ props_to_drop_before_reset: Default::default(),
}
}
@@ -31,8 +39,38 @@ impl BumpFrame {
unsafe { &*self.bump.get() }
}
- #[allow(clippy::mut_from_ref)]
- pub(crate) unsafe fn bump_mut(&self) -> &mut Bump {
- unsafe { &mut *self.bump.get() }
+ pub(crate) fn add_attribute_to_drop(&self, attribute: *const Attribute<'static>) {
+ self.attributes_to_drop_before_reset
+ .borrow_mut()
+ .push(attribute);
+ }
+
+ /// Reset the bump allocator and drop all the attributes and props that were allocated in it.
+ ///
+ /// # Safety
+ /// The caller must insure that no reference to anything allocated in the bump allocator is available after this function is called.
+ pub(crate) unsafe fn reset(&self) {
+ let mut attributes = self.attributes_to_drop_before_reset.borrow_mut();
+ attributes.drain(..).for_each(|attribute| {
+ let attribute = unsafe { &*attribute };
+ if let AttributeValue::Any(l) = &attribute.value {
+ _ = l.take();
+ }
+ });
+ let mut props = self.props_to_drop_before_reset.borrow_mut();
+ props.drain(..).for_each(|prop| {
+ let prop = unsafe { &*prop };
+ _ = prop.props.borrow_mut().take();
+ });
+ unsafe {
+ let bump = &mut *self.bump.get();
+ bump.reset();
+ }
+ }
+}
+
+impl Drop for BumpFrame {
+ fn drop(&mut self) {
+ unsafe { self.reset() }
}
}
diff --git a/packages/core/src/diff.rs b/packages/core/src/diff.rs
index 74017db0f..2de862218 100644
--- a/packages/core/src/diff.rs
+++ b/packages/core/src/diff.rs
@@ -560,7 +560,7 @@ impl<'b> VirtualDom {
// If none of the old keys are reused by the new children, then we remove all the remaining old children and
// create the new children afresh.
if shared_keys.is_empty() {
- if old.get(0).is_some() {
+ if old.first().is_some() {
self.remove_nodes(&old[1..]);
self.replace(&old[0], new);
} else {
diff --git a/packages/core/src/events.rs b/packages/core/src/events.rs
index 3b8edb05c..784a8c865 100644
--- a/packages/core/src/events.rs
+++ b/packages/core/src/events.rs
@@ -107,8 +107,6 @@ impl std::fmt::Debug for Event {
}
}
-#[doc(hidden)]
-
/// The callback type generated by the `rsx!` macro when an `on` field is specified for components.
///
/// This makes it possible to pass `move |evt| {}` style closures into components as property fields.
diff --git a/packages/core/src/lazynodes.rs b/packages/core/src/lazynodes.rs
index 811dbb734..3189086b4 100644
--- a/packages/core/src/lazynodes.rs
+++ b/packages/core/src/lazynodes.rs
@@ -23,8 +23,37 @@ use crate::{innerlude::VNode, ScopeState};
///
///
/// ```rust, ignore
-/// LazyNodes::new(|f| f.element("div", [], [], [] None))
+/// LazyNodes::new(|f| {
+/// static TEMPLATE: dioxus::core::Template = dioxus::core::Template {
+/// name: "main.rs:5:5:20", // Source location of the template for hot reloading
+/// roots: &[
+/// dioxus::core::TemplateNode::Element {
+/// tag: dioxus_elements::div::TAG_NAME,
+/// namespace: dioxus_elements::div::NAME_SPACE,
+/// attrs: &[],
+/// children: &[],
+/// },
+/// ],
+/// node_paths: &[],
+/// attr_paths: &[],
+/// };
+/// dioxus::core::VNode {
+/// parent: None,
+/// key: None,
+/// template: std::cell::Cell::new(TEMPLATE),
+/// root_ids: dioxus::core::exports::bumpalo::collections::Vec::with_capacity_in(
+/// 1usize,
+/// f.bump(),
+/// )
+/// .into(),
+/// dynamic_nodes: f.bump().alloc([]),
+/// dynamic_attrs: f.bump().alloc([]),
+/// })
+/// }
/// ```
+///
+/// Find more information about how to construct [`VNode`] at
+
pub struct LazyNodes<'a, 'b> {
#[cfg(not(miri))]
inner: SmallBox VNode<'a> + 'b, S16>,
@@ -61,7 +90,7 @@ impl<'a, 'b> LazyNodes<'a, 'b> {
/// Call the closure with the given factory to produce real [`VNode`].
///
/// ```rust, ignore
- /// let f = LazyNodes::new(move |f| f.element("div", [], [], [] None));
+ /// let f = LazyNodes::new(/* Closure for creating VNodes */);
///
/// let node = f.call(cac);
/// ```
diff --git a/packages/core/src/mutations.rs b/packages/core/src/mutations.rs
index 27976c157..f33590e2e 100644
--- a/packages/core/src/mutations.rs
+++ b/packages/core/src/mutations.rs
@@ -91,7 +91,7 @@ pub enum Mutation<'a> {
id: ElementId,
},
- /// Create an placeholder int he DOM that we will use later.
+ /// Create a placeholder in the DOM that we will use later.
///
/// Dioxus currently requires the use of placeholders to maintain a re-entrance point for things like list diffing
CreatePlaceholder {
diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs
index c83b1059a..78868d29f 100644
--- a/packages/core/src/nodes.rs
+++ b/packages/core/src/nodes.rs
@@ -707,7 +707,7 @@ impl<'a, 'b> IntoDynNode<'b> for &'a str {
impl IntoDynNode<'_> for String {
fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
DynamicNode::Text(VText {
- value: cx.bump().alloc(self),
+ value: cx.bump().alloc_str(&self),
id: Default::default(),
})
}
@@ -791,6 +791,12 @@ impl<'a> IntoAttributeValue<'a> for &'a str {
}
}
+impl<'a> IntoAttributeValue<'a> for String {
+ fn into_value(self, cx: &'a Bump) -> AttributeValue<'a> {
+ AttributeValue::Text(cx.alloc_str(&self))
+ }
+}
+
impl<'a> IntoAttributeValue<'a> for f64 {
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
AttributeValue::Float(self)
diff --git a/packages/core/src/scope_arena.rs b/packages/core/src/scope_arena.rs
index 1ed5b816c..397f5328e 100644
--- a/packages/core/src/scope_arena.rs
+++ b/packages/core/src/scope_arena.rs
@@ -35,7 +35,7 @@ impl VirtualDom {
hook_idx: Default::default(),
borrowed_props: Default::default(),
- attributes_to_drop: Default::default(),
+ attributes_to_drop_before_render: Default::default(),
}));
let context =
@@ -54,7 +54,7 @@ impl VirtualDom {
let new_nodes = unsafe {
let scope = &self.scopes[scope_id.0];
- scope.previous_frame().bump_mut().reset();
+ scope.previous_frame().reset();
scope.context().suspended.set(false);
diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs
index 996529a0d..f0bbd776c 100644
--- a/packages/core/src/scopes.rs
+++ b/packages/core/src/scopes.rs
@@ -94,7 +94,7 @@ pub struct ScopeState {
pub(crate) hook_idx: Cell,
pub(crate) borrowed_props: RefCell>>,
- pub(crate) attributes_to_drop: RefCell>>,
+ pub(crate) attributes_to_drop_before_render: RefCell>>,
pub(crate) props: Option>>,
}
@@ -348,25 +348,36 @@ impl<'src> ScopeState {
pub fn render(&'src self, rsx: LazyNodes<'src, '_>) -> Element<'src> {
let element = rsx.call(self);
- let mut listeners = self.attributes_to_drop.borrow_mut();
+ let mut listeners = self.attributes_to_drop_before_render.borrow_mut();
for attr in element.dynamic_attrs {
match attr.value {
- AttributeValue::Any(_) | AttributeValue::Listener(_) => {
+ // We need to drop listeners before the next render because they may borrow data from the borrowed props which will be dropped
+ AttributeValue::Listener(_) => {
let unbounded = unsafe { std::mem::transmute(attr as *const Attribute) };
listeners.push(unbounded);
}
+ // We need to drop any values manually to make sure that their drop implementation is called before the next render
+ AttributeValue::Any(_) => {
+ let unbounded = unsafe { std::mem::transmute(attr as *const Attribute) };
+ self.previous_frame().add_attribute_to_drop(unbounded);
+ }
_ => (),
}
}
let mut props = self.borrowed_props.borrow_mut();
+ let mut drop_props = self
+ .previous_frame()
+ .props_to_drop_before_reset
+ .borrow_mut();
for node in element.dynamic_nodes {
if let DynamicNode::Component(comp) = node {
+ let unbounded = unsafe { std::mem::transmute(comp as *const VComponent) };
if !comp.static_props {
- let unbounded = unsafe { std::mem::transmute(comp as *const VComponent) };
props.push(unbounded);
}
+ drop_props.push(unbounded);
}
}
diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml
index dcb994120..f0ea9e189 100644
--- a/packages/desktop/Cargo.toml
+++ b/packages/desktop/Cargo.toml
@@ -59,6 +59,7 @@ devtools = ["wry/devtools"]
tray = ["wry/tray"]
dox = ["wry/dox"]
hot-reload = ["dioxus-hot-reload"]
+gnu = []
[package.metadata.docs.rs]
default-features = false
diff --git a/packages/desktop/build.rs b/packages/desktop/build.rs
new file mode 100644
index 000000000..2061732e8
--- /dev/null
+++ b/packages/desktop/build.rs
@@ -0,0 +1,9 @@
+fn main() {
+ // WARN about wry support on windows gnu targets. GNU windows targets don't work well in wry currently
+ if std::env::var("CARGO_CFG_WINDOWS").is_ok()
+ && std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu"
+ && !cfg!(feature = "gnu")
+ {
+ println!("cargo:warning=GNU windows targets have some limitations within Wry. Using the MSVC windows toolchain is recommended. If you would like to use continue using GNU, you can read https://github.com/wravery/webview2-rs#cross-compilation and disable this warning by adding the gnu feature to dioxus-desktop in your Cargo.toml")
+ }
+}
diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs
index badbe3276..63a8914f3 100644
--- a/packages/desktop/src/lib.rs
+++ b/packages/desktop/src/lib.rs
@@ -54,7 +54,7 @@ use wry::{application::window::WindowId, webview::WebContext};
///
/// This function will start a multithreaded Tokio runtime as well the WebView event loop.
///
-/// ```rust, ignore
+/// ```rust, no_run
/// use dioxus::prelude::*;
///
/// fn main() {
@@ -77,11 +77,12 @@ pub fn launch(root: Component) {
///
/// You can configure the WebView window with a configuration closure
///
-/// ```rust, ignore
+/// ```rust, no_run
/// use dioxus::prelude::*;
+/// use dioxus_desktop::*;
///
/// fn main() {
-/// dioxus_desktop::launch_cfg(app, |c| c.with_window(|w| w.with_title("My App")));
+/// dioxus_desktop::launch_cfg(app, Config::default().with_window(WindowBuilder::new().with_title("My App")));
/// }
///
/// fn app(cx: Scope) -> Element {
@@ -100,8 +101,9 @@ pub fn launch_cfg(root: Component, config_builder: Config) {
///
/// You can configure the WebView window with a configuration closure
///
-/// ```rust, ignore
+/// ```rust, no_run
/// use dioxus::prelude::*;
+/// use dioxus_desktop::Config;
///
/// fn main() {
/// dioxus_desktop::launch_with_props(app, AppProps { name: "asd" }, Config::default());
@@ -161,6 +163,7 @@ pub fn launch_with_props(root: Component
, props: P, cfg: Config)
// iOS panics if we create a window before the event loop is started
let props = Rc::new(Cell::new(Some(props)));
let cfg = Rc::new(Cell::new(Some(cfg)));
+ let mut is_visible_before_start = true;
event_loop.run(move |window_event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
@@ -210,6 +213,8 @@ pub fn launch_with_props(root: Component
, props: P, cfg: Config)
// Create a dom
let dom = VirtualDom::new_with_props(root, props);
+ is_visible_before_start = cfg.window.window.visible;
+
let handler = create_new_window(
cfg,
event_loop,
@@ -323,6 +328,10 @@ pub fn launch_with_props(root: Component
, props: P, cfg: Config)
EventData::Ipc(msg) if msg.method() == "initialize" => {
let view = webviews.get_mut(&event.1).unwrap();
send_edits(view.dom.rebuild(), &view.desktop_context.webview);
+ view.desktop_context
+ .webview
+ .window()
+ .set_visible(is_visible_before_start);
}
EventData::Ipc(msg) if msg.method() == "browser_open" => {
diff --git a/packages/desktop/src/webview.rs b/packages/desktop/src/webview.rs
index 469aa8068..6e6d26b17 100644
--- a/packages/desktop/src/webview.rs
+++ b/packages/desktop/src/webview.rs
@@ -13,7 +13,7 @@ pub fn build(
proxy: EventLoopProxy,
) -> (WebView, WebContext) {
let builder = cfg.window.clone();
- let window = builder.build(event_loop).unwrap();
+ let window = builder.with_visible(false).build(event_loop).unwrap();
let file_handler = cfg.file_drop_handler.take();
let custom_head = cfg.custom_head.clone();
let index_file = cfg.custom_index.clone();
diff --git a/packages/dioxus-tui/examples/colorpicker.rs b/packages/dioxus-tui/examples/colorpicker.rs
index 00f8ef7e0..01cc38cda 100644
--- a/packages/dioxus-tui/examples/colorpicker.rs
+++ b/packages/dioxus-tui/examples/colorpicker.rs
@@ -15,21 +15,21 @@ fn app(cx: Scope) -> Element {
let mapping: DioxusElementToNodeId = cx.consume_context().unwrap();
// disable templates so that every node has an id and can be queried
cx.render(rsx! {
- div{
+ div {
width: "100%",
background_color: "hsl({hue}, 70%, {brightness}%)",
onmousemove: move |evt| {
if let RenderReturn::Ready(node) = cx.root_node() {
- if let Some(id) = node.root_ids.borrow().get(0).cloned() {
+ if let Some(id) = node.root_ids.borrow().first().cloned() {
let node = tui_query.get(mapping.get_node_id(id).unwrap());
- let Size{width, height} = node.size().unwrap();
+ let Size { width, height } = node.size().unwrap();
let pos = evt.inner().element_coordinates();
- hue.set((pos.x as f32/width as f32)*255.0);
- brightness.set((pos.y as f32/height as f32)*100.0);
+ hue.set((pos.x as f32 / width as f32) * 255.0);
+ brightness.set((pos.y as f32 / height as f32) * 100.0);
}
}
},
- "hsl({hue}, 70%, {brightness}%)",
+ "hsl({hue}, 70%, {brightness}%)"
}
})
}
diff --git a/packages/extension/src/lib.rs b/packages/extension/src/lib.rs
index abcdaf598..fb9cca8a9 100644
--- a/packages/extension/src/lib.rs
+++ b/packages/extension/src/lib.rs
@@ -1,17 +1,39 @@
//! This file exports functions into the vscode extension
-use dioxus_autofmt::FormattedBlock;
+use dioxus_autofmt::{FormattedBlock, IndentOptions, IndentType};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
-pub fn format_rsx(raw: String) -> String {
- let block = dioxus_autofmt::fmt_block(&raw, 0);
+pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String {
+ let block = dioxus_autofmt::fmt_block(
+ &raw,
+ 0,
+ IndentOptions::new(
+ if use_tabs {
+ IndentType::Tabs
+ } else {
+ IndentType::Spaces
+ },
+ indent_size,
+ ),
+ );
block.unwrap()
}
#[wasm_bindgen]
-pub fn format_selection(raw: String) -> String {
- let block = dioxus_autofmt::fmt_block(&raw, 0);
+pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> String {
+ let block = dioxus_autofmt::fmt_block(
+ &raw,
+ 0,
+ IndentOptions::new(
+ if use_tabs {
+ IndentType::Tabs
+ } else {
+ IndentType::Spaces
+ },
+ indent_size,
+ ),
+ );
block.unwrap()
}
@@ -35,8 +57,18 @@ impl FormatBlockInstance {
}
#[wasm_bindgen]
-pub fn format_file(contents: String) -> FormatBlockInstance {
- let _edits = dioxus_autofmt::fmt_file(&contents);
+pub fn format_file(contents: String, use_tabs: bool, indent_size: usize) -> FormatBlockInstance {
+ let _edits = dioxus_autofmt::fmt_file(
+ &contents,
+ IndentOptions::new(
+ if use_tabs {
+ IndentType::Tabs
+ } else {
+ IndentType::Spaces
+ },
+ indent_size,
+ ),
+ );
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());
FormatBlockInstance { new: out, _edits }
}
diff --git a/packages/extension/src/main.ts b/packages/extension/src/main.ts
index fc12457ad..76b7261f1 100644
--- a/packages/extension/src/main.ts
+++ b/packages/extension/src/main.ts
@@ -90,7 +90,13 @@ function fmtDocument(document: vscode.TextDocument) {
if (!editor) return; // Need an editor to apply text edits.
const contents = editor.document.getText();
- const formatted = dioxus.format_file(contents);
+ let tabSize: number;
+ if (typeof editor.options.tabSize === 'number') {
+ tabSize = editor.options.tabSize;
+ } else {
+ tabSize = 4;
+ }
+ const formatted = dioxus.format_file(contents, !editor.options.insertSpaces, tabSize);
// Replace the entire text document
// Yes, this is a bit heavy handed, but the dioxus side doesn't know the line/col scheme that vscode is using
diff --git a/packages/fermi/src/hooks/atom_root.rs b/packages/fermi/src/hooks/atom_root.rs
index 24fd70093..468f5f980 100644
--- a/packages/fermi/src/hooks/atom_root.rs
+++ b/packages/fermi/src/hooks/atom_root.rs
@@ -7,6 +7,6 @@ use dioxus_core::ScopeState;
pub fn use_atom_root(cx: &ScopeState) -> &Rc {
cx.use_hook(|| match cx.consume_context::>() {
Some(root) => root,
- None => panic!("No atom root found in context. Did you forget place an AtomRoot component at the top of your app?"),
+ None => panic!("No atom root found in context. Did you forget to call use_init_atom_root at the top of your app?"),
})
}
diff --git a/packages/fermi/src/hooks/state.rs b/packages/fermi/src/hooks/state.rs
index d4ebc529c..7d473588a 100644
--- a/packages/fermi/src/hooks/state.rs
+++ b/packages/fermi/src/hooks/state.rs
@@ -86,7 +86,9 @@ impl AtomState {
/// ```
#[must_use]
pub fn current(&self) -> Rc {
- self.value.as_ref().unwrap().clone()
+ let atoms = self.root.atoms.borrow();
+ let slot = atoms.get(&self.id).unwrap();
+ slot.value.clone().downcast().unwrap()
}
/// Get the `setter` function directly without the `AtomState` wrapper.
diff --git a/packages/fermi/src/lib.rs b/packages/fermi/src/lib.rs
index 52bf4cd09..dc5ccf010 100644
--- a/packages/fermi/src/lib.rs
+++ b/packages/fermi/src/lib.rs
@@ -22,8 +22,6 @@ mod atoms {
pub use atom::*;
pub use atomfamily::*;
pub use atomref::*;
- pub use selector::*;
- pub use selectorfamily::*;
}
pub mod hooks {
diff --git a/packages/fullstack/Cargo.toml b/packages/fullstack/Cargo.toml
index 8ab213e78..8de27a9b1 100644
--- a/packages/fullstack/Cargo.toml
+++ b/packages/fullstack/Cargo.toml
@@ -11,7 +11,7 @@ keywords = ["ui", "gui", "react", "ssr", "fullstack"]
[dependencies]
# server functions
-server_fn = { version = "0.4.6", default-features = false }
+server_fn = { version = "0.5.2", default-features = false }
dioxus_server_macro = { workspace = true }
# warp
diff --git a/packages/fullstack/examples/axum-hello-world/src/main.rs b/packages/fullstack/examples/axum-hello-world/src/main.rs
index 8f1d3c8c6..64e5b44a0 100644
--- a/packages/fullstack/examples/axum-hello-world/src/main.rs
+++ b/packages/fullstack/examples/axum-hello-world/src/main.rs
@@ -24,6 +24,7 @@ fn app(cx: Scope) -> Element {
let mut count = use_state(cx, || 0);
let text = use_state(cx, || "...".to_string());
+ let eval = use_eval(cx);
cx.render(rsx! {
div {
diff --git a/packages/fullstack/src/adapters/axum_adapter.rs b/packages/fullstack/src/adapters/axum_adapter.rs
index 95d9ee6cd..41a7c92be 100644
--- a/packages/fullstack/src/adapters/axum_adapter.rs
+++ b/packages/fullstack/src/adapters/axum_adapter.rs
@@ -369,15 +369,65 @@ fn apply_request_parts_to_response(
}
}
-/// SSR renderer handler for Axum
-pub async fn render_handler(
- State((cfg, ssr_state)): State<(ServeConfig
, SSRState)>,
+/// SSR renderer handler for Axum with added context injection.
+///
+/// # Example
+/// ```rust,no_run
+/// #![allow(non_snake_case)]
+/// use std::sync::{Arc, Mutex};
+///
+/// use axum::routing::get;
+/// use dioxus::prelude::*;
+/// use dioxus_fullstack::{axum_adapter::render_handler_with_context, prelude::*};
+///
+/// fn app(cx: Scope) -> Element {
+/// render! {
+/// "hello!"
+/// }
+/// }
+///
+/// #[tokio::main]
+/// async fn main() {
+/// let cfg = ServeConfigBuilder::new(app, ())
+/// .assets_path("dist")
+/// .build();
+/// let ssr_state = SSRState::new(&cfg);
+///
+/// // This could be any state you want to be accessible from your server
+/// // functions using `[DioxusServerContext::get]`.
+/// let state = Arc::new(Mutex::new("state".to_string()));
+///
+/// let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
+/// axum::Server::bind(&addr)
+/// .serve(
+/// axum::Router::new()
+/// // Register server functions, etc.
+/// // Note you probably want to use `register_server_fns_with_handler`
+/// // to inject the context into server functions running outside
+/// // of an SSR render context.
+/// .fallback(get(render_handler_with_context).with_state((
+/// move |ctx| ctx.insert(state.clone()).unwrap(),
+/// cfg,
+/// ssr_state,
+/// )))
+/// .into_make_service(),
+/// )
+/// .await
+/// .unwrap();
+/// }
+/// ```
+pub async fn render_handler_with_context<
+ P: Clone + serde::Serialize + Send + Sync + 'static,
+ F: FnMut(&mut DioxusServerContext),
+>(
+ State((mut inject_context, cfg, ssr_state)): State<(F, ServeConfig
, SSRState)>,
request: Request
,
) -> impl IntoResponse {
let (parts, _) = request.into_parts();
let url = parts.uri.path_and_query().unwrap().to_string();
let parts: Arc> = Arc::new(RwLock::new(parts.into()));
- let server_context = DioxusServerContext::new(parts.clone());
+ let mut server_context = DioxusServerContext::new(parts.clone());
+ inject_context(&mut server_context);
match ssr_state.render(url, &cfg, &server_context).await {
Ok(rendered) => {
@@ -395,6 +445,14 @@ pub async fn render_handler
}
}
+/// SSR renderer handler for Axum
+pub async fn render_handler(
+ State((cfg, ssr_state)): State<(ServeConfig
, SSRState)>,
+ request: Request
,
+) -> impl IntoResponse {
+ render_handler_with_context(State((|_: &mut _| (), cfg, ssr_state)), request).await
+}
+
fn report_err(e: E) -> Response {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
diff --git a/packages/fullstack/src/adapters/mod.rs b/packages/fullstack/src/adapters/mod.rs
index 98d190919..d368b9c73 100644
--- a/packages/fullstack/src/adapters/mod.rs
+++ b/packages/fullstack/src/adapters/mod.rs
@@ -89,26 +89,26 @@ impl Service for ServerFnHandler {
let parts = Arc::new(RwLock::new(parts));
// Because the future returned by `server_fn_handler` is `Send`, and the future returned by this function must be send, we need to spawn a new runtime
- let (resp_tx, resp_rx) = tokio::sync::oneshot::channel();
let pool = get_local_pool();
- pool.spawn_pinned({
- let function = function.clone();
- let mut server_context = server_context.clone();
- server_context.parts = parts;
- move || async move {
- let data = match function.encoding() {
- Encoding::Url | Encoding::Cbor => &body,
- Encoding::GetJSON | Encoding::GetCBOR => &query,
- };
- let server_function_future = function.call((), data);
- let server_function_future =
- ProvideServerContext::new(server_function_future, server_context.clone());
- let resp = server_function_future.await;
-
- resp_tx.send(resp).unwrap();
- }
- });
- let result = resp_rx.await.unwrap();
+ let result = pool
+ .spawn_pinned({
+ let function = function.clone();
+ let mut server_context = server_context.clone();
+ server_context.parts = parts;
+ move || async move {
+ let data = match function.encoding() {
+ Encoding::Url | Encoding::Cbor => &body,
+ Encoding::GetJSON | Encoding::GetCBOR => &query,
+ };
+ let server_function_future = function.call((), data);
+ let server_function_future = ProvideServerContext::new(
+ server_function_future,
+ server_context.clone(),
+ );
+ server_function_future.await
+ }
+ })
+ .await?;
let mut res = http::Response::builder();
// Set the headers from the server context
diff --git a/packages/fullstack/src/layer.rs b/packages/fullstack/src/layer.rs
index 10e67b3f5..60f76107b 100644
--- a/packages/fullstack/src/layer.rs
+++ b/packages/fullstack/src/layer.rs
@@ -3,7 +3,9 @@ use tracing_futures::Instrument;
use http::{Request, Response};
+/// A layer that wraps a service. This can be used to add additional information to the request, or response on top of some other service
pub trait Layer: Send + Sync + 'static {
+ /// Wrap a boxed service with this layer
fn layer(&self, inner: BoxedService) -> BoxedService;
}
@@ -17,7 +19,9 @@ where
}
}
+/// A service is a function that takes a request and returns an async response
pub trait Service {
+ /// Run the service and produce a future that resolves to a response
fn run(
&mut self,
req: http::Request,
@@ -55,6 +59,7 @@ where
}
}
+/// A boxed service is a type-erased service that can be used without knowing the underlying type
pub struct BoxedService(pub Box);
impl tower::Service> for BoxedService {
diff --git a/packages/fullstack/src/lib.rs b/packages/fullstack/src/lib.rs
index 1fe7504f1..7ef582da1 100644
--- a/packages/fullstack/src/lib.rs
+++ b/packages/fullstack/src/lib.rs
@@ -40,6 +40,8 @@ pub mod prelude {
#[cfg(not(feature = "ssr"))]
pub use crate::html_storage::deserialize::get_root_props_from_document;
pub use crate::launch::LaunchBuilder;
+ #[cfg(feature = "ssr")]
+ pub use crate::layer::{Layer, Service};
#[cfg(all(feature = "ssr", feature = "router"))]
pub use crate::render::pre_cache_static_routes_with_props;
#[cfg(feature = "ssr")]
diff --git a/packages/fullstack/src/render.rs b/packages/fullstack/src/render.rs
index cda41f82c..886a5bd99 100644
--- a/packages/fullstack/src/render.rs
+++ b/packages/fullstack/src/render.rs
@@ -45,6 +45,8 @@ impl SsrRendererPool {
.expect("couldn't spawn runtime")
.block_on(async move {
let mut vdom = VirtualDom::new_with_props(component, props);
+ // Make sure the evaluator is initialized
+ dioxus_ssr::eval::init_eval(vdom.base_scope());
let mut to = WriteBuffer { buffer: Vec::new() };
// before polling the future, we need to set the context
let prev_context =
diff --git a/packages/fullstack/src/router.rs b/packages/fullstack/src/router.rs
index b556aa000..b8d93f0e0 100644
--- a/packages/fullstack/src/router.rs
+++ b/packages/fullstack/src/router.rs
@@ -53,7 +53,7 @@ fn default_external_navigation_handler() -> fn(Scope) -> Element {
dioxus_router::prelude::FailureExternalNavigation
}
-/// The configeration for the router
+/// The configuration for the router
#[derive(Props, serde::Serialize, serde::Deserialize)]
pub struct FullstackRouterConfig
where
diff --git a/packages/fullstack/src/server_fn.rs b/packages/fullstack/src/server_fn.rs
index 539a0d70a..fd0639372 100644
--- a/packages/fullstack/src/server_fn.rs
+++ b/packages/fullstack/src/server_fn.rs
@@ -125,14 +125,6 @@ impl server_fn::ServerFunctionRegistry<()> for DioxusServerFnRegistry {
}
}
- fn register(
- url: &'static str,
- server_function: ServerFunction,
- encoding: server_fn::Encoding,
- ) -> Result<(), Self::Error> {
- Self::register_explicit("", url, server_function, encoding)
- }
-
/// Returns the server function registered at the given URL, or `None` if no function is registered at that URL.
fn get(url: &str) -> Option> {
REGISTERED_SERVER_FUNCTIONS
diff --git a/packages/generational-box/Cargo.toml b/packages/generational-box/Cargo.toml
index 5567e9ccc..1bc298e25 100644
--- a/packages/generational-box/Cargo.toml
+++ b/packages/generational-box/Cargo.toml
@@ -1,9 +1,12 @@
[package]
name = "generational-box"
authors = ["Evan Almloff"]
-version = "0.0.0"
+version = "0.4.3"
edition = "2018"
-
+description = "A box backed by a generational runtime"
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/DioxusLabs/dioxus/"
+keywords = ["generational", "box", "memory", "allocator"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
@@ -17,6 +20,8 @@ criterion = "0.3"
[features]
default = ["check_generation"]
check_generation = []
+debug_borrows = []
+debug_ownership = []
[[bench]]
name = "lock"
diff --git a/packages/generational-box/README.md b/packages/generational-box/README.md
index 7d3088eda..95aab8402 100644
--- a/packages/generational-box/README.md
+++ b/packages/generational-box/README.md
@@ -11,6 +11,8 @@ Three main types manage state in Generational Box:
Example:
```rust
+use generational_box::Store;
+
// Create a store for this thread
let store = Store::default();
diff --git a/packages/generational-box/src/lib.rs b/packages/generational-box/src/lib.rs
index 692af76b0..293bf6c5b 100644
--- a/packages/generational-box/src/lib.rs
+++ b/packages/generational-box/src/lib.rs
@@ -4,12 +4,16 @@
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
+use std::any::Any;
+use std::error::Error;
+use std::fmt::Display;
+use std::sync::atomic::AtomicU32;
use std::{
cell::{Ref, RefCell, RefMut},
fmt::Debug,
marker::PhantomData,
ops::{Deref, DerefMut},
- sync::{atomic::AtomicU32, Arc, OnceLock},
+ sync::{Arc, OnceLock},
};
/// # Example
@@ -52,7 +56,10 @@ fn leaking_is_ok() {
// don't drop the owner
std::mem::forget(owner);
}
- assert_eq!(key.try_read().as_deref(), Some(&"hello world".to_string()));
+ assert_eq!(
+ key.try_read().as_deref().unwrap(),
+ &"hello world".to_string()
+ );
}
#[test]
@@ -66,7 +73,7 @@ fn drops() {
key = owner.insert(data);
// drop the owner
}
- assert!(key.try_read().is_none());
+ assert!(key.try_read().is_err());
}
#[test]
@@ -123,7 +130,7 @@ fn fuzz() {
println!("{:?}", path);
for key in valid_keys.iter() {
let value = key.read();
- println!("{:?}", value);
+ println!("{:?}", &*value);
assert!(value.starts_with("hello world"));
}
#[cfg(any(debug_assertions, feature = "check_generation"))]
@@ -164,10 +171,12 @@ impl Debug for GenerationalBoxId {
}
/// The core Copy state type. The generational box will be dropped when the [Owner] is dropped.
-pub struct GenerationalBox {
+pub struct GenerationalBox {
raw: MemoryLocation,
#[cfg(any(debug_assertions, feature = "check_generation"))]
generation: u32,
+ #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+ created_at: &'static std::panic::Location<'static>,
_marker: PhantomData,
}
@@ -176,11 +185,11 @@ impl Debug for GenerationalBox {
#[cfg(any(debug_assertions, feature = "check_generation"))]
f.write_fmt(format_args!(
"{:?}@{:?}",
- self.raw.data.data_ptr(),
+ self.raw.0.data.data_ptr(),
self.generation
))?;
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
- f.write_fmt(format_args!("{:?}", self.raw.data.as_ptr()))?;
+ f.write_fmt(format_args!("{:?}", self.raw.0.data.as_ptr()))?;
Ok(())
}
}
@@ -191,6 +200,7 @@ impl> GenerationalBox {
#[cfg(any(debug_assertions, feature = "check_generation"))]
{
self.raw
+ .0
.generation
.load(std::sync::atomic::Ordering::Relaxed)
== self.generation
@@ -204,28 +214,54 @@ impl> GenerationalBox {
/// Get the id of the generational box.
pub fn id(&self) -> GenerationalBoxId {
GenerationalBoxId {
- data_ptr: self.raw.data.data_ptr(),
+ data_ptr: self.raw.0.data.data_ptr(),
#[cfg(any(debug_assertions, feature = "check_generation"))]
generation: self.generation,
}
}
/// Try to read the value. Returns None if the value is no longer valid.
- pub fn try_read(&self) -> Option {
- self.validate().then(|| self.raw.data.try_read()).flatten()
+ #[track_caller]
+ pub fn try_read(&self) -> Result {
+ if !self.validate() {
+ return Err(BorrowError::Dropped(ValueDroppedError {
+ #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+ created_at: self.created_at,
+ }));
+ }
+ self.raw.0.data.try_read().ok_or_else(|| {
+ BorrowError::Dropped(ValueDroppedError {
+ #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+ created_at: self.created_at,
+ })
+ })
}
/// Read the value. Panics if the value is no longer valid.
+ #[track_caller]
pub fn read(&self) -> S::Ref {
self.try_read().unwrap()
}
/// Try to write the value. Returns None if the value is no longer valid.
- pub fn try_write(&self) -> Option where {
- self.validate().then(|| self.raw.data.try_write()).flatten()
+ #[track_caller]
+ pub fn try_write(&self) -> Result {
+ if !self.validate() {
+ return Err(BorrowMutError::Dropped(ValueDroppedError {
+ #[cfg(any(debug_assertions, feature = "debug_borrows"))]
+ created_at: self.created_at,
+ }));
+ }
+ self.raw.0.data.try_write().ok_or_else(|| {
+ BorrowMutError::Dropped(ValueDroppedError {
+ #[cfg(any(debug_assertions, feature = "debug_ownership"))]
+ created_at: self.created_at,
+ })
+ })
}
/// Write the value. Panics if the value is no longer valid.
+ #[track_caller]
pub fn write(&self) -> S::Mut {
self.try_write().unwrap()
}
@@ -233,7 +269,7 @@ impl> GenerationalBox {
/// Set the value. Panics if the value is no longer valid.
pub fn set(&self, value: T) {
self.validate().then(|| {
- self.raw.data.set(value);
+ self.raw.0.data.set(value);
});
}
@@ -241,7 +277,7 @@ impl> GenerationalBox {
pub fn ptr_eq(&self, other: &Self) -> bool {
#[cfg(any(debug_assertions, feature = "check_generation"))]
{
- self.raw.data.data_ptr() == other.raw.data.data_ptr()
+ self.raw.0.data.data_ptr() == other.raw.0.data.data_ptr()
&& self.generation == other.generation
}
#[cfg(not(any(debug_assertions, feature = "check_generation")))]
@@ -251,7 +287,7 @@ impl> GenerationalBox {
}
}
-impl Copy for GenerationalBox {}
+impl Copy for GenerationalBox {}
impl Clone for GenerationalBox {
fn clone(&self) -> Self {
@@ -260,12 +296,11 @@ impl Clone for GenerationalBox {
}
/// A unsync storage. This is the default storage type.
-#[derive(Clone, Copy)]
-pub struct UnsyncStorage(&'static RefCell