Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H-721: Remove async-trait dependency #5315

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions apps/hash-graph/libs/api/src/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,16 @@ impl<S> FromRequestParts<S> for AuthenticatedUserHeader {
pub struct PermissionResponse {
has_permission: bool,
}
#[async_trait]

pub trait RestApiStore: Store + TypeFetcher {
async fn load_external_type(
fn load_external_type(
&mut self,
actor_id: AccountId,
domain_validator: &DomainValidator,
reference: OntologyTypeReference<'_>,
) -> Result<OntologyTypeMetadata, Response>;
) -> impl Future<Output = Result<OntologyTypeMetadata, Response>> + Send;
}

#[async_trait]
impl<S> RestApiStore for S
where
S: Store + TypeFetcher + Send,
Expand Down
1 change: 0 additions & 1 deletion apps/hash-graph/libs/graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type-system = { workspace = true, features = ["postgres"] }

# Private third-party dependencies
async-scoped = { workspace = true, features = ["use-tokio"] }
async-trait = { workspace = true }
bytes = { workspace = true }
clap = { workspace = true, features = ["derive", "env"], optional = true }
derive-where = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/snapshot/entity/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use authorization::{AuthorizationApi, backend::ZanzibarBackend, schema::EntityRelationAndSubject};
use error_stack::{Report, ResultExt};
use graph_types::{
Expand Down Expand Up @@ -36,7 +35,6 @@ pub enum EntityRowBatch {
Relations(Vec<(EntityUuid, EntityRelationAndSubject)>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for EntityRowBatch
where
C: AsClient,
Expand Down
15 changes: 9 additions & 6 deletions apps/hash-graph/libs/graph/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mod web;
use core::future::ready;

use async_scoped::TokioScope;
use async_trait::async_trait;
use authorization::{
AuthorizationApi, NoAuthorization,
backend::ZanzibarBackend,
Expand Down Expand Up @@ -229,14 +228,18 @@ impl SnapshotEntry {
}
}

#[async_trait]
trait WriteBatch<C, A> {
async fn begin(postgres_client: &mut PostgresStore<C, A>) -> Result<(), InsertionError>;
async fn write(self, postgres_client: &mut PostgresStore<C, A>) -> Result<(), InsertionError>;
async fn commit(
fn begin(
postgres_client: &mut PostgresStore<C, A>,
) -> impl Future<Output = Result<(), InsertionError>> + Send;
fn write(
self,
postgres_client: &mut PostgresStore<C, A>,
) -> impl Future<Output = Result<(), InsertionError>> + Send;
fn commit(
postgres_client: &mut PostgresStore<C, A>,
validation: bool,
) -> Result<(), InsertionError>;
) -> impl Future<Output = Result<(), InsertionError>> + Send;
}

pub struct SnapshotStore<C, A>(PostgresStore<C, A>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use async_trait::async_trait;
use authorization::{
AuthorizationApi, backend::ZanzibarBackend, schema::DataTypeRelationAndSubject,
};
Expand All @@ -23,7 +22,6 @@ pub enum DataTypeRowBatch {
Embeddings(Vec<DataTypeEmbeddingRow<'static>>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for DataTypeRowBatch
where
C: AsClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use async_trait::async_trait;
use authorization::{
AuthorizationApi, backend::ZanzibarBackend, schema::EntityTypeRelationAndSubject,
};
Expand Down Expand Up @@ -33,7 +32,6 @@ pub enum EntityTypeRowBatch {
Embeddings(Vec<EntityTypeEmbeddingRow<'static>>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for EntityTypeRowBatch
where
C: AsClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use error_stack::{Result, ResultExt};
use tokio_postgres::GenericClient;

Expand All @@ -20,7 +19,6 @@ pub enum OntologyTypeMetadataRowBatch {
ExternalMetadata(Vec<OntologyExternalMetadataRow>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for OntologyTypeMetadataRowBatch
where
C: AsClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use async_trait::async_trait;
use authorization::{backend::ZanzibarBackend, schema::PropertyTypeRelationAndSubject};
use error_stack::{Result, ResultExt};
use graph_types::ontology::PropertyTypeId;
Expand All @@ -25,7 +24,6 @@ pub enum PropertyTypeRowBatch {
Embeddings(Vec<PropertyTypeEmbeddingRow<'static>>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for PropertyTypeRowBatch
where
C: AsClient,
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/snapshot/owner/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use authorization::{backend::ZanzibarBackend, schema::AccountGroupRelationAndSubject};
use error_stack::{Result, ResultExt};
use graph_types::account::AccountGroupId;
Expand All @@ -18,7 +17,6 @@ pub enum AccountRowBatch {
AccountGroupAccountRelations(Vec<(AccountGroupId, AccountGroupRelationAndSubject)>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for AccountRowBatch
where
C: AsClient,
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/snapshot/restore/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use authorization::{AuthorizationApi, backend::ZanzibarBackend};
use error_stack::Result;

Expand Down Expand Up @@ -26,7 +25,6 @@ pub enum SnapshotRecordBatch {
Entities(EntityRowBatch),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for SnapshotRecordBatch
where
C: AsClient,
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/snapshot/web/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use authorization::{backend::ZanzibarBackend, schema::WebRelationAndSubject};
use error_stack::{Result, ResultExt};
use graph_types::owned_by_id::OwnedById;
Expand All @@ -14,7 +13,6 @@ pub enum WebBatch {
Relations(Vec<(OwnedById, WebRelationAndSubject)>),
}

#[async_trait]
impl<C, A> WriteBatch<C, A> for WebBatch
where
C: AsClient,
Expand Down
20 changes: 8 additions & 12 deletions apps/hash-graph/libs/graph/src/store/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
//!
//! [`Store`]: crate::store::Store

use async_trait::async_trait;
use error_stack::Result;
use futures::{Stream, TryStreamExt};
use futures::{Stream, TryFutureExt, TryStreamExt};
use hash_graph_store::{
filter::{Filter, QueryRecord},
subgraph::{SubgraphRecord, temporal_axes::QueryTemporalAxes},
Expand Down Expand Up @@ -202,36 +201,33 @@ pub trait ReadPaginated<R: QueryRecord, S: Sorting + Sync>: Read<R> {
/// Read access to a [`Store`].
///
/// [`Store`]: crate::store::Store
#[async_trait]
pub trait Read<R: QueryRecord>: Sync {
type ReadStream: Stream<Item = Result<R, QueryError>> + Send + Sync;

async fn read(
fn read(
&self,
filter: &Filter<'_, R>,
temporal_axes: Option<&QueryTemporalAxes>,
include_drafts: bool,
) -> Result<Self::ReadStream, QueryError>;
) -> impl Future<Output = Result<Self::ReadStream, QueryError>> + Send;

#[instrument(level = "info", skip(self, filter))]
async fn read_vec(
fn read_vec(
&self,
filter: &Filter<'_, R>,
temporal_axes: Option<&QueryTemporalAxes>,
include_drafts: bool,
) -> Result<Vec<R>, QueryError> {
) -> impl Future<Output = Result<Vec<R>, QueryError>> + Send {
self.read(filter, temporal_axes, include_drafts)
.await?
.try_collect()
.await
.and_then(TryStreamExt::try_collect)
}
Comment on lines 214 to 223
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and_then works quite nice here, no await is needed here 🚀


async fn read_one(
fn read_one(
&self,
filter: &Filter<'_, R>,
temporal_axes: Option<&QueryTemporalAxes>,
include_drafts: bool,
) -> Result<R, QueryError>;
) -> impl Future<Output = Result<R, QueryError>> + Send;
}

// TODO: Add remaining CRUD traits
8 changes: 2 additions & 6 deletions apps/hash-graph/libs/graph/src/store/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::sync::Arc;
use core::mem;
use std::collections::{HashMap, HashSet};

use async_trait::async_trait;
use authorization::{
AuthorizationApi,
schema::{
Expand Down Expand Up @@ -73,14 +72,13 @@ use crate::{
},
};

#[async_trait]
pub trait TypeFetcher {
/// Fetches the provided type reference and inserts it to the Graph.
async fn insert_external_ontology_type(
fn insert_external_ontology_type(
&mut self,
actor_id: AccountId,
reference: OntologyTypeReference<'_>,
) -> Result<OntologyTypeMetadata, InsertionError>;
) -> impl Future<Output = Result<OntologyTypeMetadata, InsertionError>> + Send;
}

#[derive(Clone)]
Expand Down Expand Up @@ -675,7 +673,6 @@ where
}
}

#[async_trait]
impl<S, A> TypeFetcher for FetchingStore<S, A>
where
A: ToSocketAddrs + Send + Sync,
Expand Down Expand Up @@ -740,7 +737,6 @@ where
}
}

#[async_trait]
impl<S, A, R> Read<R> for FetchingStore<S, A>
where
A: Send + Sync,
Expand Down
18 changes: 12 additions & 6 deletions apps/hash-graph/libs/graph/src/store/migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use error_stack::Result;

use super::error::MigrationError;
Expand Down Expand Up @@ -54,12 +53,19 @@ impl Migration {
///
/// In addition to the errors described in the methods of this trait, further errors might also be
/// raised depending on the implementation, e.g. connection issues.
#[async_trait]
pub trait StoreMigration: Sync {
async fn run_migrations(&mut self) -> Result<Vec<Migration>, MigrationError>;
fn run_migrations(
&mut self,
) -> impl Future<Output = Result<Vec<Migration>, MigrationError>> + Send;

async fn all_migrations(&mut self) -> Result<Vec<Migration>, MigrationError>;
fn all_migrations(
&mut self,
) -> impl Future<Output = Result<Vec<Migration>, MigrationError>> + Send;

async fn applied_migrations(&mut self) -> Result<Vec<Migration>, MigrationError>;
async fn missing_migrations(&mut self) -> Result<Vec<Migration>, MigrationError>;
fn applied_migrations(
&mut self,
) -> impl Future<Output = Result<Vec<Migration>, MigrationError>> + Send;
fn missing_migrations(
&mut self,
) -> impl Future<Output = Result<Vec<Migration>, MigrationError>> + Send;
}
3 changes: 1 addition & 2 deletions apps/hash-graph/libs/graph/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod validation;
mod fetcher;
pub(crate) mod postgres;

use async_trait::async_trait;
use hash_graph_store::account::AccountStore;
use serde::Deserialize;
#[cfg(feature = "utoipa")]
Expand Down Expand Up @@ -41,11 +40,11 @@ pub use self::{
///
/// In addition to the errors described in the methods of this trait, further errors might also be
/// raised depending on the implementation, e.g. connection issues.
#[async_trait]
pub trait Store:
AccountStore + DataTypeStore + PropertyTypeStore + EntityTypeStore + EntityStore
{
}

impl<S> Store for S where
S: AccountStore + DataTypeStore + PropertyTypeStore + EntityTypeStore + EntityStore
{
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/store/postgres/crud.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use error_stack::{Report, ResultExt};
use futures::{Stream, StreamExt, TryStreamExt};
use hash_graph_store::{filter::Filter, subgraph::temporal_axes::QueryTemporalAxes};
Expand Down Expand Up @@ -99,7 +98,6 @@ where
}
}

#[async_trait]
impl<Cl, A, R> Read<R> for PostgresStore<Cl, A>
where
Cl: AsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ where
let num_returned_entities = entities.len();

let (permitted_ids, zookie) = if let Some((permitted_ids, zookie)) = &permissions {
(Cow::Borrowed(permitted_ids), Cow::Borrowed(zookie))
(
Cow::<HashSet<EntityUuid>>::Borrowed(permitted_ids),
Cow::<Zookie>::Borrowed(zookie),
)
} else {
// TODO: The subgraph structure differs from the API interface. At the API the
// vertices are stored in a nested `HashMap` and here it's flattened. We need
Expand Down
2 changes: 0 additions & 2 deletions apps/hash-graph/libs/graph/src/store/postgres/migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use error_stack::{Result, ResultExt};
use tokio_postgres::Client;

Expand Down Expand Up @@ -31,7 +30,6 @@ impl Migration {
}
}

#[async_trait]
impl<C, A> StoreMigration for PostgresStore<C, A>
where
C: AsClient<Client = Client>,
Expand Down
Loading