소스 검색

Merge pull request #2064 from ealmloff/make-resource-copy

Implement copy for Resource
Jonathan Kelley 1 년 전
부모
커밋
d180f569cf
2개의 변경된 파일17개의 추가작업 그리고 6개의 파일을 삭제
  1. 7 0
      packages/hooks/src/use_resource.rs
  2. 10 6
      packages/rsx/src/lib.rs

+ 7 - 0
packages/hooks/src/use_resource.rs

@@ -119,6 +119,13 @@ pub struct Resource<T: 'static> {
     callback: UseCallback<Task>,
 }
 
+impl<T> Clone for Resource<T> {
+    fn clone(&self) -> Self {
+        *self
+    }
+}
+impl<T> Copy for Resource<T> {}
+
 /// A signal that represents the state of the resource
 // we might add more states (panicked, etc)
 #[derive(Clone, Copy, PartialEq, Hash, Eq, Debug)]

+ 10 - 6
packages/rsx/src/lib.rs

@@ -244,12 +244,16 @@ impl<'a> ToTokens for TemplateRenderer<'a> {
                 attr_paths: &[ #(#attr_paths),* ],
             };
 
-            dioxus_core::VNode::new(
-                #key_tokens,
-                TEMPLATE,
-                Box::new([ #( #node_printer),* ]),
-                Box::new([ #(#dyn_attr_printer),* ]),
-            )
+            {
+                // NOTE: Allocating a temporary is important to make reads within rsx drop before the value is returned
+                let __vnodes = dioxus_core::VNode::new(
+                    #key_tokens,
+                    TEMPLATE,
+                    Box::new([ #( #node_printer),* ]),
+                    Box::new([ #(#dyn_attr_printer),* ]),
+                );
+                __vnodes
+            }
         });
     }
 }