-
-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 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,56 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
punctuated::Punctuated, | ||
Token, | ||
}; | ||
|
||
struct MemoMacroInput { | ||
root: syn::Ident, | ||
path: Punctuated<syn::Member, Token![.]>, | ||
} | ||
|
||
impl Parse for MemoMacroInput { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let root: syn::Ident = input.parse()?; | ||
input.parse::<Token![.]>()?; | ||
// do not accept trailing punctuation | ||
let path: Punctuated<syn::Member, Token![.]> = | ||
Punctuated::parse_separated_nonempty(input)?; | ||
|
||
if path.is_empty() { | ||
return Err(input.error("expected identifier")); | ||
} | ||
|
||
if !input.is_empty() { | ||
return Err(input.error("unexpected token")); | ||
} | ||
|
||
Ok(Self { root, path }) | ||
} | ||
} | ||
|
||
impl ToTokens for MemoMacroInput { | ||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { | ||
let root = &self.root; | ||
let path = &self.path; | ||
|
||
tokens.extend(quote! { | ||
::leptos::reactive_graph::computed::Memo::new( | ||
move |_| { | ||
use ::leptos::reactive_graph::traits::With; | ||
#root.with(|st: _| st.#path.clone()) | ||
} | ||
) | ||
}) | ||
} | ||
} | ||
|
||
pub fn memo_impl(tokens: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(tokens as MemoMacroInput); | ||
input.into_token_stream().into() | ||
} |
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,33 @@ | ||
use leptos::prelude::RwSignal; | ||
use leptos_macro::memo; | ||
|
||
#[derive(Default)] | ||
pub struct OuterState { | ||
count: i32, | ||
inner: InnerState, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Default)] | ||
pub struct InnerState { | ||
inner_count: i32, | ||
inner_tuple: InnerTuple, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Default)] | ||
pub struct InnerTuple(String); | ||
|
||
#[test] | ||
fn green() { | ||
let outer_signal = RwSignal::new(OuterState::default()); | ||
|
||
let _ = memo!(outer_signal.count); | ||
|
||
let _ = memo!(outer_signal.inner.inner_count); | ||
let _ = memo!(outer_signal.inner.inner_tuple.0); | ||
} | ||
|
||
#[test] | ||
fn red() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/memo/red.rs") | ||
} |
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,26 @@ | ||
use leptos::prelude::RwSignal; | ||
use leptos_macro::memo; | ||
|
||
#[derive(Default, PartialEq)] | ||
pub struct OuterState { | ||
count: i32, | ||
inner: InnerState, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Default)] | ||
pub struct InnerState { | ||
inner_count: i32, | ||
inner_name: String, | ||
} | ||
|
||
fn main() { | ||
let outer_signal = RwSignal::new(OuterState::default()); | ||
|
||
let _ = memo!(); | ||
|
||
let _ = memo!(outer_signal); | ||
|
||
let _ = memo!(outer_signal.); | ||
|
||
let _ = memo!(outer_signal.inner.); | ||
} |
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,31 @@ | ||
error: unexpected end of input, expected identifier | ||
--> tests/memo/red.rs:19:13 | ||
| | ||
19 | let _ = memo!(); | ||
| ^^^^^^^ | ||
| | ||
= note: this error originates in the macro `memo` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: expected `.` | ||
--> tests/memo/red.rs:21:13 | ||
| | ||
21 | let _ = memo!(outer_signal); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `memo` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: unexpected end of input, expected identifier or integer | ||
--> tests/memo/red.rs:23:13 | ||
| | ||
23 | let _ = memo!(outer_signal.); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `memo` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: unexpected end of input, expected identifier or integer | ||
--> tests/memo/red.rs:25:13 | ||
| | ||
25 | let _ = memo!(outer_signal.inner.); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `memo` (in Nightly builds, run with -Z macro-backtrace for more info) |