From 9c223450199b466c535f2b715ad68f1f295fa7dc Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Fri, 18 Oct 2024 14:41:14 +0200 Subject: refactor(tvix/[ca]store): use auto_impl This implements BS, DS, PS for Box'ed or Arc'ed variants of it with less code, and less potential to accidentially forget to proxy default trait methods for blanked impls, as fixed in cl/12658. Change-Id: If2cdbb563a73792038ebe7bff45d6f880214855b Reviewed-on: https://cl.tvl.fyi/c/depot/+/12661 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: edef --- tvix/store/src/pathinfoservice/combinators.rs | 4 ++-- tvix/store/src/pathinfoservice/fs/mod.rs | 11 +++++------ tvix/store/src/pathinfoservice/mod.rs | 24 ++--------------------- tvix/store/src/pathinfoservice/nix_http.rs | 10 +++++----- tvix/store/src/pathinfoservice/signing_wrapper.rs | 2 +- 5 files changed, 15 insertions(+), 36 deletions(-) (limited to 'tvix/store/src/pathinfoservice') diff --git a/tvix/store/src/pathinfoservice/combinators.rs b/tvix/store/src/pathinfoservice/combinators.rs index 1f413cf310a2..f386fd52dc3c 100644 --- a/tvix/store/src/pathinfoservice/combinators.rs +++ b/tvix/store/src/pathinfoservice/combinators.rs @@ -88,8 +88,8 @@ impl ServiceBuilder for CacheConfig { context: &CompositionContext, ) -> Result, Box> { let (near, far) = futures::join!( - context.resolve(self.near.clone()), - context.resolve(self.far.clone()) + context.resolve::(self.near.clone()), + context.resolve::(self.far.clone()) ); Ok(Arc::new(Cache { near: near?, diff --git a/tvix/store/src/pathinfoservice/fs/mod.rs b/tvix/store/src/pathinfoservice/fs/mod.rs index d996ec9f6f76..ea30a2f6477c 100644 --- a/tvix/store/src/pathinfoservice/fs/mod.rs +++ b/tvix/store/src/pathinfoservice/fs/mod.rs @@ -20,9 +20,9 @@ pub fn make_fs( show_xattr: bool, ) -> TvixStoreFs> where - BS: AsRef + Send + Clone + 'static, - DS: AsRef + Send + Clone + 'static, - PS: AsRef + Send + Sync + Clone + 'static, + BS: BlobService + Send + Clone + 'static, + DS: DirectoryService + Send + Clone + 'static, + PS: PathInfoService + Send + Sync + Clone + 'static, { TvixStoreFs::new( blob_service, @@ -46,7 +46,7 @@ pub struct RootNodesWrapper(pub(crate) T); #[async_trait] impl RootNodes for RootNodesWrapper where - T: AsRef + Send + Sync, + T: PathInfoService + Send + Sync, { async fn get_by_basename(&self, name: &PathComponent) -> Result, Error> { let Ok(store_path) = StorePathRef::from_bytes(name.as_ref()) else { @@ -55,14 +55,13 @@ where Ok(self .0 - .as_ref() .get(*store_path.digest()) .await? .map(|path_info| path_info.node)) } fn list(&self) -> BoxStream> { - Box::pin(self.0.as_ref().list().map(|result| { + Box::pin(self.0.list().map(|result| { result.map(|path_info| { let basename = path_info.store_path.to_string(); ( diff --git a/tvix/store/src/pathinfoservice/mod.rs b/tvix/store/src/pathinfoservice/mod.rs index a0c48f5cc9d5..d31a1e652e3b 100644 --- a/tvix/store/src/pathinfoservice/mod.rs +++ b/tvix/store/src/pathinfoservice/mod.rs @@ -13,6 +13,7 @@ mod fs; #[cfg(test)] mod tests; +use auto_impl::auto_impl; use futures::stream::BoxStream; use tonic::async_trait; use tvix_castore::composition::{Registry, ServiceBuilder}; @@ -45,6 +46,7 @@ pub use self::fs::make_fs; /// The base trait all PathInfo services need to implement. #[async_trait] +#[auto_impl(&, &mut, Arc, Box)] pub trait PathInfoService: Send + Sync { /// Retrieve a PathInfo message by the output digest. async fn get(&self, digest: [u8; 20]) -> Result, Error>; @@ -69,28 +71,6 @@ pub trait PathInfoService: Send + Sync { } } -#[async_trait] -impl PathInfoService for A -where - A: AsRef + Send + Sync + 'static, -{ - async fn get(&self, digest: [u8; 20]) -> Result, Error> { - self.as_ref().get(digest).await - } - - async fn put(&self, path_info: PathInfo) -> Result { - self.as_ref().put(path_info).await - } - - fn list(&self) -> BoxStream<'static, Result> { - self.as_ref().list() - } - - fn nar_calculation_service(&self) -> Option> { - self.as_ref().nar_calculation_service() - } -} - /// Registers the builtin PathInfoService implementations with the registry pub(crate) fn register_pathinfo_services(reg: &mut Registry) { reg.register::>, CachePathInfoServiceConfig>("cache"); diff --git a/tvix/store/src/pathinfoservice/nix_http.rs b/tvix/store/src/pathinfoservice/nix_http.rs index ed386f0e9d14..29e04aa45867 100644 --- a/tvix/store/src/pathinfoservice/nix_http.rs +++ b/tvix/store/src/pathinfoservice/nix_http.rs @@ -66,8 +66,8 @@ impl NixHTTPPathInfoService { #[async_trait] impl PathInfoService for NixHTTPPathInfoService where - BS: AsRef + Send + Sync + Clone + 'static, - DS: AsRef + Send + Sync + Clone + 'static, + BS: BlobService + Send + Sync + Clone + 'static, + DS: DirectoryService + Send + Sync + Clone + 'static, { #[instrument(skip_all, err, fields(path.digest=nixbase32::encode(&digest)))] async fn get(&self, digest: [u8; 20]) -> Result, Error> { @@ -316,10 +316,10 @@ impl ServiceBuilder for NixHTTPPathInfoServiceConfig { &'a self, _instance_name: &str, context: &CompositionContext, - ) -> Result, Box> { + ) -> Result, Box> { let (blob_service, directory_service) = futures::join!( - context.resolve(self.blob_service.clone()), - context.resolve(self.directory_service.clone()) + context.resolve::(self.blob_service.clone()), + context.resolve::(self.directory_service.clone()) ); let mut svc = NixHTTPPathInfoService::new( Url::parse(&self.base_url)?, diff --git a/tvix/store/src/pathinfoservice/signing_wrapper.rs b/tvix/store/src/pathinfoservice/signing_wrapper.rs index 4dff23722888..5898a5baa463 100644 --- a/tvix/store/src/pathinfoservice/signing_wrapper.rs +++ b/tvix/store/src/pathinfoservice/signing_wrapper.rs @@ -96,7 +96,7 @@ impl ServiceBuilder for KeyFileSigningPathInfoServiceConfig { _instance_name: &str, context: &CompositionContext, ) -> Result, Box> { - let inner = context.resolve(self.inner.clone()).await?; + let inner = context.resolve::(self.inner.clone()).await?; let signing_key = Arc::new( parse_keypair(tokio::fs::read_to_string(&self.keyfile).await?.trim()) .map_err(|e| Error::StorageError(e.to_string()))? -- cgit 1.4.1