Skip to content

Commit

Permalink
Add blanket implementations for Repository traits
Browse files Browse the repository at this point in the history
Some of these traits are fully defined and can have blanket
implementations that reduce the amount of boilerplate needed.

Signed-off-by: J Robert Ray <[email protected]>
  • Loading branch information
jrray committed Nov 27, 2024
1 parent 80039ea commit 548f890
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 126 deletions.
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ pub trait BlobStorage: graph::Database + Sync + Send {
}
}

impl<T: BlobStorage> BlobStorage for &T {}
/// Blanket implementation.
impl<T> BlobStorage for T where T: graph::Database + Sync + Send {}
5 changes: 0 additions & 5 deletions crates/spfs/src/storage/fallback/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,6 @@ impl TagStorageMut for FallbackProxy {
}
}

impl BlobStorage for FallbackProxy {}
impl ManifestStorage for FallbackProxy {}
impl LayerStorage for FallbackProxy {}
impl PlatformStorage for FallbackProxy {}
impl Address for FallbackProxy {
fn address(&self) -> Cow<'_, url::Url> {
let config = Config {
Expand All @@ -383,7 +379,6 @@ impl Address for FallbackProxy {
)
}
}
impl Repository for FallbackProxy {}

impl LocalRepository for FallbackProxy {
#[inline]
Expand Down
10 changes: 0 additions & 10 deletions crates/spfs/src/storage/fs/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,11 @@ impl FsRepository {
}
}

impl BlobStorage for FsRepository {}
impl ManifestStorage for FsRepository {}
impl LayerStorage for FsRepository {}
impl PlatformStorage for FsRepository {}
impl Address for FsRepository {
fn address(&self) -> Cow<'_, url::Url> {
Cow::Owned(url::Url::from_directory_path(self.root()).unwrap())
}
}
impl Repository for FsRepository {}

impl std::fmt::Debug for FsRepository {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -535,16 +530,11 @@ impl OpenFsRepository {
}
}

impl BlobStorage for OpenFsRepository {}
impl ManifestStorage for OpenFsRepository {}
impl LayerStorage for OpenFsRepository {}
impl PlatformStorage for OpenFsRepository {}
impl Address for OpenFsRepository {
fn address(&self) -> Cow<'_, url::Url> {
Cow::Owned(url::Url::from_directory_path(self.root()).unwrap())
}
}
impl Repository for OpenFsRepository {}

impl std::fmt::Debug for OpenFsRepository {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
63 changes: 0 additions & 63 deletions crates/spfs/src/storage/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// https://github.com/spkenv/spk

use std::borrow::Cow;
use std::collections::HashSet;
use std::pin::Pin;
use std::sync::Arc;

Expand All @@ -13,7 +12,6 @@ use relative_path::RelativePath;
use spfs_encoding as encoding;

use super::prelude::*;
use super::repository::Ref;
use super::tag::TagSpecAndTagStream;
use super::{RepositoryHandle, TagNamespace, TagNamespaceBuf, TagStorageMut};
use crate::graph::ObjectProto;
Expand Down Expand Up @@ -42,29 +40,6 @@ impl Address for RepositoryHandle {
}
}

#[async_trait::async_trait]
impl Repository for RepositoryHandle {
async fn has_ref(&self, reference: &str) -> bool {
each_variant!(self, repo, { repo.has_ref(reference).await })
}

async fn resolve_ref(&self, reference: &str) -> Result<encoding::Digest> {
each_variant!(self, repo, { repo.resolve_ref(reference).await })
}

async fn read_ref(&self, reference: &str) -> Result<graph::Object> {
each_variant!(self, repo, { repo.read_ref(reference).await })
}

async fn find_aliases(&self, reference: &str) -> Result<HashSet<Ref>> {
each_variant!(self, repo, { repo.find_aliases(reference).await })
}

async fn commit_blob(&self, reader: Pin<Box<dyn BlobRead>>) -> Result<encoding::Digest> {
each_variant!(self, repo, { repo.commit_blob(reader).await })
}
}

#[async_trait::async_trait]
impl TagStorage for RepositoryHandle {
#[inline]
Expand Down Expand Up @@ -197,11 +172,6 @@ impl PayloadStorage for RepositoryHandle {
}
}

impl BlobStorage for RepositoryHandle {}
impl ManifestStorage for RepositoryHandle {}
impl LayerStorage for RepositoryHandle {}
impl PlatformStorage for RepositoryHandle {}

#[async_trait::async_trait]
impl DatabaseView for RepositoryHandle {
async fn has_object(&self, digest: encoding::Digest) -> bool {
Expand Down Expand Up @@ -256,34 +226,6 @@ impl Address for Arc<RepositoryHandle> {
}
}

#[async_trait::async_trait]
impl Repository for Arc<RepositoryHandle> {
/// Return true if this repository contains the given reference.
async fn has_ref(&self, reference: &str) -> bool {
each_variant!(&**self, repo, { repo.has_ref(reference).await })
}

/// Resolve a tag or digest string into its absolute digest.
async fn resolve_ref(&self, reference: &str) -> Result<encoding::Digest> {
each_variant!(&**self, repo, { repo.resolve_ref(reference).await })
}

/// Read an object of unknown type by tag or digest.
async fn read_ref(&self, reference: &str) -> Result<graph::Object> {
each_variant!(&**self, repo, { repo.read_ref(reference).await })
}

/// Return the other identifiers that can be used for 'reference'.
async fn find_aliases(&self, reference: &str) -> Result<HashSet<Ref>> {
each_variant!(&**self, repo, { repo.find_aliases(reference).await })
}

/// Commit the data from 'reader' as a blob in this repository
async fn commit_blob(&self, reader: Pin<Box<dyn BlobRead>>) -> Result<encoding::Digest> {
each_variant!(&**self, repo, { repo.commit_blob(reader).await })
}
}

#[async_trait::async_trait]
impl TagStorage for Arc<RepositoryHandle> {
#[inline]
Expand Down Expand Up @@ -402,11 +344,6 @@ impl PayloadStorage for Arc<RepositoryHandle> {
}
}

impl BlobStorage for Arc<RepositoryHandle> {}
impl ManifestStorage for Arc<RepositoryHandle> {}
impl LayerStorage for Arc<RepositoryHandle> {}
impl PlatformStorage for Arc<RepositoryHandle> {}

#[async_trait::async_trait]
impl DatabaseView for Arc<RepositoryHandle> {
async fn has_object(&self, digest: encoding::Digest) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ pub trait LayerStorage: graph::Database + Sync + Send {
}
}

impl<T: LayerStorage> LayerStorage for &T {}
/// Blanket implementation.
impl<T> LayerStorage for T where T: graph::Database + Sync + Send {}
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ pub trait ManifestStorage: graph::Database + Sync + Send {
}
}

impl<T: ManifestStorage> ManifestStorage for &T {}
/// Blanket implementation.
impl<T> ManifestStorage for T where T: graph::Database + Sync + Send {}
5 changes: 0 additions & 5 deletions crates/spfs/src/storage/pinned/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ where
}
}

impl<T> BlobStorage for PinnedRepository<T> where T: BlobStorage + 'static {}
impl<T> ManifestStorage for PinnedRepository<T> where T: ManifestStorage + 'static {}
impl<T> LayerStorage for PinnedRepository<T> where T: LayerStorage + 'static {}
impl<T> PlatformStorage for PinnedRepository<T> where T: PlatformStorage + 'static {}
impl<T> Address for PinnedRepository<T>
where
T: Repository + 'static,
Expand All @@ -165,7 +161,6 @@ where
Cow::Owned(base)
}
}
impl<T> Repository for PinnedRepository<T> where T: Repository + 'static {}

impl<T> std::fmt::Debug for PinnedRepository<T>
where
Expand Down
3 changes: 2 additions & 1 deletion crates/spfs/src/storage/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ pub trait PlatformStorage: graph::Database + Sync + Send {
}
}

impl<T: PlatformStorage> PlatformStorage for &T {}
/// Blanket implementation.
impl<T> PlatformStorage for T where T: graph::Database + Sync + Send {}
5 changes: 0 additions & 5 deletions crates/spfs/src/storage/proxy/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,6 @@ impl TagStorageMut for ProxyRepository {
}
}

impl BlobStorage for ProxyRepository {}
impl ManifestStorage for ProxyRepository {}
impl LayerStorage for ProxyRepository {}
impl PlatformStorage for ProxyRepository {}
impl Address for ProxyRepository {
fn address(&self) -> Cow<'_, url::Url> {
let config = Config {
Expand All @@ -348,4 +344,3 @@ impl Address for ProxyRepository {
Cow::Owned(config.to_address().expect("config creates a valid url"))
}
}
impl Repository for ProxyRepository {}
36 changes: 15 additions & 21 deletions crates/spfs/src/storage/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,21 @@ pub trait Repository:
}
}

#[async_trait::async_trait]
impl<T: Repository> Repository for &T {
async fn has_ref(&self, reference: &str) -> bool {
(**self).has_ref(reference).await
}

async fn resolve_ref(&self, reference: &str) -> Result<encoding::Digest> {
(**self).resolve_ref(reference).await
}

async fn read_ref(&self, reference: &str) -> Result<graph::Object> {
(**self).read_ref(reference).await
}

async fn find_aliases(&self, reference: &str) -> Result<HashSet<Ref>> {
(**self).find_aliases(reference).await
}

async fn commit_blob(&self, reader: Pin<Box<dyn BlobRead>>) -> Result<encoding::Digest> {
(**self).commit_blob(reader).await
}
/// Blanket implementation.
impl<T> Repository for T where
T: super::Address
+ super::TagStorage
+ super::PayloadStorage
+ super::ManifestStorage
+ super::BlobStorage
+ super::LayerStorage
+ super::PlatformStorage
+ graph::Database
+ graph::DatabaseView
+ std::fmt::Debug
+ Send
+ Sync
{
}

/// Accessor methods for types only applicable to repositories that have
Expand Down
7 changes: 1 addition & 6 deletions crates/spfs/src/storage/rpc/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::{Stream, TryStreamExt};
use proto::RpcResult;

use crate::graph::{self, ObjectProto};
use crate::{encoding, proto, storage, Result};
use crate::{encoding, proto, Result};

#[async_trait::async_trait]
impl graph::DatabaseView for super::RpcRepository {
Expand Down Expand Up @@ -112,8 +112,3 @@ impl graph::Database for super::RpcRepository {
.to_result()?)
}
}

impl storage::PlatformStorage for super::RpcRepository {}
impl storage::LayerStorage for super::RpcRepository {}
impl storage::ManifestStorage for super::RpcRepository {}
impl storage::BlobStorage for super::RpcRepository {}
2 changes: 0 additions & 2 deletions crates/spfs/src/storage/rpc/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,3 @@ impl storage::Address for RpcRepository {
Cow::Borrowed(&self.address)
}
}

impl storage::Repository for RpcRepository {}
5 changes: 0 additions & 5 deletions crates/spfs/src/storage/tar/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,10 @@ impl TagStorageMut for TarRepository {
}
}

impl BlobStorage for TarRepository {}
impl ManifestStorage for TarRepository {}
impl LayerStorage for TarRepository {}
impl PlatformStorage for TarRepository {}
impl Address for TarRepository {
fn address(&self) -> Cow<'_, url::Url> {
Cow::Owned(
url::Url::from_file_path(&self.repo_dir).expect("unexpected failure creating url"),
)
}
}
impl Repository for TarRepository {}

0 comments on commit 548f890

Please sign in to comment.