diff --git a/CHANGELOG.md b/CHANGELOG.md index 8565cb438..513919cb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased ### Changes -- refactorings #3373 #3345 #3380 +- refactorings #3373 #3345 #3380 #3382 - node: move split2 to devDependencies diff --git a/Cargo.lock b/Cargo.lock index 502dbae60..f69dc395c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1077,7 +1077,6 @@ dependencies = [ "async-std", "async-std-resolver", "async-tar", - "async-trait", "backtrace", "base64 0.13.0", "bitflags", diff --git a/Cargo.toml b/Cargo.toml index dccc4527c..29c5570db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ async-smtp = { git = "https://github.com/async-email/async-smtp", branch="master async-std-resolver = "0.21" async-std = { version = "1" } async-tar = { version = "0.4", default-features=false } -async-trait = "0.1" backtrace = "0.3" base64 = "0.13" bitflags = "1.3" diff --git a/src/key.rs b/src/key.rs index 923314dc0..d98f14991 100644 --- a/src/key.rs +++ b/src/key.rs @@ -3,9 +3,10 @@ use std::collections::BTreeMap; use std::fmt; use std::io::Cursor; +use std::pin::Pin; use anyhow::{ensure, Context as _, Result}; -use async_trait::async_trait; +use futures::Future; use num_traits::FromPrimitive; use pgp::composed::Deserializable; use pgp::ser::Serialize; @@ -25,7 +26,6 @@ pub use pgp::composed::{SignedPublicKey, SignedSecretKey}; /// This trait is implemented for rPGP's [SignedPublicKey] and /// [SignedSecretKey] types and makes working with them a little /// easier in the deltachat world. -#[async_trait] pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone { type KeyType: Serialize + Deserializable + KeyTrait + Clone; @@ -54,7 +54,9 @@ pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone { } /// Load the users' default key from the database. - async fn load_self(context: &Context) -> Result; + fn load_self<'a>( + context: &'a Context, + ) -> Pin> + 'a + Send>>; /// Serialise the key as bytes. fn to_bytes(&self) -> Vec { @@ -86,35 +88,38 @@ pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone { } } -#[async_trait] impl DcKey for SignedPublicKey { type KeyType = SignedPublicKey; - async fn load_self(context: &Context) -> Result { - let addr = context.get_primary_self_addr().await?; - match context - .sql - .query_row_optional( - r#" - SELECT public_key - FROM keypairs - WHERE addr=? - AND is_default=1; - "#, - paramsv![addr], - |row| { - let bytes: Vec = row.get(0)?; - Ok(bytes) - }, - ) - .await? - { - Some(bytes) => Self::from_slice(&bytes), - None => { - let keypair = generate_keypair(context).await?; - Ok(keypair.public) + fn load_self<'a>( + context: &'a Context, + ) -> Pin> + 'a + Send>> { + Box::pin(async move { + let addr = context.get_primary_self_addr().await?; + match context + .sql + .query_row_optional( + r#" + SELECT public_key + FROM keypairs + WHERE addr=? + AND is_default=1; + "#, + paramsv![addr], + |row| { + let bytes: Vec = row.get(0)?; + Ok(bytes) + }, + ) + .await? + { + Some(bytes) => Self::from_slice(&bytes), + None => { + let keypair = generate_keypair(context).await?; + Ok(keypair.public) + } } - } + }) } fn to_asc(&self, header: Option<(&str, &str)>) -> String { @@ -134,34 +139,37 @@ impl DcKey for SignedPublicKey { } } -#[async_trait] impl DcKey for SignedSecretKey { type KeyType = SignedSecretKey; - async fn load_self(context: &Context) -> Result { - match context - .sql - .query_row_optional( - r#" - SELECT private_key - FROM keypairs - WHERE addr=(SELECT value FROM config WHERE keyname="configured_addr") - AND is_default=1; - "#, - paramsv![], - |row| { - let bytes: Vec = row.get(0)?; - Ok(bytes) - }, - ) - .await? - { - Some(bytes) => Self::from_slice(&bytes), - None => { - let keypair = generate_keypair(context).await?; - Ok(keypair.secret) + fn load_self<'a>( + context: &'a Context, + ) -> Pin> + 'a + Send>> { + Box::pin(async move { + match context + .sql + .query_row_optional( + r#" + SELECT private_key + FROM keypairs + WHERE addr=(SELECT value FROM config WHERE keyname="configured_addr") + AND is_default=1; + "#, + paramsv![], + |row| { + let bytes: Vec = row.get(0)?; + Ok(bytes) + }, + ) + .await? + { + Some(bytes) => Self::from_slice(&bytes), + None => { + let keypair = generate_keypair(context).await?; + Ok(keypair.secret) + } } - } + }) } fn to_asc(&self, header: Option<(&str, &str)>) -> String {