@@ -30,18 +30,11 @@ pub enum Error {
3030/// a backing store for an AddressCache.
3131
3232#[ async_trait]
33- pub trait AddressCacheBacking : Sync + Send {
33+ pub trait AddressCacheBacking : ' static + Sync + Send + std :: clone :: Clone {
3434 async fn read ( & self ) -> Result < Vec < u8 > , Error > ;
3535 async fn write ( & self , data : & [ u8 ] ) -> Result < ( ) , Error > ;
3636}
3737
38- // this should allow this to compile.
39- impl Debug for dyn AddressCacheBacking {
40- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
41- write ! ( f, "[some AddressCacheBacking]" )
42- }
43- }
44-
4538#[ derive( Clone ) ]
4639pub struct FileAddressCacheBacking {
4740 read_path : Option < Arc < Path > > ,
@@ -74,7 +67,7 @@ impl AddressCacheBacking for FileAddressCacheBacking {
7467
7568/// A DNS resolver which resolves using `AddressCache`.
7669#[ async_trait]
77- impl DnsResolver for AddressCache {
70+ impl < T : AddressCacheBacking > DnsResolver for AddressCache < T > {
7871 async fn resolve ( & self , host : String ) -> Result < Vec < SocketAddr > , io:: Error > {
7972 self . resolve_hostname ( & host)
8073 . await
@@ -84,69 +77,65 @@ impl DnsResolver for AddressCache {
8477}
8578
8679#[ derive( Clone , Debug ) ]
87- pub struct AddressCache {
80+ pub struct AddressCache < T >
81+ where
82+ T : Clone ,
83+ {
8884 hostname : String ,
8985 inner : Arc < Mutex < AddressCacheInner > > ,
90- backing : Arc < dyn AddressCacheBacking > ,
86+ backing : Arc < T > ,
9187}
9288
93- impl AddressCache {
94- /// Initialise cache using a hardcoded address and a Backing for writing to
95- pub fn new_with_address ( endpoint : & ApiEndpoint , backing : Arc < dyn AddressCacheBacking > ) -> Self {
96- Self :: new_inner ( endpoint. address ( ) , endpoint. host ( ) . to_owned ( ) , backing)
89+ impl AddressCache < FileAddressCacheBacking > {
90+ /// Initialize cache using `read_path`, and write changes to `write_path`.
91+ pub async fn from_file (
92+ read_path : & Path ,
93+ write_path : Option < Box < Path > > ,
94+ hostname : String ,
95+ ) -> Result < AddressCache < FileAddressCacheBacking > , Error > {
96+ log:: debug!( "Loading API addresses from {}" , read_path. display( ) ) ;
97+ let backing = FileAddressCacheBacking {
98+ read_path : Some ( Arc :: from ( read_path) ) ,
99+ write_path : write_path. map ( Arc :: from) ,
100+ } ;
101+ AddressCache :: from_backing ( hostname, Arc :: new ( backing) ) . await
97102 }
98103
99104 /// Initialize cache using the hardcoded address, and write changes to `write_path`.
100- pub fn new ( endpoint : & ApiEndpoint , write_path : Option < Box < Path > > ) -> AddressCache {
101- AddressCache :: new_with_address (
102- endpoint,
103- Arc :: new ( FileAddressCacheBacking {
104- read_path : None ,
105- write_path : write_path. map ( Arc :: from) ,
106- } ) ,
107- )
105+ pub fn new (
106+ endpoint : & ApiEndpoint ,
107+ write_path : Option < Box < Path > > ,
108+ ) -> AddressCache < FileAddressCacheBacking > {
109+ let backing = FileAddressCacheBacking {
110+ read_path : None ,
111+ write_path : write_path. map ( Arc :: from) ,
112+ } ;
113+ AddressCache :: new_with_address ( endpoint, Arc :: new ( backing) )
108114 }
115+ }
109116
110- pub async fn from_backing (
111- hostname : String ,
112- backing : Arc < dyn AddressCacheBacking > ,
113- ) -> Result < Self , Error > {
114- let address = read_address_backing ( & backing) . await ?;
117+ impl < T : AddressCacheBacking + std:: clone:: Clone > AddressCache < T > {
118+ /// Initialise cache using a hardcoded address and a Backing for writing to
119+ pub fn new_with_address ( endpoint : & ApiEndpoint , backing : Arc < T > ) -> Self {
120+ Self :: new_inner ( endpoint. address ( ) , endpoint. host ( ) . to_owned ( ) , backing)
121+ }
122+
123+ pub async fn from_backing ( hostname : String , backing : Arc < T > ) -> Result < Self , Error > {
124+ let address = read_address_backing ( backing. as_ref ( ) ) . await ?;
115125 Ok ( Self :: new_inner ( address, hostname, backing) )
116126 }
117127
118128 pub async fn from_backing_or (
119129 hostname : String ,
120- backing : Arc < dyn AddressCacheBacking > ,
130+ backing : Arc < T > ,
121131 endpoint : & ApiEndpoint ,
122132 ) -> Self {
123- Self :: from_backing ( hostname. clone ( ) , backing . clone ( ) )
133+ Self :: from_backing ( hostname. clone ( ) , Arc :: clone ( & backing ) )
124134 . await
125135 . unwrap_or ( Self :: new_inner ( endpoint. address ( ) , hostname, backing) )
126136 }
127137
128- /// Initialize cache using `read_path`, and write changes to `write_path`.
129- pub async fn from_file (
130- read_path : & Path ,
131- write_path : Option < Box < Path > > ,
132- hostname : String ,
133- ) -> Result < AddressCache , Error > {
134- log:: debug!( "Loading API addresses from {}" , read_path. display( ) ) ;
135- AddressCache :: from_backing (
136- hostname,
137- Arc :: new ( FileAddressCacheBacking {
138- read_path : Some ( Arc :: from ( read_path) ) ,
139- write_path : write_path. map ( Arc :: from) ,
140- } ) ,
141- )
142- . await
143- }
144-
145- fn new_inner (
146- address : SocketAddr ,
147- hostname : String ,
148- backing : Arc < dyn AddressCacheBacking > ,
149- ) -> Self {
138+ fn new_inner ( address : SocketAddr , hostname : String , backing : Arc < T > ) -> Self {
150139 let cache = AddressCacheInner :: from_address ( address) ;
151140 log:: debug!( "Using API address: {}" , cache. address) ;
152141
@@ -201,9 +190,7 @@ impl AddressCacheInner {
201190 }
202191}
203192
204- async fn read_address_backing (
205- backing : & Arc < dyn AddressCacheBacking + ' static > ,
206- ) -> Result < SocketAddr , Error > {
193+ async fn read_address_backing ( backing : & impl AddressCacheBacking ) -> Result < SocketAddr , Error > {
207194 backing
208195 . read ( )
209196 . await
0 commit comments