-
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1332 from Demonthos/fix-variance
Separate Parent and Child Component Lifetimes
- Loading branch information
Showing
5 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" } | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters