-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Function Components & Hooks V2 #2401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
c1ed876
Make a use_hook hook with the new Hook trait.
futursolo 863fa92
Merge branch 'master' into fc-hook-v2
futursolo 22d91f6
Implement Lifetime.
futursolo d940a7d
Rewrites function signature.
futursolo 572becb
Only apply lifetime if there're other lifetimes.
futursolo 7dc8373
Merge branch 'master' into fc-hook-v2
futursolo 7e8bcdd
Cleanup signature rewrite logic.
futursolo 2664141
Rewrite hook body.
futursolo aac78b1
Port some built-in hooks.
futursolo 1e5c373
Finish porting all built-in hooks.
futursolo 5f086e8
Port tests.
futursolo a87b2c7
Fix tests.
futursolo 62396f0
Migrate to macro-based hooks.
futursolo 72518e2
Fix HookContext, add tests on non-possible locations.
futursolo 38e06b1
Fix stderr for trybuild.
futursolo 19af0b3
Add 1 more test case.
futursolo 49454cd
Adjust doc location.
futursolo eb85f47
Pretty print hook signature.
futursolo 4f0260c
Fix Items & std::ops::Fn*.
futursolo 344ae10
Add use_memo.
futursolo ffca655
Optimise Implementation of hooks.
futursolo 7b8978b
Use Box to capture function value only.
futursolo 15585ff
Detect whether needs boxing.
futursolo 3dbcdac
Merge commit '38021e31fe97202b112c952c6f5fb5b06289fbc5' into fc-hook-v2
futursolo f07c648
Add args if boxing not needed.
futursolo 7118b19
Enforce hook number.
futursolo 171e085
Deduplicate use_effect.
futursolo 2f3856a
Optimise Implementation.
futursolo 2b1d6f1
Update documentation.
futursolo ac765cb
Fix website test. Strip BoxedHook implementation from it.
futursolo 9c300ed
Allow doc string.
futursolo 76da3f6
Workaround doc tests.
futursolo 541a945
Optimise codebase & documentation.
futursolo 80dc7e2
Fix website test.
futursolo 347623b
Reduce implementation complexity.
futursolo e974dff
Destructor is no more.
futursolo 7160c18
Documentation and macros.
futursolo aa2e803
Reduce heap allocation and hook complexity.
futursolo 1799102
Remove Queue as well.
futursolo acc87d1
Prefer Generics.
futursolo 00a653a
Fix typo.
futursolo c861231
Remove more allocations.
futursolo e82b4ec
Add comments.
futursolo 935a672
Remove outdated comment.
futursolo 806f618
Bare Function Pointer for better code size.
futursolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| use proc_macro2::Span; | ||
| use proc_macro_error::emit_error; | ||
| use std::sync::{Arc, Mutex}; | ||
| use syn::spanned::Spanned; | ||
| use syn::visit_mut::VisitMut; | ||
| use syn::{ | ||
| parse_quote_spanned, visit_mut, Expr, ExprCall, ExprClosure, ExprForLoop, ExprIf, ExprLoop, | ||
| ExprMatch, ExprWhile, Ident, Item, | ||
| }; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct BodyRewriter { | ||
| branch_lock: Arc<Mutex<()>>, | ||
| } | ||
|
|
||
| impl BodyRewriter { | ||
| fn is_branched(&self) -> bool { | ||
| self.branch_lock.try_lock().is_err() | ||
| } | ||
|
|
||
| fn with_branch<F, O>(&mut self, f: F) -> O | ||
| where | ||
| F: FnOnce(&mut BodyRewriter) -> O, | ||
| { | ||
| let branch_lock = self.branch_lock.clone(); | ||
| let _branched = branch_lock.try_lock(); | ||
| f(self) | ||
| } | ||
| } | ||
|
|
||
| impl VisitMut for BodyRewriter { | ||
| fn visit_expr_call_mut(&mut self, i: &mut ExprCall) { | ||
| let ctx_ident = Ident::new("ctx", Span::mixed_site()); | ||
|
|
||
| // Only rewrite hook calls. | ||
| if let Expr::Path(ref m) = &*i.func { | ||
| if let Some(m) = m.path.segments.last().as_ref().map(|m| &m.ident) { | ||
| if m.to_string().starts_with("use_") { | ||
| if self.is_branched() { | ||
| emit_error!( | ||
| m, | ||
| "hooks cannot be called at this position."; | ||
| help = "move hooks to the top-level of your function."; | ||
| note = "see: https://yew.rs/docs/next/concepts/function-components/introduction#hooks" | ||
| ); | ||
| } else { | ||
| *i = parse_quote_spanned! { i.span() => ::yew::functional::Hook::run(#i, #ctx_ident) }; | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| visit_mut::visit_expr_call_mut(self, i); | ||
| } | ||
|
|
||
| fn visit_expr_closure_mut(&mut self, i: &mut ExprClosure) { | ||
| self.with_branch(move |m| visit_mut::visit_expr_closure_mut(m, i)) | ||
| } | ||
|
|
||
| fn visit_expr_if_mut(&mut self, i: &mut ExprIf) { | ||
| for it in &mut i.attrs { | ||
| visit_mut::visit_attribute_mut(self, it); | ||
| } | ||
|
|
||
| visit_mut::visit_expr_mut(self, &mut *i.cond); | ||
|
|
||
| self.with_branch(|m| visit_mut::visit_block_mut(m, &mut i.then_branch)); | ||
|
|
||
| if let Some(it) = &mut i.else_branch { | ||
| self.with_branch(|m| visit_mut::visit_expr_mut(m, &mut *(it).1)); | ||
| } | ||
| } | ||
|
|
||
| fn visit_expr_loop_mut(&mut self, i: &mut ExprLoop) { | ||
| self.with_branch(|m| visit_mut::visit_expr_loop_mut(m, i)); | ||
| } | ||
|
|
||
| fn visit_expr_for_loop_mut(&mut self, i: &mut ExprForLoop) { | ||
| for it in &mut i.attrs { | ||
| visit_mut::visit_attribute_mut(self, it); | ||
| } | ||
| if let Some(it) = &mut i.label { | ||
| visit_mut::visit_label_mut(self, it); | ||
| } | ||
| visit_mut::visit_pat_mut(self, &mut i.pat); | ||
| visit_mut::visit_expr_mut(self, &mut *i.expr); | ||
|
|
||
| self.with_branch(|m| visit_mut::visit_block_mut(m, &mut i.body)); | ||
| } | ||
|
|
||
| fn visit_expr_match_mut(&mut self, i: &mut ExprMatch) { | ||
| for it in &mut i.attrs { | ||
| visit_mut::visit_attribute_mut(self, it); | ||
| } | ||
|
|
||
| visit_mut::visit_expr_mut(self, &mut *i.expr); | ||
|
|
||
| self.with_branch(|m| { | ||
| for it in &mut i.arms { | ||
| visit_mut::visit_arm_mut(m, it); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| fn visit_expr_while_mut(&mut self, i: &mut ExprWhile) { | ||
| for it in &mut i.attrs { | ||
| visit_mut::visit_attribute_mut(self, it); | ||
| } | ||
| if let Some(it) = &mut i.label { | ||
| visit_mut::visit_label_mut(self, it); | ||
| } | ||
|
|
||
| self.with_branch(|m| visit_mut::visit_expr_mut(m, &mut i.cond)); | ||
| self.with_branch(|m| visit_mut::visit_block_mut(m, &mut i.body)); | ||
| } | ||
|
|
||
| fn visit_item_mut(&mut self, _i: &mut Item) { | ||
| // We don't do anything for items. | ||
| // for components / hooks in other components / hooks, apply the attribute again. | ||
| } | ||
| } |
This file contains hidden or 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,121 @@ | ||
| use proc_macro2::Span; | ||
| use std::sync::{Arc, Mutex}; | ||
| use syn::visit_mut::{self, VisitMut}; | ||
| use syn::{ | ||
| GenericArgument, Lifetime, ParenthesizedGenericArguments, Receiver, TypeBareFn, TypeImplTrait, | ||
| TypeParamBound, TypeReference, | ||
| }; | ||
|
|
||
| // borrowed from the awesome async-trait crate. | ||
| pub struct CollectLifetimes { | ||
| pub elided: Vec<Lifetime>, | ||
| pub explicit: Vec<Lifetime>, | ||
| pub name: &'static str, | ||
| pub default_span: Span, | ||
|
|
||
| pub impl_trait_lock: Arc<Mutex<()>>, | ||
| pub impl_fn_lock: Arc<Mutex<()>>, | ||
| } | ||
|
|
||
| impl CollectLifetimes { | ||
| pub fn new(name: &'static str, default_span: Span) -> Self { | ||
| CollectLifetimes { | ||
| elided: Vec::new(), | ||
| explicit: Vec::new(), | ||
| name, | ||
| default_span, | ||
|
|
||
| impl_trait_lock: Arc::default(), | ||
| impl_fn_lock: Arc::default(), | ||
| } | ||
| } | ||
|
|
||
| fn is_impl_trait(&self) -> bool { | ||
| self.impl_trait_lock.try_lock().is_err() | ||
| } | ||
|
|
||
| fn is_impl_fn(&self) -> bool { | ||
| self.impl_fn_lock.try_lock().is_err() | ||
| } | ||
|
|
||
| fn visit_opt_lifetime(&mut self, lifetime: &mut Option<Lifetime>) { | ||
| match lifetime { | ||
| None => *lifetime = Some(self.next_lifetime(None)), | ||
| Some(lifetime) => self.visit_lifetime(lifetime), | ||
| } | ||
| } | ||
|
|
||
| fn visit_lifetime(&mut self, lifetime: &mut Lifetime) { | ||
| if lifetime.ident == "_" { | ||
| *lifetime = self.next_lifetime(lifetime.span()); | ||
| } else { | ||
| self.explicit.push(lifetime.clone()); | ||
| } | ||
| } | ||
|
|
||
| fn next_lifetime<S: Into<Option<Span>>>(&mut self, span: S) -> Lifetime { | ||
| let name = format!("{}{}", self.name, self.elided.len()); | ||
| let span = span.into().unwrap_or(self.default_span); | ||
| let life = Lifetime::new(&name, span); | ||
| self.elided.push(life.clone()); | ||
| life | ||
| } | ||
| } | ||
|
|
||
| impl VisitMut for CollectLifetimes { | ||
| fn visit_receiver_mut(&mut self, arg: &mut Receiver) { | ||
| if let Some((_, lifetime)) = &mut arg.reference { | ||
| self.visit_opt_lifetime(lifetime); | ||
| } | ||
| } | ||
|
|
||
| fn visit_type_reference_mut(&mut self, ty: &mut TypeReference) { | ||
| // We don't rewrite references in the impl FnOnce(&arg) or fn(&arg) | ||
| if self.is_impl_fn() { | ||
| return; | ||
| } | ||
|
|
||
| self.visit_opt_lifetime(&mut ty.lifetime); | ||
| visit_mut::visit_type_reference_mut(self, ty); | ||
| } | ||
|
|
||
| fn visit_generic_argument_mut(&mut self, gen: &mut GenericArgument) { | ||
| // We don't rewrite types in the impl FnOnce(&arg) -> Type<'_> | ||
| if self.is_impl_fn() { | ||
| return; | ||
| } | ||
|
|
||
| if let GenericArgument::Lifetime(lifetime) = gen { | ||
| self.visit_lifetime(lifetime); | ||
| } | ||
| visit_mut::visit_generic_argument_mut(self, gen); | ||
| } | ||
|
|
||
| fn visit_type_impl_trait_mut(&mut self, impl_trait: &mut TypeImplTrait) { | ||
| let impl_trait_lock = self.impl_trait_lock.clone(); | ||
| let _locked = impl_trait_lock.try_lock(); | ||
|
|
||
| impl_trait | ||
| .bounds | ||
| .insert(0, TypeParamBound::Lifetime(self.next_lifetime(None))); | ||
|
|
||
| visit_mut::visit_type_impl_trait_mut(self, impl_trait); | ||
| } | ||
|
|
||
| fn visit_parenthesized_generic_arguments_mut( | ||
| &mut self, | ||
| generic_args: &mut ParenthesizedGenericArguments, | ||
| ) { | ||
| let impl_fn_lock = self.impl_fn_lock.clone(); | ||
| let _maybe_locked = self.is_impl_trait().then(|| impl_fn_lock.try_lock()); | ||
|
|
||
| visit_mut::visit_parenthesized_generic_arguments_mut(self, generic_args); | ||
| } | ||
|
|
||
| fn visit_type_bare_fn_mut(&mut self, i: &mut TypeBareFn) { | ||
| let impl_fn_lock = self.impl_fn_lock.clone(); | ||
| let _locked = impl_fn_lock.try_lock(); | ||
|
|
||
| visit_mut::visit_type_bare_fn_mut(self, i); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.