nodes.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. use crate::{
  2. any_props::AnyProps, arena::ElementId, Element, Event, LazyNodes, ScopeId, ScopeState,
  3. };
  4. use bumpalo::boxed::Box as BumpBox;
  5. use bumpalo::Bump;
  6. use std::{
  7. any::{Any, TypeId},
  8. cell::{Cell, RefCell, UnsafeCell},
  9. fmt::Arguments,
  10. future::Future,
  11. };
  12. pub type TemplateId = &'static str;
  13. /// The actual state of the component's most recent computation
  14. ///
  15. /// Because Dioxus accepts components in the form of `async fn(Scope) -> Result<VNode>`, we need to support both
  16. /// sync and async versions.
  17. ///
  18. /// Dioxus will do its best to immediately resolve any async components into a regular Element, but as an implementor
  19. /// you might need to handle the case where there's no node immediately ready.
  20. pub enum RenderReturn<'a> {
  21. /// A currently-available element
  22. Sync(Element<'a>),
  23. /// An ongoing future that will resolve to a [`Element`]
  24. Async(BumpBox<'a, dyn Future<Output = Element<'a>> + 'a>),
  25. }
  26. /// A reference to a template along with any context needed to hydrate it
  27. ///
  28. /// The dynamic parts of the template are stored separately from the static parts. This allows faster diffing by skipping
  29. /// static parts of the template.
  30. #[derive(Debug, Clone)]
  31. pub struct VNode<'a> {
  32. /// The key given to the root of this template.
  33. ///
  34. /// In fragments, this is the key of the first child. In other cases, it is the key of the root.
  35. pub key: Option<&'a str>,
  36. /// When rendered, this template will be linked to its parent manually
  37. pub parent: Option<ElementId>,
  38. /// The static nodes and static descriptor of the template
  39. pub template: Cell<Template<'static>>,
  40. /// The IDs for the roots of this template - to be used when moving the template around and removing it from
  41. /// the actual Dom
  42. pub root_ids: BoxedCellSlice,
  43. /// The dynamic parts of the template
  44. pub dynamic_nodes: &'a [DynamicNode<'a>],
  45. /// The dynamic parts of the template
  46. pub dynamic_attrs: &'a [Attribute<'a>],
  47. }
  48. // Saftey: There is no way to get references to the internal data of this struct so no refrences will be invalidated by mutating the data with a immutable reference (The same principle behind Cell)
  49. #[derive(Debug, Default)]
  50. pub struct BoxedCellSlice(UnsafeCell<Option<Box<[ElementId]>>>);
  51. impl Clone for BoxedCellSlice {
  52. fn clone(&self) -> Self {
  53. Self(UnsafeCell::new(unsafe { (*self.0.get()).clone() }))
  54. }
  55. }
  56. impl BoxedCellSlice {
  57. pub fn last(&self) -> Option<ElementId> {
  58. unsafe {
  59. (*self.0.get())
  60. .as_ref()
  61. .and_then(|inner| inner.as_ref().last().copied())
  62. }
  63. }
  64. pub fn get(&self, idx: usize) -> Option<ElementId> {
  65. unsafe {
  66. (*self.0.get())
  67. .as_ref()
  68. .and_then(|inner| inner.as_ref().get(idx).copied())
  69. }
  70. }
  71. pub unsafe fn get_unchecked(&self, idx: usize) -> Option<ElementId> {
  72. (*self.0.get())
  73. .as_ref()
  74. .and_then(|inner| inner.as_ref().get(idx).copied())
  75. }
  76. pub fn set(&self, idx: usize, new: ElementId) {
  77. unsafe {
  78. if let Some(inner) = &mut *self.0.get() {
  79. inner[idx] = new;
  80. }
  81. }
  82. }
  83. pub fn intialize(&self, contents: Box<[ElementId]>) {
  84. unsafe {
  85. *self.0.get() = Some(contents);
  86. }
  87. }
  88. pub fn transfer(&self, other: &Self) {
  89. unsafe {
  90. *self.0.get() = (*other.0.get()).clone();
  91. }
  92. }
  93. pub fn take_from(&self, other: &Self) {
  94. unsafe {
  95. *self.0.get() = (*other.0.get()).take();
  96. }
  97. }
  98. pub fn len(&self) -> usize {
  99. unsafe {
  100. (*self.0.get())
  101. .as_ref()
  102. .map(|inner| inner.len())
  103. .unwrap_or(0)
  104. }
  105. }
  106. }
  107. impl<'a> IntoIterator for &'a BoxedCellSlice {
  108. type Item = ElementId;
  109. type IntoIter = BoxedCellSliceIter<'a>;
  110. fn into_iter(self) -> Self::IntoIter {
  111. BoxedCellSliceIter {
  112. index: 0,
  113. borrow: self,
  114. }
  115. }
  116. }
  117. pub struct BoxedCellSliceIter<'a> {
  118. index: usize,
  119. borrow: &'a BoxedCellSlice,
  120. }
  121. impl Iterator for BoxedCellSliceIter<'_> {
  122. type Item = ElementId;
  123. fn next(&mut self) -> Option<Self::Item> {
  124. let result = self.borrow.get(self.index);
  125. if result.is_some() {
  126. self.index += 1;
  127. }
  128. result
  129. }
  130. }
  131. impl<'a> VNode<'a> {
  132. /// Create a template with no nodes that will be skipped over during diffing
  133. pub fn empty() -> Element<'a> {
  134. Some(VNode {
  135. key: None,
  136. parent: None,
  137. root_ids: BoxedCellSlice::default(),
  138. dynamic_nodes: &[],
  139. dynamic_attrs: &[],
  140. template: Cell::new(Template {
  141. name: "dioxus-empty",
  142. roots: &[],
  143. node_paths: &[],
  144. attr_paths: &[],
  145. }),
  146. })
  147. }
  148. /// Load a dynamic root at the given index
  149. ///
  150. /// Returns [`None`] if the root is actually a static node (Element/Text)
  151. pub fn dynamic_root(&self, idx: usize) -> Option<&'a DynamicNode<'a>> {
  152. match &self.template.get().roots[idx] {
  153. TemplateNode::Element { .. } | TemplateNode::Text { text: _ } => None,
  154. TemplateNode::Dynamic { id } | TemplateNode::DynamicText { id } => {
  155. Some(&self.dynamic_nodes[*id])
  156. }
  157. }
  158. }
  159. pub(crate) fn clear_listeners(&self) {
  160. for attr in self.dynamic_attrs {
  161. if let AttributeValue::Listener(l) = &attr.value {
  162. l.borrow_mut().take();
  163. }
  164. }
  165. }
  166. }
  167. /// A static layout of a UI tree that describes a set of dynamic and static nodes.
  168. ///
  169. /// This is the core innovation in Dioxus. Most UIs are made of static nodes, yet participate in diffing like any
  170. /// dynamic node. This struct can be created at compile time. It promises that its name is unique, allow Dioxus to use
  171. /// its static description of the UI to skip immediately to the dynamic nodes during diffing.
  172. ///
  173. /// For this to work properly, the [`Template::name`] *must* be unique across your entire project. This can be done via variety of
  174. /// ways, with the suggested approach being the unique code location (file, line, col, etc).
  175. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  176. #[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
  177. pub struct Template<'a> {
  178. /// The name of the template. This must be unique across your entire program for template diffing to work properly
  179. ///
  180. /// If two templates have the same name, it's likely that Dioxus will panic when diffing.
  181. #[cfg_attr(
  182. feature = "serialize",
  183. serde(deserialize_with = "deserialize_string_leaky")
  184. )]
  185. pub name: &'a str,
  186. /// The list of template nodes that make up the template
  187. ///
  188. /// Unlike react, calls to `rsx!` can have multiple roots. This list supports that paradigm.
  189. #[cfg_attr(feature = "serialize", serde(deserialize_with = "deserialize_leaky"))]
  190. pub roots: &'a [TemplateNode<'a>],
  191. /// The paths of each node relative to the root of the template.
  192. ///
  193. /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
  194. /// topmost element, not the `roots` field.
  195. #[cfg_attr(
  196. feature = "serialize",
  197. serde(deserialize_with = "deserialize_bytes_leaky")
  198. )]
  199. pub node_paths: &'a [&'a [u8]],
  200. /// The paths of each dynamic attribute relative to the root of the template
  201. ///
  202. /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
  203. /// topmost element, not the `roots` field.
  204. #[cfg_attr(
  205. feature = "serialize",
  206. serde(deserialize_with = "deserialize_bytes_leaky")
  207. )]
  208. pub attr_paths: &'a [&'a [u8]],
  209. }
  210. #[cfg(feature = "serialize")]
  211. fn deserialize_string_leaky<'a, 'de, D>(deserializer: D) -> Result<&'a str, D::Error>
  212. where
  213. D: serde::Deserializer<'de>,
  214. {
  215. use serde::Deserialize;
  216. let deserialized = String::deserialize(deserializer)?;
  217. Ok(&*Box::leak(deserialized.into_boxed_str()))
  218. }
  219. #[cfg(feature = "serialize")]
  220. fn deserialize_bytes_leaky<'a, 'de, D>(deserializer: D) -> Result<&'a [&'a [u8]], D::Error>
  221. where
  222. D: serde::Deserializer<'de>,
  223. {
  224. use serde::Deserialize;
  225. let deserialized = Vec::<Vec<u8>>::deserialize(deserializer)?;
  226. let deserialized = deserialized
  227. .into_iter()
  228. .map(|v| &*Box::leak(v.into_boxed_slice()))
  229. .collect::<Vec<_>>();
  230. Ok(&*Box::leak(deserialized.into_boxed_slice()))
  231. }
  232. #[cfg(feature = "serialize")]
  233. fn deserialize_leaky<'a, 'de, T: serde::Deserialize<'de>, D>(
  234. deserializer: D,
  235. ) -> Result<&'a [T], D::Error>
  236. where
  237. T: serde::Deserialize<'de>,
  238. D: serde::Deserializer<'de>,
  239. {
  240. use serde::Deserialize;
  241. let deserialized = Box::<[T]>::deserialize(deserializer)?;
  242. Ok(&*Box::leak(deserialized))
  243. }
  244. impl<'a> Template<'a> {
  245. /// Is this template worth caching at all, since it's completely runtime?
  246. ///
  247. /// There's no point in saving templates that are completely dynamic, since they'll be recreated every time anyway.
  248. pub fn is_completely_dynamic(&self) -> bool {
  249. use TemplateNode::*;
  250. self.roots
  251. .iter()
  252. .all(|root| matches!(root, Dynamic { .. } | DynamicText { .. }))
  253. }
  254. }
  255. /// A statically known node in a layout.
  256. ///
  257. /// This can be created at compile time, saving the VirtualDom time when diffing the tree
  258. #[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
  259. #[cfg_attr(
  260. feature = "serialize",
  261. derive(serde::Serialize, serde::Deserialize),
  262. serde(tag = "type")
  263. )]
  264. pub enum TemplateNode<'a> {
  265. /// An statically known element in the dom.
  266. ///
  267. /// In HTML this would be something like `<div id="123"> </div>`
  268. Element {
  269. /// The name of the element
  270. ///
  271. /// IE for a div, it would be the string "div"
  272. tag: &'a str,
  273. /// The namespace of the element
  274. ///
  275. /// In HTML, this would be a valid URI that defines a namespace for all elements below it
  276. /// SVG is an example of this namespace
  277. namespace: Option<&'a str>,
  278. /// A list of possibly dynamic attribues for this element
  279. ///
  280. /// An attribute on a DOM node, such as `id="my-thing"` or `href="https://example.com"`.
  281. #[cfg_attr(feature = "serialize", serde(deserialize_with = "deserialize_leaky"))]
  282. attrs: &'a [TemplateAttribute<'a>],
  283. /// A list of template nodes that define another set of template nodes
  284. #[cfg_attr(feature = "serialize", serde(deserialize_with = "deserialize_leaky"))]
  285. children: &'a [TemplateNode<'a>],
  286. },
  287. /// This template node is just a piece of static text
  288. Text {
  289. /// The actual text
  290. text: &'a str,
  291. },
  292. /// This template node is unknown, and needs to be created at runtime.
  293. Dynamic {
  294. /// The index of the dynamic node in the VNode's dynamic_nodes list
  295. id: usize,
  296. },
  297. /// This template node is known to be some text, but needs to be created at runtime
  298. ///
  299. /// This is separate from the pure Dynamic variant for various optimizations
  300. DynamicText {
  301. /// The index of the dynamic node in the VNode's dynamic_nodes list
  302. id: usize,
  303. },
  304. }
  305. /// A node created at runtime
  306. ///
  307. /// This node's index in the DynamicNode list on VNode should match its repsective `Dynamic` index
  308. #[derive(Debug)]
  309. pub enum DynamicNode<'a> {
  310. /// A component node
  311. ///
  312. /// Most of the time, Dioxus will actually know which component this is as compile time, but the props and
  313. /// assigned scope are dynamic.
  314. ///
  315. /// The actual VComponent can be dynamic between two VNodes, though, allowing implementations to swap
  316. /// the render function at runtime
  317. Component(VComponent<'a>),
  318. /// A text node
  319. Text(VText<'a>),
  320. /// A placeholder
  321. ///
  322. /// Used by suspense when a node isn't ready and by fragments that don't render anything
  323. ///
  324. /// In code, this is just an ElementId whose initial value is set to 0 upon creation
  325. Placeholder(VPlaceholder),
  326. /// A list of VNodes.
  327. ///
  328. /// Note that this is not a list of dynamic nodes. These must be VNodes and created through conditional rendering
  329. /// or iterators.
  330. Fragment(&'a [VNode<'a>]),
  331. }
  332. impl Default for DynamicNode<'_> {
  333. fn default() -> Self {
  334. Self::Placeholder(Default::default())
  335. }
  336. }
  337. /// An instance of a child component
  338. pub struct VComponent<'a> {
  339. /// The name of this component
  340. pub name: &'static str,
  341. /// Are the props valid for the 'static lifetime?
  342. ///
  343. /// Internally, this is used as a guarantee. Externally, this might be incorrect, so don't count on it.
  344. ///
  345. /// This flag is assumed by the [`crate::Properties`] trait which is unsafe to implement
  346. pub static_props: bool,
  347. /// The assigned Scope for this component
  348. pub scope: Cell<Option<ScopeId>>,
  349. /// The function pointer of the component, known at compile time
  350. ///
  351. /// It is possible that components get folded at comppile time, so these shouldn't be really used as a key
  352. pub render_fn: *const (),
  353. pub(crate) props: RefCell<Option<Box<dyn AnyProps<'a> + 'a>>>,
  354. }
  355. impl<'a> std::fmt::Debug for VComponent<'a> {
  356. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  357. f.debug_struct("VComponent")
  358. .field("name", &self.name)
  359. .field("static_props", &self.static_props)
  360. .field("scope", &self.scope)
  361. .finish()
  362. }
  363. }
  364. /// An instance of some text, mounted to the DOM
  365. #[derive(Debug)]
  366. pub struct VText<'a> {
  367. /// The actual text itself
  368. pub value: &'a str,
  369. /// The ID of this node in the real DOM
  370. pub id: Cell<Option<ElementId>>,
  371. }
  372. /// A placeholder node, used by suspense and fragments
  373. #[derive(Debug, Default)]
  374. pub struct VPlaceholder {
  375. /// The ID of this node in the real DOM
  376. pub id: Cell<Option<ElementId>>,
  377. }
  378. /// An attribute of the TemplateNode, created at compile time
  379. #[derive(Debug, PartialEq, Hash, Eq, PartialOrd, Ord)]
  380. #[cfg_attr(
  381. feature = "serialize",
  382. derive(serde::Serialize, serde::Deserialize),
  383. serde(tag = "type")
  384. )]
  385. pub enum TemplateAttribute<'a> {
  386. /// This attribute is entirely known at compile time, enabling
  387. Static {
  388. /// The name of this attribute.
  389. ///
  390. /// For example, the `href` attribute in `href="https://example.com"`, would have the name "href"
  391. name: &'a str,
  392. /// The value of this attribute, known at compile time
  393. ///
  394. /// Currently this only accepts &str, so values, even if they're known at compile time, are not known
  395. value: &'a str,
  396. /// The namespace of this attribute. Does not exist in the HTML spec
  397. namespace: Option<&'a str>,
  398. },
  399. /// The attribute in this position is actually determined dynamically at runtime
  400. ///
  401. /// This is the index into the dynamic_attributes field on the container VNode
  402. Dynamic {
  403. /// The index
  404. id: usize,
  405. },
  406. }
  407. /// An attribute on a DOM node, such as `id="my-thing"` or `href="https://example.com"`
  408. #[derive(Debug)]
  409. pub struct Attribute<'a> {
  410. /// The name of the attribute.
  411. pub name: &'a str,
  412. /// The value of the attribute
  413. pub value: AttributeValue<'a>,
  414. /// The namespace of the attribute.
  415. ///
  416. /// Doesn’t exist in the html spec. Used in Dioxus to denote “style” tags and other attribute groups.
  417. pub namespace: Option<&'static str>,
  418. /// The element in the DOM that this attribute belongs to
  419. pub mounted_element: Cell<ElementId>,
  420. /// An indication of we should always try and set the attribute. Used in controlled components to ensure changes are propagated
  421. pub volatile: bool,
  422. }
  423. /// Any of the built-in values that the Dioxus VirtualDom supports as dynamic attributes on elements
  424. ///
  425. /// These are built-in to be faster during the diffing process. To use a custom value, use the [`AttributeValue::Any`]
  426. /// variant.
  427. pub enum AttributeValue<'a> {
  428. /// Text attribute
  429. Text(&'a str),
  430. /// A float
  431. Float(f64),
  432. /// Signed integer
  433. Int(i64),
  434. /// Boolean
  435. Bool(bool),
  436. /// A listener, like "onclick"
  437. Listener(RefCell<Option<ListenerCb<'a>>>),
  438. /// An arbitrary value that implements PartialEq and is static
  439. Any(BumpBox<'a, dyn AnyValue>),
  440. /// A "none" value, resulting in the removal of an attribute from the dom
  441. None,
  442. }
  443. type ListenerCb<'a> = BumpBox<'a, dyn FnMut(Event<dyn Any>) + 'a>;
  444. impl<'a> std::fmt::Debug for AttributeValue<'a> {
  445. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  446. match self {
  447. Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
  448. Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
  449. Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
  450. Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
  451. Self::Listener(_) => f.debug_tuple("Listener").finish(),
  452. Self::Any(_) => f.debug_tuple("Any").finish(),
  453. Self::None => write!(f, "None"),
  454. }
  455. }
  456. }
  457. impl<'a> PartialEq for AttributeValue<'a> {
  458. fn eq(&self, other: &Self) -> bool {
  459. match (self, other) {
  460. (Self::Text(l0), Self::Text(r0)) => l0 == r0,
  461. (Self::Float(l0), Self::Float(r0)) => l0 == r0,
  462. (Self::Int(l0), Self::Int(r0)) => l0 == r0,
  463. (Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
  464. (Self::Listener(_), Self::Listener(_)) => true,
  465. (Self::Any(l0), Self::Any(r0)) => l0.any_cmp(r0.as_ref()),
  466. _ => core::mem::discriminant(self) == core::mem::discriminant(other),
  467. }
  468. }
  469. }
  470. #[doc(hidden)]
  471. pub trait AnyValue {
  472. fn any_cmp(&self, other: &dyn AnyValue) -> bool;
  473. fn our_typeid(&self) -> TypeId;
  474. }
  475. impl<T: PartialEq + Any> AnyValue for T {
  476. fn any_cmp(&self, other: &dyn AnyValue) -> bool {
  477. if self.type_id() != other.our_typeid() {
  478. return false;
  479. }
  480. self == unsafe { &*(other as *const _ as *const T) }
  481. }
  482. fn our_typeid(&self) -> TypeId {
  483. self.type_id()
  484. }
  485. }
  486. #[doc(hidden)]
  487. pub trait ComponentReturn<'a, A = ()> {
  488. fn into_return(self, cx: &'a ScopeState) -> RenderReturn<'a>;
  489. }
  490. impl<'a> ComponentReturn<'a> for Element<'a> {
  491. fn into_return(self, _cx: &ScopeState) -> RenderReturn<'a> {
  492. RenderReturn::Sync(self)
  493. }
  494. }
  495. #[doc(hidden)]
  496. pub struct AsyncMarker;
  497. impl<'a, F> ComponentReturn<'a, AsyncMarker> for F
  498. where
  499. F: Future<Output = Element<'a>> + 'a,
  500. {
  501. fn into_return(self, cx: &'a ScopeState) -> RenderReturn<'a> {
  502. let f: &mut dyn Future<Output = Element<'a>> = cx.bump().alloc(self);
  503. RenderReturn::Async(unsafe { BumpBox::from_raw(f) })
  504. }
  505. }
  506. impl<'a> RenderReturn<'a> {
  507. pub(crate) unsafe fn extend_lifetime_ref<'c>(&self) -> &'c RenderReturn<'c> {
  508. unsafe { std::mem::transmute(self) }
  509. }
  510. pub(crate) unsafe fn extend_lifetime<'c>(self) -> RenderReturn<'c> {
  511. unsafe { std::mem::transmute(self) }
  512. }
  513. }
  514. /// A trait that allows various items to be converted into a dynamic node for the rsx macro
  515. pub trait IntoDynNode<'a, A = ()> {
  516. /// Consume this item along with a scopestate and produce a DynamicNode
  517. ///
  518. /// You can use the bump alloactor of the scopestate to creat the dynamic node
  519. fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a>;
  520. }
  521. impl<'a> IntoDynNode<'a> for () {
  522. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  523. DynamicNode::default()
  524. }
  525. }
  526. impl<'a> IntoDynNode<'a> for VNode<'a> {
  527. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  528. DynamicNode::Fragment(_cx.bump().alloc([self]))
  529. }
  530. }
  531. impl<'a> IntoDynNode<'a> for DynamicNode<'a> {
  532. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  533. self
  534. }
  535. }
  536. impl<'a, T: IntoDynNode<'a>> IntoDynNode<'a> for Option<T> {
  537. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  538. match self {
  539. Some(val) => val.into_vnode(_cx),
  540. None => DynamicNode::default(),
  541. }
  542. }
  543. }
  544. impl<'a> IntoDynNode<'a> for &Element<'a> {
  545. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  546. match self.as_ref() {
  547. Some(val) => val.clone().into_vnode(_cx),
  548. _ => DynamicNode::default(),
  549. }
  550. }
  551. }
  552. impl<'a, 'b> IntoDynNode<'a> for LazyNodes<'a, 'b> {
  553. fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a> {
  554. DynamicNode::Fragment(cx.bump().alloc([self.call(cx)]))
  555. }
  556. }
  557. impl<'a> IntoDynNode<'_> for &'a str {
  558. fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
  559. cx.text_node(format_args!("{}", self))
  560. }
  561. }
  562. impl IntoDynNode<'_> for String {
  563. fn into_vnode(self, cx: &ScopeState) -> DynamicNode {
  564. cx.text_node(format_args!("{}", self))
  565. }
  566. }
  567. impl<'b> IntoDynNode<'b> for Arguments<'_> {
  568. fn into_vnode(self, cx: &'b ScopeState) -> DynamicNode<'b> {
  569. cx.text_node(self)
  570. }
  571. }
  572. impl<'a> IntoDynNode<'a> for &'a VNode<'a> {
  573. fn into_vnode(self, _cx: &'a ScopeState) -> DynamicNode<'a> {
  574. DynamicNode::Fragment(_cx.bump().alloc([VNode {
  575. parent: self.parent,
  576. template: self.template.clone(),
  577. root_ids: self.root_ids.clone(),
  578. key: self.key,
  579. dynamic_nodes: self.dynamic_nodes,
  580. dynamic_attrs: self.dynamic_attrs,
  581. }]))
  582. }
  583. }
  584. pub trait IntoTemplate<'a> {
  585. fn into_template(self, _cx: &'a ScopeState) -> VNode<'a>;
  586. }
  587. impl<'a> IntoTemplate<'a> for VNode<'a> {
  588. fn into_template(self, _cx: &'a ScopeState) -> VNode<'a> {
  589. self
  590. }
  591. }
  592. impl<'a> IntoTemplate<'a> for Element<'a> {
  593. fn into_template(self, _cx: &'a ScopeState) -> VNode<'a> {
  594. match self {
  595. Some(val) => val.into_template(_cx),
  596. _ => VNode::empty().unwrap(),
  597. }
  598. }
  599. }
  600. impl<'a, 'b> IntoTemplate<'a> for LazyNodes<'a, 'b> {
  601. fn into_template(self, cx: &'a ScopeState) -> VNode<'a> {
  602. self.call(cx)
  603. }
  604. }
  605. // Note that we're using the E as a generic but this is never crafted anyways.
  606. #[doc(hidden)]
  607. pub struct FromNodeIterator;
  608. impl<'a, T, I> IntoDynNode<'a, FromNodeIterator> for T
  609. where
  610. T: Iterator<Item = I>,
  611. I: IntoTemplate<'a>,
  612. {
  613. fn into_vnode(self, cx: &'a ScopeState) -> DynamicNode<'a> {
  614. let mut nodes = bumpalo::collections::Vec::new_in(cx.bump());
  615. nodes.extend(self.into_iter().map(|node| node.into_template(cx)));
  616. match nodes.into_bump_slice() {
  617. children if children.is_empty() => DynamicNode::default(),
  618. children => DynamicNode::Fragment(children),
  619. }
  620. }
  621. }
  622. /// A value that can be converted into an attribute value
  623. pub trait IntoAttributeValue<'a> {
  624. /// Convert into an attribute value
  625. fn into_value(self, bump: &'a Bump) -> AttributeValue<'a>;
  626. }
  627. impl<'a> IntoAttributeValue<'a> for &'a str {
  628. fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
  629. AttributeValue::Text(self)
  630. }
  631. }
  632. impl<'a> IntoAttributeValue<'a> for f64 {
  633. fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
  634. AttributeValue::Float(self)
  635. }
  636. }
  637. impl<'a> IntoAttributeValue<'a> for i64 {
  638. fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
  639. AttributeValue::Int(self)
  640. }
  641. }
  642. impl<'a> IntoAttributeValue<'a> for bool {
  643. fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
  644. AttributeValue::Bool(self)
  645. }
  646. }
  647. impl<'a> IntoAttributeValue<'a> for Arguments<'_> {
  648. fn into_value(self, bump: &'a Bump) -> AttributeValue<'a> {
  649. use bumpalo::core_alloc::fmt::Write;
  650. let mut str_buf = bumpalo::collections::String::new_in(bump);
  651. str_buf.write_fmt(self).unwrap();
  652. AttributeValue::Text(str_buf.into_bump_str())
  653. }
  654. }