From 612599c61011e30dfef62454ef42998930c44f24 Mon Sep 17 00:00:00 2001 From: CodesInChaos Date: Wed, 28 Aug 2024 11:13:31 +0200 Subject: [PATCH 1/4] Add QueryExecutor trait The trait enables writing functions that don't care if they're executed in a transaction or directly on a client --- edgedb-tokio/src/lib.rs | 2 + edgedb-tokio/src/query_executor.rs | 192 +++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 edgedb-tokio/src/query_executor.rs diff --git a/edgedb-tokio/src/lib.rs b/edgedb-tokio/src/lib.rs index 6cb60db8..92751240 100644 --- a/edgedb-tokio/src/lib.rs +++ b/edgedb-tokio/src/lib.rs @@ -137,6 +137,7 @@ mod sealed; pub mod state; mod transaction; pub mod tutorial; +mod query_executor; pub use edgedb_derive::{ConfigDelta, GlobalsDelta, Queryable}; @@ -147,6 +148,7 @@ pub use errors::Error; pub use options::{RetryCondition, RetryOptions, TransactionOptions}; pub use state::{ConfigDelta, GlobalsDelta}; pub use transaction::Transaction; +pub use query_executor::QueryExecutor; #[cfg(feature = "unstable")] pub use builder::get_project_dir; diff --git a/edgedb-tokio/src/query_executor.rs b/edgedb-tokio/src/query_executor.rs new file mode 100644 index 00000000..52737459 --- /dev/null +++ b/edgedb-tokio/src/query_executor.rs @@ -0,0 +1,192 @@ +use edgedb_protocol::model::Json; +use edgedb_protocol::query_arg::QueryArgs; +use edgedb_protocol::QueryResult; +use std::future::Future; + +use crate::{Client, Error, Transaction}; + +/// Abstracts over different query executors +/// In particular &Client and &mut Transaction +pub trait QueryExecutor { + /// see [Client::query] + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_single] + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_required_single] + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_json] + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future>; + + /// see [Client::query_single_json] + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>>; + + /// see [Client::query_required_single_json] + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future>; + + /// see [Client::execute] + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs; +} + +impl QueryExecutor for &Client { + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Client::query(self, query, arguments) + } + + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Client::query_single(self, query, arguments) + } + + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult, + { + Client::query_required_single(self, query, arguments) + } + + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Client::query_json(self, query, arguments) + } + + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>> { + Client::query_single_json(self, query, arguments) + } + + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Client::query_required_single_json(self, query, arguments) + } + + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs, + { + Client::execute(self, query, arguments) + } +} + +impl QueryExecutor for &mut Transaction { + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query(self, query, arguments) + } + + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query_single(self, query, arguments) + } + + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query_required_single(self, query, arguments) + } + + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Transaction::query_json(self, query, arguments) + } + + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>> { + Transaction::query_single_json(self, query, arguments) + } + + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Transaction::query_required_single_json(self, query, arguments) + } + + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs, + { + Transaction::execute(self, query, arguments) + } +} From 00ccea9fd95ed4d9e997dbf99351d039b127f2ac Mon Sep 17 00:00:00 2001 From: CodesInChaos Date: Wed, 28 Aug 2024 16:11:49 +0200 Subject: [PATCH 2/4] Add `Send` constraints on `QueryExecutor` --- edgedb-tokio/src/query_executor.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/edgedb-tokio/src/query_executor.rs b/edgedb-tokio/src/query_executor.rs index 52737459..41112426 100644 --- a/edgedb-tokio/src/query_executor.rs +++ b/edgedb-tokio/src/query_executor.rs @@ -9,44 +9,44 @@ use crate::{Client, Error, Transaction}; /// In particular &Client and &mut Transaction pub trait QueryExecutor { /// see [Client::query] - fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + Send where A: QueryArgs, - R: QueryResult; + R: QueryResult + Send; /// see [Client::query_single] fn query_single( self, query: &str, arguments: &A, - ) -> impl Future, Error>> + ) -> impl Future, Error>> + Send where A: QueryArgs, - R: QueryResult; + R: QueryResult + Send; /// see [Client::query_required_single] fn query_required_single( self, query: &str, arguments: &A, - ) -> impl Future> + ) -> impl Future> + Send where A: QueryArgs, - R: QueryResult; + R: QueryResult + Send; /// see [Client::query_json] fn query_json( self, query: &str, arguments: &impl QueryArgs, - ) -> impl Future>; + ) -> impl Future> + Send; /// see [Client::query_single_json] fn query_single_json( &mut self, query: &str, arguments: &impl QueryArgs, - ) -> impl Future, Error>>; + ) -> impl Future, Error>> + Send; /// see [Client::query_required_single_json] fn query_required_single_json( @@ -56,7 +56,7 @@ pub trait QueryExecutor { ) -> impl Future>; /// see [Client::execute] - fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + Send where A: QueryArgs; } @@ -77,7 +77,7 @@ impl QueryExecutor for &Client { ) -> impl Future, Error>> where A: QueryArgs, - R: QueryResult, + R: QueryResult + Send, { Client::query_single(self, query, arguments) } @@ -86,10 +86,10 @@ impl QueryExecutor for &Client { self, query: &str, arguments: &A, - ) -> impl Future> + ) -> impl Future> + Send where A: QueryArgs, - R: QueryResult, + R: QueryResult + Send, { Client::query_required_single(self, query, arguments) } From 3bb6a2db7168bf972c644b63bb4cf1008f3f1990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Mur=20Er=C5=BEen?= Date: Wed, 28 Aug 2024 13:46:10 +0200 Subject: [PATCH 3/4] fmt --- edgedb-tokio/src/lib.rs | 4 ++-- edgedb-tokio/src/query_executor.rs | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/edgedb-tokio/src/lib.rs b/edgedb-tokio/src/lib.rs index 92751240..4827270f 100644 --- a/edgedb-tokio/src/lib.rs +++ b/edgedb-tokio/src/lib.rs @@ -133,11 +133,11 @@ mod builder; mod client; mod errors; mod options; +mod query_executor; mod sealed; pub mod state; mod transaction; pub mod tutorial; -mod query_executor; pub use edgedb_derive::{ConfigDelta, GlobalsDelta, Queryable}; @@ -146,9 +146,9 @@ pub use client::Client; pub use credentials::TlsSecurity; pub use errors::Error; pub use options::{RetryCondition, RetryOptions, TransactionOptions}; +pub use query_executor::QueryExecutor; pub use state::{ConfigDelta, GlobalsDelta}; pub use transaction::Transaction; -pub use query_executor::QueryExecutor; #[cfg(feature = "unstable")] pub use builder::get_project_dir; diff --git a/edgedb-tokio/src/query_executor.rs b/edgedb-tokio/src/query_executor.rs index 41112426..13400cd8 100644 --- a/edgedb-tokio/src/query_executor.rs +++ b/edgedb-tokio/src/query_executor.rs @@ -9,7 +9,11 @@ use crate::{Client, Error, Transaction}; /// In particular &Client and &mut Transaction pub trait QueryExecutor { /// see [Client::query] - fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + Send + fn query( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + Send where A: QueryArgs, R: QueryResult + Send; @@ -56,7 +60,11 @@ pub trait QueryExecutor { ) -> impl Future>; /// see [Client::execute] - fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + Send + fn execute( + &mut self, + query: &str, + arguments: &A, + ) -> impl Future> + Send where A: QueryArgs; } From 1f260d718481ef0d5c137b8be9aa2a75fcbb52ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Mur=20Er=C5=BEen?= Date: Wed, 28 Aug 2024 13:50:39 +0200 Subject: [PATCH 4/4] Increase MSRV to 1.75 --- Cargo.toml | 2 +- flake.nix | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f5cd66b3..8ff6b180 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ debug = true lto = true [workspace.package] -rust-version = "1.72" # keep in sync with flake.nix +rust-version = "1.75" # keep in sync with flake.nix [workspace.lints.clippy] useless_format = 'allow' diff --git a/flake.nix b/flake.nix index ad242959..4d253c9c 100644 --- a/flake.nix +++ b/flake.nix @@ -54,8 +54,8 @@ devShells.minimum = pkgs.mkShell { buildInputs = [ (fenix_pkgs.toolchainOf { - channel = "1.72"; # keep in sync with ./Cargo.toml rust-version - sha256 = "sha256-dxE7lmCFWlq0nl/wKcmYvpP9zqQbBitAQgZ1zx9Ooik="; + channel = "1.75"; # keep in sync with ./Cargo.toml rust-version + sha256 = "sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8="; }).defaultToolchain ] ++ common; };