Skip to content

Commit d02189b

Browse files
committed
feat: ✨ removed Rcs completely
Anything wrapped in an `Rc` in a `Template` or an error page no longer needs it! BREAKING CHANGE: `Rc`s are eliminated and done behind the scenes
1 parent 488a9a0 commit d02189b

File tree

26 files changed

+152
-169
lines changed

26 files changed

+152
-169
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ Here's a taste of Perseus (see [the _tiny_ example](https://github.com/arctic-he
2222

2323
```rust
2424
use perseus::{define_app, ErrorPages, Template};
25-
use std::rc::Rc;
2625
use sycamore::template;
2726
define_app! {
2827
templates: [
29-
Template::<G>::new("index").template(Rc::new(|_| {
28+
Template::<G>::new("index").template(|_| {
3029
template! {
3130
p { "Hello World!" }
3231
}
33-
}))
32+
})
3433
],
35-
error_pages: ErrorPages::new(Rc::new(|url, status, err, _| {
34+
error_pages: ErrorPages::new(|url, status, err, _| {
3635
template! {
3736
p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) }
3837
}
39-
}))
38+
})
4039
}
4140
```
4241

docs/0.3.x/en-US/updating.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Perseus v0.3.0 added significant architectural changes to Perseus under the hood
88
2. Upgrade the Perseus CLI with `cargo install perseus-cli`.
99
3. Run `perseus clean` to remove the old `.perseus/` directory.
1010
4. Remove any custom config managers you may have, they've been replaced by [mutable and immutable stores](:stores).
11-
5. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG).
11+
5. Take anything Perseus-related wrapped in `Rc::new` (these will be all through your template definitions and error pages) and remove the `Rc::new`, Perseus now handles that internally, with no performance cost!
12+
6. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG).
1213

1314
Perseus v0.3.0 also changed a few common idioms, like breaking out the `.template()` call into a separate function `template_fn()`. This is no longer recommended, though it will still work fine. You can check out the [examples directory](https://github.com/arctic-hen7/perseus/tree/main/examples) to see how things are a bit nicer now in terms of formatting.
1415

docs/next/en-US/updating.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Perseus v0.3.0 added significant architectural changes to Perseus under the hood
88
2. Upgrade the Perseus CLI with `cargo install perseus-cli`.
99
3. Run `perseus clean` to remove the old `.perseus/` directory.
1010
4. Remove any custom config managers you may have, they've been replaced by [mutable and immutable stores](:stores).
11-
5. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG).
11+
5. Take anything Perseus-related wrapped in `Rc::new` (these will be all through your template definitions and error pages) and remove the `Rc::new`, Perseus now handles that internally, with no performance cost!
12+
6. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG).
1213

1314
Perseus v0.3.0 also changed a few common idioms, like breaking out the `.template()` call into a separate function `template_fn()`. This is no longer recommended, though it will still work fine. You can check out the [examples directory](https://github.com/arctic-hen7/perseus/tree/main/examples) to see how things are a bit nicer now in terms of formatting.
1415

examples/basic/src/error_pages.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
use perseus::{ErrorPages, GenericNode};
2-
use std::rc::Rc;
32
use sycamore::template;
43

54
pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
6-
let mut error_pages = ErrorPages::new(Rc::new(|url, status, err, _| {
5+
let mut error_pages = ErrorPages::new(|url, status, err, _| {
76
template! {
87
p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) }
98
}
10-
}));
11-
error_pages.add_page(
12-
404,
13-
Rc::new(|_, _, _, _| {
14-
template! {
15-
p { "Page not found." }
16-
}
17-
}),
18-
);
9+
});
10+
error_pages.add_page(404, |_, _, _, _| {
11+
template! {
12+
p { "Page not found." }
13+
}
14+
});
1915

2016
error_pages
2117
}

examples/basic/src/templates/about.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use perseus::Template;
2-
use std::rc::Rc;
32
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
43

54
#[component(AboutPage<G>)]
@@ -11,14 +10,14 @@ pub fn about_page() -> SycamoreTemplate<G> {
1110

1211
pub fn get_template<G: GenericNode>() -> Template<G> {
1312
Template::new("about")
14-
.template(Rc::new(|_| {
13+
.template(|_| {
1514
template! {
1615
AboutPage()
1716
}
18-
}))
19-
.head(Rc::new(|_| {
17+
})
18+
.head(|_| {
2019
template! {
2120
title { "About Page | Perseus Example – Basic" }
2221
}
23-
}))
22+
})
2423
}

examples/basic/src/templates/index.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use perseus::{
33
GenericNode, RenderFnResultWithCause, Template,
44
};
55
use serde::{Deserialize, Serialize};
6-
use std::rc::Rc;
76
use sycamore::prelude::{component, template, Template as SycamoreTemplate};
87

98
#[derive(Serialize, Deserialize, Debug)]
@@ -21,20 +20,20 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
2120

2221
pub fn get_template<G: GenericNode>() -> Template<G> {
2322
Template::new("index")
24-
.build_state_fn(Rc::new(get_build_props))
25-
.template(Rc::new(|props: Option<String>| {
23+
.build_state_fn(get_build_props)
24+
.template(|props: Option<String>| {
2625
template! {
2726
IndexPage(
2827
serde_json::from_str::<IndexPageProps>(&props.unwrap()).unwrap()
2928
)
3029
}
31-
}))
32-
.head(Rc::new(|_| {
30+
})
31+
.head(|_| {
3332
template! {
3433
title { "Index Page | Perseus Example – Basic" }
3534
}
36-
}))
37-
.set_headers_fn(Rc::new(set_headers))
35+
})
36+
.set_headers_fn(set_headers)
3837
}
3938

4039
pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {

examples/i18n/src/error_pages.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
use perseus::{ErrorPages, GenericNode};
2-
use std::rc::Rc;
32
use sycamore::template;
43

54
pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
6-
let mut error_pages = ErrorPages::new(Rc::new(|_, _, err, _| {
5+
let mut error_pages = ErrorPages::new(|_, _, err, _| {
76
template! {
87
p { (format!("Another error occurred: '{}'.", err)) }
98
}
10-
}));
11-
error_pages.add_page(
12-
404,
13-
Rc::new(|_, _, _, _| {
14-
template! {
15-
p { "Page not found." }
16-
}
17-
}),
18-
);
19-
error_pages.add_page(
20-
400,
21-
Rc::new(|_, _, _, _| {
22-
template! {
23-
p { "Client error occurred..." }
24-
}
25-
}),
26-
);
9+
});
10+
error_pages.add_page(404, |_, _, _, _| {
11+
template! {
12+
p { "Page not found." }
13+
}
14+
});
15+
error_pages.add_page(400, |_, _, _, _| {
16+
template! {
17+
p { "Client error occurred..." }
18+
}
19+
});
2720

2821
error_pages
2922
}

examples/i18n/src/templates/about.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use perseus::{is_server, t, Template};
2-
use std::rc::Rc;
32
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
43

54
#[component(AboutPage<G>)]
@@ -19,9 +18,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
1918
}
2019

2120
pub fn get_template<G: GenericNode>() -> Template<G> {
22-
Template::new("about").template(Rc::new(|_| {
21+
Template::new("about").template(|_| {
2322
template! {
2423
AboutPage()
2524
}
26-
}))
25+
})
2726
}

examples/i18n/src/templates/index.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use perseus::{link, t, Template};
2-
use std::rc::Rc;
32
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
43

54
#[component(IndexPage<G>)]
@@ -14,9 +13,9 @@ pub fn index_page() -> SycamoreTemplate<G> {
1413
}
1514

1615
pub fn get_template<G: GenericNode>() -> Template<G> {
17-
Template::new("index").template(Rc::new(|_| {
16+
Template::new("index").template(|_| {
1817
template! {
1918
IndexPage()
2019
}
21-
}))
20+
})
2221
}

examples/i18n/src/templates/post.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use perseus::{link, RenderFnResult, RenderFnResultWithCause, Template};
22
use serde::{Deserialize, Serialize};
3-
use std::rc::Rc;
43
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
54

65
#[derive(Serialize, Deserialize)]
@@ -28,15 +27,15 @@ pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {
2827

2928
pub fn get_template<G: GenericNode>() -> Template<G> {
3029
Template::new("post")
31-
.build_paths_fn(Rc::new(get_static_paths))
32-
.build_state_fn(Rc::new(get_static_props))
33-
.template(Rc::new(|props| {
30+
.build_paths_fn(get_static_paths)
31+
.build_state_fn(get_static_props)
32+
.template(|props| {
3433
template! {
3534
PostPage(
3635
serde_json::from_str::<PostPageProps>(&props.unwrap()).unwrap()
3736
)
3837
}
39-
}))
38+
})
4039
}
4140

4241
pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {

examples/showcase/src/error_pages.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
use perseus::{ErrorPages, GenericNode};
2-
use std::rc::Rc;
32
use sycamore::template;
43

54
pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
6-
let mut error_pages = ErrorPages::new(Rc::new(|_, _, _, _| {
5+
let mut error_pages = ErrorPages::new(|_, _, _, _| {
76
template! {
87
p { "Another error occurred." }
98
}
10-
}));
11-
error_pages.add_page(
12-
404,
13-
Rc::new(|_, _, err, _| {
14-
template! {
15-
p { "Page not found." }
16-
p { (err) }
17-
}
18-
}),
19-
);
20-
error_pages.add_page(
21-
400,
22-
Rc::new(|_, _, _, _| {
23-
template! {
24-
p { "Client error occurred..." }
25-
}
26-
}),
27-
);
9+
});
10+
error_pages.add_page(404, |_, _, err, _| {
11+
template! {
12+
p { "Page not found." }
13+
p { (err) }
14+
}
15+
});
16+
error_pages.add_page(400, |_, _, _, _| {
17+
template! {
18+
p { "Client error occurred..." }
19+
}
20+
});
2821

2922
error_pages
3023
}
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use perseus::Template;
2-
use std::rc::Rc;
32
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
43

54
#[component(AboutPage<G>)]
@@ -10,9 +9,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
109
}
1110

1211
pub fn get_template<G: GenericNode>() -> Template<G> {
13-
Template::new("about").template(Rc::new(|_| {
12+
Template::new("about").template(|_| {
1413
template! {
1514
AboutPage()
1615
}
17-
}))
16+
})
1817
}

examples/showcase/src/templates/amalgamation.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use perseus::{RenderFnResultWithCause, Request, States, Template};
22
use serde::{Deserialize, Serialize};
3-
use std::rc::Rc;
43
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
54

65
#[derive(Serialize, Deserialize, Debug)]
@@ -17,16 +16,16 @@ pub fn amalgamation_page(props: AmalagamationPageProps) -> SycamoreTemplate<G> {
1716

1817
pub fn get_template<G: GenericNode>() -> Template<G> {
1918
Template::new("amalgamation")
20-
.build_state_fn(Rc::new(get_build_state))
21-
.request_state_fn(Rc::new(get_request_state))
22-
.amalgamate_states_fn(Rc::new(amalgamate_states))
23-
.template(Rc::new(|props| {
19+
.build_state_fn(get_build_state)
20+
.request_state_fn(get_request_state)
21+
.amalgamate_states_fn(amalgamate_states)
22+
.template(|props| {
2423
template! {
2524
AmalgamationPage(
2625
serde_json::from_str::<AmalagamationPageProps>(&props.unwrap()).unwrap()
2726
)
2827
}
29-
}))
28+
})
3029
}
3130

3231
pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<String>> {

examples/showcase/src/templates/index.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use perseus::{RenderFnResultWithCause, Template};
22
use serde::{Deserialize, Serialize};
3-
use std::rc::Rc;
43
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
54

65
#[derive(Serialize, Deserialize, Debug)]
@@ -17,14 +16,14 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
1716

1817
pub fn get_template<G: GenericNode>() -> Template<G> {
1918
Template::new("index")
20-
.build_state_fn(Rc::new(get_static_props))
21-
.template(Rc::new(|props| {
19+
.build_state_fn(get_static_props)
20+
.template(|props| {
2221
template! {
2322
IndexPage(
2423
serde_json::from_str::<IndexPageProps>(&props.unwrap()).unwrap()
2524
)
2625
}
27-
}))
26+
})
2827
}
2928

3029
pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {

examples/showcase/src/templates/ip.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use perseus::{RenderFnResultWithCause, Request, Template};
22
use serde::{Deserialize, Serialize};
3-
use std::rc::Rc;
43
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
54

65
#[derive(Serialize, Deserialize)]
@@ -21,14 +20,14 @@ pub fn dashboard_page(props: IpPageProps) -> SycamoreTemplate<G> {
2120

2221
pub fn get_template<G: GenericNode>() -> Template<G> {
2322
Template::new("ip")
24-
.request_state_fn(Rc::new(get_request_state))
25-
.template(Rc::new(|props| {
23+
.request_state_fn(get_request_state)
24+
.template(|props| {
2625
template! {
2726
IpPage(
2827
serde_json::from_str::<IpPageProps>(&props.unwrap()).unwrap()
2928
)
3029
}
31-
}))
30+
})
3231
}
3332

3433
pub async fn get_request_state(
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use perseus::Template;
2-
use std::rc::Rc;
32
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
43

54
#[component(NewPostPage<G>)]
@@ -10,9 +9,9 @@ pub fn new_post_page() -> SycamoreTemplate<G> {
109
}
1110

1211
pub fn get_template<G: GenericNode>() -> Template<G> {
13-
Template::new("post/new").template(Rc::new(|_| {
12+
Template::new("post/new").template(|_| {
1413
template! {
1514
NewPostPage()
1615
}
17-
}))
16+
})
1817
}

0 commit comments

Comments
 (0)