From c963dcf5510dae751cdee87bbdb3e5c300122be1 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Sat, 5 Feb 2022 20:12:28 +0100 Subject: [PATCH] use Clone()able backends only --- src/backend/decrypt.rs | 13 +++++++------ src/backend/local.rs | 1 + src/backend/mod.rs | 2 +- src/crypto/mod.rs | 1 + src/index/indexfiles.rs | 14 +++++++------- src/repo/index.rs | 12 ++++++++---- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/backend/decrypt.rs b/src/backend/decrypt.rs index f7fa0a9..dcf5eca 100644 --- a/src/backend/decrypt.rs +++ b/src/backend/decrypt.rs @@ -15,21 +15,22 @@ pub enum RepoError { RepoError(#[from] E), } -pub struct DecryptBackend<'a, R> { - backend: &'a R, +#[derive(Clone)] +pub struct DecryptBackend { + backend: R, key: Key, } -impl<'a, R: ReadBackend> DecryptBackend<'a, R> { - pub fn new(be: &'a R, key: Key) -> Self { +impl DecryptBackend { + pub fn new(be: &R, key: Key) -> Self { Self { - backend: be, + backend: be.clone(), key: key, } } } -impl<'a, R: ReadBackend> ReadBackend for DecryptBackend<'a, R> { +impl ReadBackend for DecryptBackend { type Error = RepoError; fn location(&self) -> &str { diff --git a/src/backend/local.rs b/src/backend/local.rs index 3126972..d8d7606 100644 --- a/src/backend/local.rs +++ b/src/backend/local.rs @@ -5,6 +5,7 @@ use walkdir::WalkDir; use super::{FileType, Id, ReadBackend}; +#[derive(Clone)] pub struct LocalBackend { path: PathBuf, } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 8631e1c..81a815e 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -27,7 +27,7 @@ impl FileType { } } -pub trait ReadBackend { +pub trait ReadBackend: Clone { type Error: Send + Sync + std::error::Error + 'static; fn location(&self) -> &str; diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 053da8a..a8f6da3 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -8,6 +8,7 @@ pub type CryptoError = aead::Error; type Nonce = aead::Nonce; type AeadKey = aead::Key; +#[derive(Clone)] pub struct Key(AeadKey); impl Key { diff --git a/src/index/indexfiles.rs b/src/index/indexfiles.rs index 0e024cc..b50675f 100644 --- a/src/index/indexfiles.rs +++ b/src/index/indexfiles.rs @@ -3,26 +3,26 @@ use crate::backend::{FileType, ReadBackend}; use crate::id::Id; use crate::repo::IndexFile; -pub struct AllIndexFiles<'a, BE> { - be: &'a BE, +pub struct AllIndexFiles { + be: BE, files: Vec, } -impl<'a, BE: ReadBackend> AllIndexFiles<'a, BE> { - pub fn new(be: &'a BE) -> Self { +impl AllIndexFiles { + pub fn new(be: &BE) -> Self { Self { - be: be, + be: be.clone(), files: be.list(FileType::Index).unwrap(), } } } -impl<'a, BE: ReadBackend> ReadIndex for AllIndexFiles<'a, BE> { +impl ReadIndex for AllIndexFiles { fn iter(&self) -> Box + '_> { Box::new( self.files .iter() - .flat_map(|id| IndexFile::from_backend(self.be, *id).unwrap().into_iter()), + .flat_map(|&id| IndexFile::from_backend(&self.be, id).unwrap().into_iter()), ) } } diff --git a/src/repo/index.rs b/src/repo/index.rs index 0f8f5b1..0d0bb2e 100644 --- a/src/repo/index.rs +++ b/src/repo/index.rs @@ -28,16 +28,20 @@ impl IntoIterator for IndexFile { fn into_iter(self) -> Self::IntoIter { Box::new(self.packs.into_iter().flat_map(|p| { - p.blobs.into_iter().map(move |b| IndexEntry::new(p.id,b.to_bi())) + p.blobs + .into_iter() + .map(move |b| IndexEntry::new(p.id, b.to_bi())) })) } } impl ReadIndex for IndexFile { fn iter(&self) -> Box + '_> { - Box::new(self.packs.iter().flat_map(|p| { - p.blobs.iter().map(|b| IndexEntry::new(p.id,b.to_bi())) - })) + Box::new( + self.packs + .iter() + .flat_map(|p| p.blobs.iter().map(|b| IndexEntry::new(p.id, b.to_bi()))), + ) } }