@@ -550,38 +550,45 @@ impl BinContext {
550
550
}
551
551
}
552
552
553
- impl Context for BinContext {
554
- fn build_queue ( & self ) -> Result < Arc < BuildQueue > , Error > {
555
- Ok ( self
556
- . build_queue
557
- . get_or_try_init :: < _ , Error > ( || {
558
- Ok ( Arc :: new ( BuildQueue :: new (
559
- self . pool ( ) ?,
560
- self . metrics ( ) ?,
561
- & * self . config ( ) ?,
562
- ) ) )
563
- } ) ?
564
- . clone ( ) )
565
- }
566
-
567
- fn storage ( & self ) -> Result < Arc < Storage > , Error > {
568
- Ok ( self
569
- . storage
570
- . get_or_try_init :: < _ , Error > ( || {
571
- Ok ( Arc :: new ( Storage :: new (
572
- self . pool ( ) ?,
573
- self . metrics ( ) ?,
574
- & * self . config ( ) ?,
575
- ) ?) )
576
- } ) ?
577
- . clone ( ) )
553
+ macro_rules! lazy {
554
+ ( $( fn $name: ident( $self: ident) -> $type: ty = $init: expr) ;+ $( ; ) ? ) => {
555
+ $( fn $name( & $self) -> Result <Arc <$type>, Error > {
556
+ Ok ( $self
557
+ . $name
558
+ . get_or_try_init:: <_, Error >( || Ok ( Arc :: new( $init) ) ) ?
559
+ . clone( ) )
560
+ } ) *
578
561
}
562
+ }
579
563
580
- fn config ( & self ) -> Result < Arc < Config > , Error > {
581
- Ok ( self
582
- . config
583
- . get_or_try_init :: < _ , Error > ( || Ok ( Arc :: new ( Config :: from_env ( ) ?) ) ) ?
584
- . clone ( ) )
564
+ impl Context for BinContext {
565
+ lazy ! {
566
+ fn build_queue( self ) -> BuildQueue = BuildQueue :: new(
567
+ self . pool( ) ?,
568
+ self . metrics( ) ?,
569
+ & * self . config( ) ?,
570
+ ) ;
571
+ fn storage( self ) -> Storage = Storage :: new(
572
+ self . pool( ) ?,
573
+ self . metrics( ) ?,
574
+ & * self . config( ) ?,
575
+ ) ?;
576
+ fn config( self ) -> Config = Config :: from_env( ) ?;
577
+ fn metrics( self ) -> Metrics = Metrics :: new( ) ?;
578
+ fn index( self ) -> Index = {
579
+ let config = self . config( ) ?;
580
+ let path = config. registry_index_path. clone( ) ;
581
+ if let Some ( registry_url) = config. registry_url. clone( ) {
582
+ Index :: from_url( path, registry_url)
583
+ } else {
584
+ Index :: new( path)
585
+ } ?
586
+ } ;
587
+ fn repository_stats_updater( self ) -> RepositoryStatsUpdater = {
588
+ let config = self . config( ) ?;
589
+ let pool = self . pool( ) ?;
590
+ RepositoryStatsUpdater :: new( & config, pool)
591
+ } ;
585
592
}
586
593
587
594
fn pool ( & self ) -> Result < Pool , Error > {
@@ -590,38 +597,4 @@ impl Context for BinContext {
590
597
. get_or_try_init :: < _ , Error > ( || Ok ( Pool :: new ( & * self . config ( ) ?, self . metrics ( ) ?) ?) ) ?
591
598
. clone ( ) )
592
599
}
593
-
594
- fn metrics ( & self ) -> Result < Arc < Metrics > , Error > {
595
- Ok ( self
596
- . metrics
597
- . get_or_try_init :: < _ , Error > ( || Ok ( Arc :: new ( Metrics :: new ( ) ?) ) ) ?
598
- . clone ( ) )
599
- }
600
-
601
- fn index ( & self ) -> Result < Arc < Index > , Error > {
602
- Ok ( self
603
- . index
604
- . get_or_try_init :: < _ , Error > ( || {
605
- let config = self . config ( ) ?;
606
- Ok ( Arc :: new (
607
- if let Some ( registry_url) = config. registry_url . clone ( ) {
608
- Index :: from_url ( config. registry_index_path . clone ( ) , registry_url)
609
- } else {
610
- Index :: new ( config. registry_index_path . clone ( ) )
611
- } ?,
612
- ) )
613
- } ) ?
614
- . clone ( ) )
615
- }
616
-
617
- fn repository_stats_updater ( & self ) -> Result < Arc < RepositoryStatsUpdater > , Error > {
618
- Ok ( self
619
- . repository_stats_updater
620
- . get_or_try_init :: < _ , Error > ( || {
621
- let config = self . config ( ) ?;
622
- let pool = self . pool ( ) ?;
623
- Ok ( Arc :: new ( RepositoryStatsUpdater :: new ( & config, pool) ) )
624
- } ) ?
625
- . clone ( ) )
626
- }
627
600
}
0 commit comments