@@ -272,93 +272,9 @@ pub fn props(_attr: TokenStream, item: TokenStream) -> TokenStream {
272
272
quote ! ( #props) . into ( )
273
273
}
274
274
275
- struct ParsedContext {
276
- context : ItemStruct ,
277
- }
278
-
279
- impl Parse for ParsedContext {
280
- fn parse ( input : ParseStream ) -> Result < Self > {
281
- let context: ItemStruct = input. parse ( ) ?;
282
- Ok ( Self { context } )
283
- }
284
- }
285
-
286
- impl ToTokens for ParsedContext {
287
- fn to_tokens ( & self , tokens : & mut proc_macro2:: TokenStream ) {
288
- let context = & self . context ;
289
- let name = & context. ident ;
290
- let generics = & context. generics ;
291
- let lifetime = generics. params . first ( ) ;
292
-
293
- let ref_fields = context. fields . iter ( ) . map ( |field| {
294
- let field_name = & field. ident ;
295
- let field_type = & field. ty ;
296
- quote ! { #field_name: <#field_type as :: iocraft:: ContextRef <#lifetime>>:: RefOwner <#lifetime> }
297
- } ) ;
298
-
299
- let ref_field_assignments = context. fields . iter ( ) . map ( |field| {
300
- let field_name = & field. ident ;
301
- let field_type = & field. ty ;
302
- quote ! { #field_name: <#field_type as :: iocraft:: ContextRef >:: get_from_component_updater( updater) }
303
- } ) ;
304
-
305
- let ref_field_borrows = context. fields . iter ( ) . map ( |field| {
306
- let field_name = & field. ident ;
307
- let field_type = & field. ty ;
308
- quote ! { #field_name: <#field_type as :: iocraft:: ContextRef >:: borrow( & mut refs. #field_name) }
309
- } ) ;
310
-
311
- tokens. extend ( quote ! {
312
- #context
313
-
314
- const _: ( ) = {
315
- pub struct ContextRefs #generics {
316
- #( #ref_fields, ) *
317
- }
318
-
319
- impl <' iocraft_lta> ContextRefs <' iocraft_lta> {
320
- fn refs_from_component_updater<#lifetime: ' iocraft_lta>( updater: & #lifetime :: iocraft:: ComponentUpdater ) -> ContextRefs <#lifetime> {
321
- ContextRefs {
322
- #( #ref_field_assignments, ) *
323
- }
324
- }
325
- }
326
-
327
- impl <#lifetime> ContextRefs #generics {
328
- fn borrow_refs<' iocraft_ltb: #lifetime, ' iocraft_ltc: ' iocraft_ltb>( refs: & ' iocraft_ltb mut ContextRefs <' iocraft_ltc>) -> #name<#lifetime> {
329
- #name {
330
- #( #ref_field_borrows, ) *
331
- }
332
- }
333
- }
334
-
335
- impl <' a> :: iocraft:: ContextImplExt <' a> for #name<' a> {
336
- type Refs <' b: ' a> = ContextRefs <' b>;
337
-
338
- fn refs_from_component_updater<' b: ' a>( updater: & ' b :: iocraft:: ComponentUpdater ) -> Self :: Refs <' b> {
339
- ContextRefs :: refs_from_component_updater( updater)
340
- }
341
-
342
- fn borrow_refs<' b: ' a, ' c: ' b>( refs: & ' b mut Self :: Refs <' c>) -> Self {
343
- ContextRefs :: borrow_refs( refs)
344
- }
345
- }
346
- } ;
347
- } ) ;
348
- }
349
- }
350
-
351
- /// Defines a struct containing context references to be made available to components.
352
- #[ proc_macro_attribute]
353
- pub fn context ( _attr : TokenStream , item : TokenStream ) -> TokenStream {
354
- let context = parse_macro_input ! ( item as ParsedContext ) ;
355
- quote ! ( #context) . into ( )
356
- }
357
-
358
275
struct ParsedComponent {
359
276
f : ItemFn ,
360
277
props_type : Option < Box < Type > > ,
361
- context_type : Option < Box < Type > > ,
362
278
impl_args : Vec < proc_macro2:: TokenStream > ,
363
279
}
364
280
@@ -367,7 +283,6 @@ impl Parse for ParsedComponent {
367
283
let f: ItemFn = input. parse ( ) ?;
368
284
369
285
let mut props_type = None ;
370
- let mut context_type = None ;
371
286
let mut impl_args = Vec :: new ( ) ;
372
287
373
288
for arg in & f. sig . inputs {
@@ -400,23 +315,6 @@ impl Parse for ParsedComponent {
400
315
}
401
316
_ => return Err ( Error :: new ( arg. ty . span ( ) , "invalid `hooks` type" ) ) ,
402
317
} ,
403
- "context" | "_context" => {
404
- if context_type. is_some ( ) {
405
- return Err ( Error :: new ( arg. span ( ) , "duplicate `context` argument" ) ) ;
406
- }
407
- match & * arg. ty {
408
- Type :: Path ( _) => {
409
- context_type = Some ( arg. ty . clone ( ) ) ;
410
- impl_args. push ( {
411
- let type_name = & arg. ty ;
412
- quote ! ( #type_name:: borrow_refs( & mut context_refs) )
413
- } ) ;
414
- }
415
- _ => {
416
- return Err ( Error :: new ( arg. ty . span ( ) , "invalid `context` type" ) )
417
- }
418
- }
419
- }
420
318
_ => return Err ( Error :: new ( arg. span ( ) , "invalid argument" ) ) ,
421
319
}
422
320
}
@@ -427,7 +325,6 @@ impl Parse for ParsedComponent {
427
325
Ok ( Self {
428
326
f,
429
327
props_type,
430
- context_type,
431
328
impl_args,
432
329
} )
433
330
}
@@ -449,12 +346,6 @@ impl ToTokens for ParsedComponent {
449
346
. map ( |ty| quote ! ( #ty) )
450
347
. unwrap_or_else ( || quote ! ( :: iocraft:: NoProps ) ) ;
451
348
452
- let context_refs = self . context_type . as_ref ( ) . map ( |ty| {
453
- quote ! {
454
- let mut context_refs = #ty:: refs_from_component_updater( updater) ;
455
- }
456
- } ) ;
457
-
458
349
tokens. extend ( quote ! {
459
350
#vis struct #name;
460
351
@@ -469,9 +360,9 @@ impl ToTokens for ParsedComponent {
469
360
Self
470
361
}
471
362
472
- fn update( & mut self , props: & mut Self :: Props <' _>, hooks: :: iocraft:: Hooks , updater: & mut :: iocraft:: ComponentUpdater ) {
363
+ fn update( & mut self , props: & mut Self :: Props <' _>, mut hooks: :: iocraft:: Hooks , updater: & mut :: iocraft:: ComponentUpdater ) {
473
364
let mut e = {
474
- #context_refs
365
+ let hooks = hooks . with_context_stack ( updater . component_context_stack ( ) ) ;
475
366
Self :: implementation( #( #impl_args) , * ) . into( )
476
367
} ;
477
368
updater. update_children( [ & mut e] , None ) ;
0 commit comments