@@ -105,6 +105,10 @@ pub(crate) struct Options<A: AllowedOptions> {
105105 /// If this is `Some`, the value is the provided `heap_size` function.
106106 pub heap_size_fn : Option < syn:: Path > ,
107107
108+ /// The `self_ty = <Ty>` option is used to set the the self type of the tracked impl for tracked
109+ /// functions. This is merely used to refine the query name.
110+ pub self_ty : Option < syn:: Type > ,
111+
108112 /// Remember the `A` parameter, which plays no role after parsing.
109113 phantom : PhantomData < A > ,
110114}
@@ -130,6 +134,7 @@ impl<A: AllowedOptions> Default for Options<A> {
130134 id : Default :: default ( ) ,
131135 revisions : Default :: default ( ) ,
132136 heap_size_fn : Default :: default ( ) ,
137+ self_ty : Default :: default ( ) ,
133138 }
134139 }
135140}
@@ -153,6 +158,7 @@ pub(crate) trait AllowedOptions {
153158 const ID : bool ;
154159 const REVISIONS : bool ;
155160 const HEAP_SIZE : bool ;
161+ const SELF_TY : bool ;
156162}
157163
158164type Equals = syn:: Token ![ =] ;
@@ -416,6 +422,22 @@ impl<A: AllowedOptions> syn::parse::Parse for Options<A> {
416422 "`heap_size` option not allowed here" ,
417423 ) ) ;
418424 }
425+ } else if ident == "self_ty" {
426+ if A :: SELF_TY {
427+ let _eq = Equals :: parse ( input) ?;
428+ let ty = syn:: Type :: parse ( input) ?;
429+ if let Some ( old) = options. self_ty . replace ( ty) {
430+ return Err ( syn:: Error :: new (
431+ old. span ( ) ,
432+ "option `self_ty` provided twice" ,
433+ ) ) ;
434+ }
435+ } else {
436+ return Err ( syn:: Error :: new (
437+ ident. span ( ) ,
438+ "`self_ty` option not allowed here" ,
439+ ) ) ;
440+ }
419441 } else {
420442 return Err ( syn:: Error :: new (
421443 ident. span ( ) ,
@@ -433,3 +455,82 @@ impl<A: AllowedOptions> syn::parse::Parse for Options<A> {
433455 Ok ( options)
434456 }
435457}
458+ impl < A : AllowedOptions > quote:: ToTokens for Options < A > {
459+ fn to_tokens ( & self , tokens : & mut proc_macro2:: TokenStream ) {
460+ let Self {
461+ returns,
462+ no_eq,
463+ debug,
464+ no_lifetime,
465+ singleton,
466+ specify,
467+ non_update_return_type,
468+ db_path,
469+ cycle_fn,
470+ cycle_initial,
471+ cycle_result,
472+ data,
473+ lru,
474+ constructor_name,
475+ id,
476+ revisions,
477+ heap_size_fn,
478+ self_ty,
479+ phantom : _,
480+ } = self ;
481+ if let Some ( returns) = returns {
482+ tokens. extend ( quote:: quote! { returns( #returns) , } ) ;
483+ } ;
484+ if no_eq. is_some ( ) {
485+ tokens. extend ( quote:: quote! { no_eq, } ) ;
486+ }
487+ if debug. is_some ( ) {
488+ tokens. extend ( quote:: quote! { debug, } ) ;
489+ }
490+ if no_lifetime. is_some ( ) {
491+ tokens. extend ( quote:: quote! { no_lifetime, } ) ;
492+ }
493+ if singleton. is_some ( ) {
494+ tokens. extend ( quote:: quote! { singleton, } ) ;
495+ }
496+ if specify. is_some ( ) {
497+ tokens. extend ( quote:: quote! { specify, } ) ;
498+ }
499+ if non_update_return_type. is_some ( ) {
500+ tokens. extend ( quote:: quote! { unsafe ( non_update_return_type) , } ) ;
501+ }
502+ if let Some ( db_path) = db_path {
503+ tokens. extend ( quote:: quote! { db = #db_path, } ) ;
504+ }
505+ if let Some ( cycle_fn) = cycle_fn {
506+ tokens. extend ( quote:: quote! { cycle_fn = #cycle_fn, } ) ;
507+ }
508+ if let Some ( cycle_initial) = cycle_initial {
509+ tokens. extend ( quote:: quote! { cycle_initial = #cycle_initial, } ) ;
510+ }
511+ if let Some ( cycle_result) = cycle_result {
512+ tokens. extend ( quote:: quote! { cycle_result = #cycle_result, } ) ;
513+ }
514+ if let Some ( data) = data {
515+ tokens. extend ( quote:: quote! { data = #data, } ) ;
516+ }
517+ if let Some ( lru) = lru {
518+ tokens. extend ( quote:: quote! { lru = #lru, } ) ;
519+ }
520+ if let Some ( constructor_name) = constructor_name {
521+ tokens. extend ( quote:: quote! { constructor = #constructor_name, } ) ;
522+ }
523+ if let Some ( id) = id {
524+ tokens. extend ( quote:: quote! { id = #id, } ) ;
525+ }
526+ if let Some ( revisions) = revisions {
527+ tokens. extend ( quote:: quote! { revisions = #revisions, } ) ;
528+ }
529+ if let Some ( heap_size_fn) = heap_size_fn {
530+ tokens. extend ( quote:: quote! { heap_size_fn = #heap_size_fn, } ) ;
531+ }
532+ if let Some ( self_ty) = self_ty {
533+ tokens. extend ( quote:: quote! { self_ty = #self_ty, } ) ;
534+ }
535+ }
536+ }
0 commit comments