From da50312a40c0c1b6884e7fc96375a4eb68737e45 Mon Sep 17 00:00:00 2001 From: Anton Bilous Date: Sat, 1 Feb 2025 11:51:58 +0200 Subject: [PATCH] Authentication service & Send bounds for async --- .../src/app/authentication/src/repository.rs | 24 +++++++++---------- .../src/database/src/adapter/mysql/base.rs | 8 ++++++- .../src/database/src/adapter/mysql/package.rs | 8 ++++++- .../src/database/src/adapter/mysql/user.rs | 8 ++++++- 3/coursework/src/database/src/atomic.rs | 7 +++--- 3/coursework/src/database/src/connect.rs | 5 ++-- 3/coursework/src/database/src/lib.rs | 14 ----------- 3/coursework/src/database/src/port.rs | 17 ++++++++----- 3/coursework/src/database/src/port/base.rs | 1 - 3/coursework/src/database/src/port/package.rs | 1 - 3/coursework/src/database/src/port/user.rs | 1 - 11 files changed, 49 insertions(+), 45 deletions(-) diff --git a/3/coursework/src/app/authentication/src/repository.rs b/3/coursework/src/app/authentication/src/repository.rs index 9cd74ea..766731b 100644 --- a/3/coursework/src/app/authentication/src/repository.rs +++ b/3/coursework/src/app/authentication/src/repository.rs @@ -5,14 +5,12 @@ use derive_more::{Deref, DerefMut}; #[derive(Deref, DerefMut)] pub struct Authenticated(User); -#[allow(async_fn_in_trait)] pub trait AuthenticationRepository { - async fn get_user(&self, get: Get) -> Result>; - async fn create_user(&self, new: New) -> Result; - async fn start_session(&self, user: User) -> Result; + fn get_user(&self, get: Get) -> impl Future>> + Send; + fn create_user(&self, new: New) -> impl Future> + Send; + fn start_session(&self, user: User) -> impl Future> + Send; } - pub enum Get { Name(Name), Email(Email), @@ -34,8 +32,9 @@ use std::marker::PhantomData; pub struct AuthenticationAdapter where - D: Connect, - UR: UserRepository, + C: Send, + D: Connect + Sync, + UR: UserRepository + Sync, { driver: D, _user_repository: PhantomData, @@ -43,8 +42,9 @@ where impl AuthenticationAdapter where - D: Connect, - UR: UserRepository, + C: Send, + D: Connect + Sync, + UR: UserRepository + Sync, { pub const fn new(driver: D) -> Self { Self { @@ -56,8 +56,9 @@ where impl AuthenticationRepository for AuthenticationAdapter where - D: Connect, - UR: UserRepository, + C: Send, + D: Connect + Sync, + UR: UserRepository + Sync, { async fn get_user(&self, get: Get) -> Result> { let c = self.driver.open_connection().await?; @@ -83,4 +84,3 @@ where Ok(Authenticated(user)) } } - diff --git a/3/coursework/src/database/src/adapter/mysql/base.rs b/3/coursework/src/database/src/adapter/mysql/base.rs index 9eaa45d..02d358e 100644 --- a/3/coursework/src/database/src/adapter/mysql/base.rs +++ b/3/coursework/src/database/src/adapter/mysql/base.rs @@ -4,9 +4,15 @@ use sqlx::{Executor, MySql}; pub struct BaseAdapter; -impl BaseRepository for BaseAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {} +impl BaseRepository for BaseAdapter +where + E: Send, + for<'a> &'a E: Executor<'a, Database = MySql>, +{ +} impl crate::port::CRUD for BaseAdapter where + E: Send, for<'a> &'a E: Executor<'a, Database = MySql>, { type New = New; diff --git a/3/coursework/src/database/src/adapter/mysql/package.rs b/3/coursework/src/database/src/adapter/mysql/package.rs index 8ea6aea..9819f30 100644 --- a/3/coursework/src/database/src/adapter/mysql/package.rs +++ b/3/coursework/src/database/src/adapter/mysql/package.rs @@ -4,9 +4,15 @@ use sqlx::{Executor, MySql}; pub struct PackageAdapter; -impl PackageRepository for PackageAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {} +impl PackageRepository for PackageAdapter +where + E: Send, + for<'a> &'a E: Executor<'a, Database = MySql>, +{ +} impl crate::port::CRUD for PackageAdapter where + E: Send, for<'a> &'a E: Executor<'a, Database = MySql>, { type New = New; diff --git a/3/coursework/src/database/src/adapter/mysql/user.rs b/3/coursework/src/database/src/adapter/mysql/user.rs index e647e47..9c06555 100644 --- a/3/coursework/src/database/src/adapter/mysql/user.rs +++ b/3/coursework/src/database/src/adapter/mysql/user.rs @@ -4,9 +4,15 @@ use sqlx::{Executor, MySql}; pub struct UserAdapter; -impl UserRepository for UserAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {} +impl UserRepository for UserAdapter +where + E: Send, + for<'a> &'a E: Executor<'a, Database = MySql>, +{ +} impl crate::port::CRUD for UserAdapter where + E: Send, for<'a> &'a E: Executor<'a, Database = MySql>, { type New = New; diff --git a/3/coursework/src/database/src/atomic.rs b/3/coursework/src/database/src/atomic.rs index 63721aa..cfb5cfe 100644 --- a/3/coursework/src/database/src/atomic.rs +++ b/3/coursework/src/database/src/atomic.rs @@ -1,12 +1,11 @@ type Result = std::result::Result>; -#[allow(async_fn_in_trait)] pub trait Atomic { type Transaction<'a>; - async fn start_transaction(&mut self) -> Result>; - async fn abort_transaction(transaction: Self::Transaction<'_>) -> Result; - async fn commit_transaction(transaction: Self::Transaction<'_>) -> Result; + fn start_transaction(&mut self) -> impl Future>> + Send; + fn abort_transaction(transaction: Self::Transaction<'_>) -> impl Future + Send; + fn commit_transaction(transaction: Self::Transaction<'_>) -> impl Future + Send; } use sqlx::Connection; diff --git a/3/coursework/src/database/src/connect.rs b/3/coursework/src/database/src/connect.rs index b8bc3b5..9c4ecb5 100644 --- a/3/coursework/src/database/src/connect.rs +++ b/3/coursework/src/database/src/connect.rs @@ -1,11 +1,10 @@ type Result = std::result::Result>; -#[allow(async_fn_in_trait)] pub trait Connect { type Connection; - async fn open_connection(&self) -> Result; - async fn close_connection(connection: Self::Connection) -> Result; + fn open_connection(&self) -> impl Future> + Send; + fn close_connection(connection: Self::Connection) -> impl Future + Send; } use sqlx::Connection; diff --git a/3/coursework/src/database/src/lib.rs b/3/coursework/src/database/src/lib.rs index 0108ec5..b66abeb 100644 --- a/3/coursework/src/database/src/lib.rs +++ b/3/coursework/src/database/src/lib.rs @@ -2,17 +2,3 @@ pub mod adapter; pub mod atomic; pub mod connect; pub mod port; - -// use garde::{Report, Unvalidated, Valid, Validate}; - -// pub trait IntoValid: Validate { -// fn into_valid(self) -> Result, Report> -// where -// Self: Sized, -// ::Context: Default, -// { -// Unvalidated::new(self).validate() -// } -// } -// impl IntoValid for T {} - diff --git a/3/coursework/src/database/src/port.rs b/3/coursework/src/database/src/port.rs index ad0988d..5daecd5 100644 --- a/3/coursework/src/database/src/port.rs +++ b/3/coursework/src/database/src/port.rs @@ -1,20 +1,25 @@ pub type Result> = std::result::Result; -#[allow(async_fn_in_trait)] pub trait CRUD { type New; type Unique; type Update; type Existing; - async fn create(connection: &mut C, data: Self::New) -> Result; - async fn read(connection: &C, data: Self::Unique) -> Result>; - async fn update( + fn create( + connection: &mut C, + data: Self::New, + ) -> impl Future> + Send; + fn read( + connection: &C, + data: Self::Unique, + ) -> impl Future>> + Send; + fn update( connection: &mut C, existing: &mut Self::Existing, data: Self::Update, - ) -> Result; - async fn delete(connection: &mut C, data: Self::Unique) -> Result; + ) -> impl Future + Send; + fn delete(connection: &mut C, data: Self::Unique) -> impl Future + Send; } trait CharLength { diff --git a/3/coursework/src/database/src/port/base.rs b/3/coursework/src/database/src/port/base.rs index e228966..c312733 100644 --- a/3/coursework/src/database/src/port/base.rs +++ b/3/coursework/src/database/src/port/base.rs @@ -4,7 +4,6 @@ pub use super::{CRUD, Result}; pub use chrono::{DateTime, Utc}; use derive_more::{Deref, Into}; -#[allow(async_fn_in_trait)] pub trait BaseRepository: CRUD { diff --git a/3/coursework/src/database/src/port/package.rs b/3/coursework/src/database/src/port/package.rs index d4f6859..7d4029b 100644 --- a/3/coursework/src/database/src/port/package.rs +++ b/3/coursework/src/database/src/port/package.rs @@ -4,7 +4,6 @@ pub use super::{CRUD, Result, base::Base}; pub use chrono::{DateTime, Utc}; use derive_more::{Deref, Into}; -#[allow(async_fn_in_trait)] pub trait PackageRepository: CRUD { diff --git a/3/coursework/src/database/src/port/user.rs b/3/coursework/src/database/src/port/user.rs index b4d4141..fb49757 100644 --- a/3/coursework/src/database/src/port/user.rs +++ b/3/coursework/src/database/src/port/user.rs @@ -4,7 +4,6 @@ pub use super::{CRUD, Result}; pub use chrono::{DateTime, Utc}; use derive_more::{Deref, Into}; -#[allow(async_fn_in_trait)] pub trait UserRepository: CRUD {