From 7a9cb75e02624f8bfecb49ba94740d5282f5f01f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 1 Dec 2022 12:35:43 +0200 Subject: [PATCH] Replace dynamic dispatch with static dispatch --- libs/remote_storage/src/lib.rs | 18 ++++++++++-------- libs/remote_storage/src/local_fs.rs | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libs/remote_storage/src/lib.rs b/libs/remote_storage/src/lib.rs index 4bdd2b9608..0218fb464d 100644 --- a/libs/remote_storage/src/lib.rs +++ b/libs/remote_storage/src/lib.rs @@ -178,21 +178,23 @@ impl std::error::Error for DownloadError {} /// Every storage, currently supported. /// Serves as a simple way to pass around the [`RemoteStorage`] without dealing with generics. #[derive(Clone)] -pub struct GenericRemoteStorage(Arc); +pub enum GenericRemoteStorage { + LocalFs(LocalFs), + AwsS3(Arc), +} impl Deref for GenericRemoteStorage { type Target = dyn RemoteStorage; fn deref(&self) -> &Self::Target { - self.0.as_ref() + match self { + GenericRemoteStorage::LocalFs(local_fs) => local_fs, + GenericRemoteStorage::AwsS3(s3_bucket) => s3_bucket.as_ref(), + } } } impl GenericRemoteStorage { - pub fn new(storage: impl RemoteStorage) -> Self { - Self(Arc::new(storage)) - } - pub fn from_config( working_directory: PathBuf, storage_config: &RemoteStorageConfig, @@ -200,12 +202,12 @@ impl GenericRemoteStorage { Ok(match &storage_config.storage { RemoteStorageKind::LocalFs(root) => { info!("Using fs root '{}' as a remote storage", root.display()); - GenericRemoteStorage::new(LocalFs::new(root.clone(), working_directory)?) + GenericRemoteStorage::LocalFs(LocalFs::new(root.clone(), working_directory)?) } RemoteStorageKind::AwsS3(s3_config) => { info!("Using s3 bucket '{}' in region '{}' as a remote storage, prefix in bucket: '{:?}', bucket endpoint: '{:?}'", s3_config.bucket_name, s3_config.bucket_region, s3_config.prefix_in_bucket, s3_config.endpoint); - GenericRemoteStorage::new(S3Bucket::new(s3_config, working_directory)?) + GenericRemoteStorage::AwsS3(Arc::new(S3Bucket::new(s3_config, working_directory)?)) } }) } diff --git a/libs/remote_storage/src/local_fs.rs b/libs/remote_storage/src/local_fs.rs index 2f824cc453..363d47f38d 100644 --- a/libs/remote_storage/src/local_fs.rs +++ b/libs/remote_storage/src/local_fs.rs @@ -33,6 +33,7 @@ fn remote_object_id_from_path(path: &Path) -> anyhow::Result { )) } +#[derive(Debug, Clone)] pub struct LocalFs { working_directory: PathBuf, storage_root: PathBuf,