Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kayak UI
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Bevy Forks
Kayak UI
Commits
9ab0bdd4
Unverified
Commit
9ab0bdd4
authored
3 years ago
by
John
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #26 from MrGVSV/use-state
Added simple `use_state` proc_macro
parents
5e5c85b1
d1087782
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
kayak_render_macros/src/lib.rs
+49
-0
49 additions, 0 deletions
kayak_render_macros/src/lib.rs
with
49 additions
and
0 deletions
kayak_render_macros/src/lib.rs
+
49
−
0
View file @
9ab0bdd4
...
...
@@ -103,3 +103,52 @@ pub fn dyn_partial_eq(_: TokenStream, input: TokenStream) -> TokenStream {
})
.into
()
}
/// Create a state and its setter
///
/// # Arguments
///
/// * `initial_state`: The expression
///
/// returns: (state, set_state)
///
/// # Examples
///
/// ```
/// # use kayak_core::{EventType, OnEvent};
/// use kayak_render_macros::use_state;
///
/// let (count, set_count) = use_state!(0);
///
/// let on_event = OnEvent::new(move |_, event| match event.event_type {
/// EventType::Click => {
/// set_count(foo + 1);
/// }
/// _ => {}
/// });
///
/// rsx! {
/// <>
/// <Button on_event={Some(on_event)}>
/// <Text size={16.0} content={format!("Count: {}", count)}>{}</Text>
/// </Button>
/// </>
/// }
/// ```
#[proc_macro]
pub
fn
use_state
(
initial_state
:
TokenStream
)
->
TokenStream
{
let
initial_state
=
parse_macro_input!
(
initial_state
as
syn
::
Expr
);
let
result
=
quote!
{{
let
state
=
context
.create_state
(
#
initial_state
)
.unwrap
();
let
cloned_state
=
state
.clone
();
let
set_state
=
move
|
value
|
{
cloned_state
.set
(
value
);
};
let
state_value
=
state
.get
();
(
state
.get
(),
set_state
)
}};
TokenStream
::
from
(
result
)
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment