Merge remote-tracking branch 'main/signals' into signals

This commit is contained in:
Evan Almloff committed 2023-08-10 17:20:27 -07:00
commit ee2619abfd
12 files changed
+258 -12

No files matched your search

+1
View File
@@ -39,6 +39,7 @@ dioxus = { workspace = true }
pretty_assertions = "1.3.0"
rand = "0.8.5"
dioxus-ssr = { workspace = true }
trybuild = "1.0"
[features]
default = []
@@ -0,0 +1,32 @@
use dioxus::prelude::*;
fn main() {}
fn app(cx: Scope) -> Element {
let count: &RefCell<Vec<Element>> = cx.use_hook(|| RefCell::new(Vec::new()));
render! {
unsafe_child_component {
borrowed: count
}
}
}
#[derive(Props)]
struct Testing<'a> {
borrowed: &'a RefCell<Vec<Element<'a>>>,
}
fn unsafe_child_component<'a>(cx: Scope<'a, Testing<'a>>) -> Element<'a> {
let Testing { borrowed } = cx.props;
let borrowed_temporary_data =
cx.use_hook(|| String::from("This data is only valid for the lifetime of the child"));
borrowed
.borrow_mut()
.push(render! {"{borrowed_temporary_data}"});
cx.render(rsx! {
div { "Hello, world!" }
})
}
@@ -0,0 +1,20 @@
error[E0521]: borrowed data escapes outside of function
--> compile_tests/props_safety.rs:8:5
|
5 | fn app(cx: Scope) -> Element {
| --
| |
| `cx` is a reference that is only valid in the function body
| has type `&'1 Scoped<'1>`
...
8 | / render! {
9 | | unsafe_child_component {
10 | | borrowed: count
11 | | }
12 | | }
| | ^
| | |
| |_____`cx` escapes the function body here
| argument requires that `'1` must outlive `'static`
|
= note: this error originates in the macro `render` (in Nightly builds, run with -Z macro-backtrace for more info)
+7
View File
@@ -73,3 +73,10 @@ impl EmptyBuilder {
pub fn fc_to_builder<'a, T: Properties + 'a>(_: fn(Scope<'a, T>) -> Element<'a>) -> T::Builder {
T::builder()
}
#[cfg(not(miri))]
#[test]
fn unsafe_props_fail() {
let t = trybuild::TestCases::new();
t.compile_fail("compile_tests/props_safety.rs");
}
+5 -4
View File
@@ -417,19 +417,20 @@ impl<'src> ScopeState {
/// fn(Scope<Props>) -> Element;
/// async fn(Scope<Props<'_>>) -> Element;
/// ```
pub fn component<P>(
pub fn component<'child, P>(
&'src self,
component: fn(Scope<'src, P>) -> Element<'src>,
component: fn(Scope<'child, P>) -> Element<'child>,
props: P,
fn_name: &'static str,
) -> DynamicNode<'src>
where
P: Properties + 'src,
P: Properties + 'child,
'src: 'child,
{
let vcomp = VProps::new(component, P::memoize, props);
// cast off the lifetime of the render return
let as_dyn: Box<dyn AnyProps<'src> + '_> = Box::new(vcomp);
let as_dyn: Box<dyn AnyProps<'child> + '_> = Box::new(vcomp);
let extended: Box<dyn AnyProps<'src> + 'src> = unsafe { std::mem::transmute(as_dyn) };
DynamicNode::Component(VComponent {
+1 -1
View File
@@ -112,7 +112,7 @@ impl Service for ServerFnHandler {
let mut res = http::Response::builder();
// Set the headers from the server context
let parts = server_context.parts.read().unwrap();
let parts = server_context.response_parts().unwrap();
*res.headers_mut().expect("empty headers should be valid") = parts.headers.clone();
let serialized = result?;
+6 -5
View File
@@ -119,21 +119,22 @@ impl<Props: Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync
}
#[cfg(feature = "web")]
fn launch_web(self) {
/// Launch the web application
pub fn launch_web(self) {
let cfg = self.web_cfg.hydrate(true);
dioxus_web::launch_with_props(self.component, get_root_props_from_document().unwrap(), cfg);
}
#[cfg(feature = "desktop")]
fn launch_desktop(self) {
/// Launch the web application
pub fn launch_desktop(self) {
let cfg = self.desktop_cfg;
dioxus_desktop::launch_with_props(self.component, self.props, cfg);
}
/// Launch a server with the given configuration
/// This will use the routing integration of the currently enabled integration feature
#[cfg(feature = "ssr")]
async fn launch_server(self) {
/// Launch a server application
pub async fn launch_server(self) {
let addr = self.addr;
println!("Listening on {}", addr);
let cfg = self.server_cfg.build();