Skip to content

Commit

Permalink
fmt+clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Sep 16, 2024
1 parent 3b499ae commit acf71a1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 121 deletions.
24 changes: 11 additions & 13 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ use thiserror::Error;
use tracing::{debug, error, instrument};
use write::CryptoInnerWriter;

use crate::crypto::read::{CryptoRead, CryptoReadSeek, CryptoReadSeekSendSync, CryptoReadSendSyncImpl, CryptoReadSendSync, RingCryptoRead, CryptoReadSeekSendSyncImpl};
use crate::crypto::write::{CryptoWrite, CryptoWriteSeek, CryptoWriteSeekSendSync, CryptoWriteSeekSendSyncImpl, CryptoWriteSendSync, CryptoWriteSendSyncImpl, RingCryptoWrite};
use crate::crypto::read::{
CryptoRead, CryptoReadSeek, CryptoReadSeekSendSync, CryptoReadSeekSendSyncImpl,
CryptoReadSendSync, CryptoReadSendSyncImpl, RingCryptoRead,
};
use crate::crypto::write::{
CryptoWrite, CryptoWriteSeek, CryptoWriteSeekSendSync, CryptoWriteSeekSendSyncImpl,
CryptoWriteSendSync, CryptoWriteSendSyncImpl, RingCryptoWrite,
};
use crate::encryptedfs::FsResult;
use crate::{fs_util, stream_util};

Expand Down Expand Up @@ -169,11 +175,7 @@ fn create_ring_write_seek<W: CryptoInnerWriter + Seek + Read + Send + Sync>(
RingCryptoWrite::new(writer, true, algorithm, key)
}

fn create_ring_read<R: Read>(
reader: R,
cipher: Cipher,
key: &SecretVec<u8>,
) -> RingCryptoRead<R> {
fn create_ring_read<R: Read>(reader: R, cipher: Cipher, key: &SecretVec<u8>) -> RingCryptoRead<R> {
let algorithm = match cipher {
Cipher::ChaCha20Poly1305 => &CHACHA20_POLY1305,
Cipher::Aes256Gcm => &AES_256_GCM,
Expand All @@ -196,11 +198,7 @@ fn create_ring_read_seek<R: Read + Seek>(
/// Creates a crypto reader. This is not thread-safe.
///
/// Use [`create_read_send_sync`] if you need thread-safe access.
pub fn create_read<R: Read>(
reader: R,
cipher: Cipher,
key: &SecretVec<u8>,
) -> impl CryptoRead<R> {
pub fn create_read<R: Read>(reader: R, cipher: Cipher, key: &SecretVec<u8>) -> impl CryptoRead<R> {
create_ring_read(reader, cipher, key)
}

Expand Down Expand Up @@ -649,7 +647,7 @@ mod tests {
&key,
&mut output,
)
.unwrap();
.unwrap();
assert_eq!(&output, content.as_bytes());
}

Expand Down
84 changes: 33 additions & 51 deletions src/crypto/read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, mem};
use std::io;
use std::io::{Read, Seek, SeekFrom};
use std::sync::{Arc, Mutex};

Expand All @@ -20,7 +20,7 @@ mod test;
///
/// > ⚠️ **Warning**
/// > This is not thread-safe.
/// Use [`CryptoReadSendSync`] instead for thread-safe scenarios.
/// > Use [`CryptoReadSendSync`] instead for thread-safe scenarios.
#[allow(clippy::module_name_repetitions)]
pub trait CryptoRead<R: Read>: Read {
#[allow(clippy::wrong_self_convention)]
Expand All @@ -31,21 +31,23 @@ pub trait CryptoRead<R: Read>: Read {
///
/// > ⚠️ **Warning**
/// > This is not thread-safe.
/// Use [`CryptoReadSeekSendSync`] instead for thread-safe scenarios.
pub trait CryptoReadSeek<R: Read + Seek>:
CryptoRead<R> + Seek
{}
/// > Use [`CryptoReadSeekSendSync`] instead for thread-safe scenarios.
pub trait CryptoReadSeek<R: Read + Seek>: CryptoRead<R> + Seek {}

/// Thread-safe versions which are [`Send`] + [`Seek`] + `'static`.
/// [`Send`] + [`Seek`] version of [`CryptoReadSeek`].
pub trait CryptoReadSeekSendSync<R: Read + Seek + Send + Sync + 'static>:
CryptoRead<R> + Seek + Send + Sync + 'static
{}
CryptoRead<R> + Seek + Send + Sync + 'static
{
}

/// [`Send`] + [`Seek`] version of [`CryptoReadSeekSendSync`].
#[allow(clippy::module_name_repetitions)]
pub trait CryptoReadSendSync<R: Read + Send + Sync + 'static>: CryptoRead<R> + Send + Sync + 'static {}
pub trait CryptoReadSendSync<R: Read + Send + Sync + 'static>:
CryptoRead<R> + Send + Sync + 'static
{
}

/// ring
Expand Down Expand Up @@ -97,8 +99,8 @@ macro_rules! decrypt_block {
}};
}

pub(crate) use decrypt_block;
use crate::crypto::Cipher;
pub(crate) use decrypt_block;

#[allow(clippy::module_name_repetitions)]
pub struct RingCryptoRead<R: Read> {
Expand Down Expand Up @@ -192,7 +194,7 @@ impl<R: Read + Seek> RingCryptoRead<R> {
}
let plaintext_len = ciphertext_len
- ((ciphertext_len / self.ciphertext_block_size as u64) + 1)
* (self.ciphertext_block_size - self.plaintext_block_size) as u64;
* (self.ciphertext_block_size - self.plaintext_block_size) as u64;
Ok(plaintext_len)
}
}
Expand Down Expand Up @@ -281,10 +283,7 @@ impl<R> CryptoReadSendSyncImpl<R>
where
R: Read + Send + Sync + 'static,
{
pub fn new(reader: R,
cipher: Cipher,
key: &SecretVec<u8>,
) -> Self {
pub fn new(reader: R, cipher: Cipher, key: &SecretVec<u8>) -> Self {
Self {
inner: Mutex::new(Some(Box::new(crypto::create_read(reader, cipher, key)))),
}
Expand All @@ -307,26 +306,16 @@ where
{
fn into_inner(&mut self) -> R {
let mut lock = self.inner.lock().unwrap();
let mut boxed = mem::replace(&mut *lock, None).take().unwrap();
let mut boxed = (*lock).take().take().unwrap();
boxed.into_inner()

}
}

unsafe impl<R> Send for CryptoReadSendSyncImpl<R>
where
R: Read + Send + Sync + 'static,
{}
unsafe impl<R> Send for CryptoReadSendSyncImpl<R> where R: Read + Send + Sync + 'static {}

unsafe impl<R> Sync for CryptoReadSendSyncImpl<R>
where
R: Read + Send + Sync + 'static,
{}
unsafe impl<R> Sync for CryptoReadSendSyncImpl<R> where R: Read + Send + Sync + 'static {}

impl<R> CryptoReadSendSync<R> for CryptoReadSendSyncImpl<R>
where
R: Read + Send + Sync + 'static,
{}
impl<R> CryptoReadSendSync<R> for CryptoReadSendSyncImpl<R> where R: Read + Send + Sync + 'static {}

pub struct CryptoReadSeekSendSyncImpl<R>
where
Expand All @@ -335,26 +324,19 @@ where
inner: Mutex<Option<Box<dyn CryptoReadSeek<R>>>>,
}

unsafe impl<R> Send for CryptoReadSeekSendSyncImpl<R>
where
R: Read + Seek + Send + Sync + 'static,
{}
unsafe impl<R> Send for CryptoReadSeekSendSyncImpl<R> where R: Read + Seek + Send + Sync + 'static {}

unsafe impl<R> Sync for CryptoReadSeekSendSyncImpl<R>
where
R: Read + Seek + Send + Sync + 'static,
{}
unsafe impl<R> Sync for CryptoReadSeekSendSyncImpl<R> where R: Read + Seek + Send + Sync + 'static {}

impl<R> CryptoReadSeekSendSyncImpl<R>
where
R: Read + Seek + Send + Sync + 'static,
{
pub fn new(reader: R,
cipher: Cipher,
key: &SecretVec<u8>,
) -> Self {
pub fn new(reader: R, cipher: Cipher, key: &SecretVec<u8>) -> Self {
Self {
inner: Mutex::new(Some(Box::new(crypto::create_read_seek(reader, cipher, key)))),
inner: Mutex::new(Some(Box::new(crypto::create_read_seek(
reader, cipher, key,
)))),
}
}
}
Expand Down Expand Up @@ -385,17 +367,17 @@ where
{
fn into_inner(&mut self) -> R {
let mut lock = self.inner.lock().unwrap();
let mut boxed = mem::replace(&mut *lock, None).take().unwrap();
let mut boxed = (*lock).take().take().unwrap();
boxed.into_inner()
}
}

impl<R> CryptoReadSeek<R> for CryptoReadSeekSendSyncImpl<R>
where
R: Read + Seek + Send + Sync + 'static,
{}
impl<R> CryptoReadSeek<R> for CryptoReadSeekSendSyncImpl<R> where
R: Read + Seek + Send + Sync + 'static
{
}

impl<R> CryptoReadSeekSendSync<R> for CryptoReadSeekSendSyncImpl<R>
where
R: Read + Seek + Send + Sync + 'static,
{}
impl<R> CryptoReadSeekSendSync<R> for CryptoReadSeekSendSyncImpl<R> where
R: Read + Seek + Send + Sync + 'static
{
}
Loading

0 comments on commit acf71a1

Please sign in to comment.