Remove direct dependency on async_trait (#3382)

Not completely sure it's worth it since some other dependencies still
depend on it. Anyway, proc macros are said to be bad for compile times, I just typed out what the proc macro generates and it's only 8 more lines, and we're already doing it this way in e.g. action_by_contact() and collect_texts_recursive() (the latter needs the boxed future both for the trait and for recursion).
This commit is contained in:
Hocuri
2022-06-02 10:57:19 +02:00
committed by GitHub
parent 80efaa0dfa
commit e2b3339475
4 changed files with 61 additions and 55 deletions

View File

@@ -3,7 +3,7 @@
## Unreleased ## Unreleased
### Changes ### Changes
- refactorings #3373 #3345 #3380 - refactorings #3373 #3345 #3380 #3382
- node: move split2 to devDependencies - node: move split2 to devDependencies

1
Cargo.lock generated
View File

@@ -1077,7 +1077,6 @@ dependencies = [
"async-std", "async-std",
"async-std-resolver", "async-std-resolver",
"async-tar", "async-tar",
"async-trait",
"backtrace", "backtrace",
"base64 0.13.0", "base64 0.13.0",
"bitflags", "bitflags",

View File

@@ -25,7 +25,6 @@ async-smtp = { git = "https://github.com/async-email/async-smtp", branch="master
async-std-resolver = "0.21" async-std-resolver = "0.21"
async-std = { version = "1" } async-std = { version = "1" }
async-tar = { version = "0.4", default-features=false } async-tar = { version = "0.4", default-features=false }
async-trait = "0.1"
backtrace = "0.3" backtrace = "0.3"
base64 = "0.13" base64 = "0.13"
bitflags = "1.3" bitflags = "1.3"

View File

@@ -3,9 +3,10 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::io::Cursor; use std::io::Cursor;
use std::pin::Pin;
use anyhow::{ensure, Context as _, Result}; use anyhow::{ensure, Context as _, Result};
use async_trait::async_trait; use futures::Future;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use pgp::composed::Deserializable; use pgp::composed::Deserializable;
use pgp::ser::Serialize; use pgp::ser::Serialize;
@@ -25,7 +26,6 @@ pub use pgp::composed::{SignedPublicKey, SignedSecretKey};
/// This trait is implemented for rPGP's [SignedPublicKey] and /// This trait is implemented for rPGP's [SignedPublicKey] and
/// [SignedSecretKey] types and makes working with them a little /// [SignedSecretKey] types and makes working with them a little
/// easier in the deltachat world. /// easier in the deltachat world.
#[async_trait]
pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone { pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone {
type KeyType: 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. /// Load the users' default key from the database.
async fn load_self(context: &Context) -> Result<Self::KeyType>; fn load_self<'a>(
context: &'a Context,
) -> Pin<Box<dyn Future<Output = Result<Self::KeyType>> + 'a + Send>>;
/// Serialise the key as bytes. /// Serialise the key as bytes.
fn to_bytes(&self) -> Vec<u8> { fn to_bytes(&self) -> Vec<u8> {
@@ -86,35 +88,38 @@ pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone {
} }
} }
#[async_trait]
impl DcKey for SignedPublicKey { impl DcKey for SignedPublicKey {
type KeyType = SignedPublicKey; type KeyType = SignedPublicKey;
async fn load_self(context: &Context) -> Result<Self::KeyType> { fn load_self<'a>(
let addr = context.get_primary_self_addr().await?; context: &'a Context,
match context ) -> Pin<Box<dyn Future<Output = Result<Self::KeyType>> + 'a + Send>> {
.sql Box::pin(async move {
.query_row_optional( let addr = context.get_primary_self_addr().await?;
r#" match context
SELECT public_key .sql
FROM keypairs .query_row_optional(
WHERE addr=? r#"
AND is_default=1; SELECT public_key
"#, FROM keypairs
paramsv![addr], WHERE addr=?
|row| { AND is_default=1;
let bytes: Vec<u8> = row.get(0)?; "#,
Ok(bytes) paramsv![addr],
}, |row| {
) let bytes: Vec<u8> = row.get(0)?;
.await? Ok(bytes)
{ },
Some(bytes) => Self::from_slice(&bytes), )
None => { .await?
let keypair = generate_keypair(context).await?; {
Ok(keypair.public) 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 { fn to_asc(&self, header: Option<(&str, &str)>) -> String {
@@ -134,34 +139,37 @@ impl DcKey for SignedPublicKey {
} }
} }
#[async_trait]
impl DcKey for SignedSecretKey { impl DcKey for SignedSecretKey {
type KeyType = SignedSecretKey; type KeyType = SignedSecretKey;
async fn load_self(context: &Context) -> Result<Self::KeyType> { fn load_self<'a>(
match context context: &'a Context,
.sql ) -> Pin<Box<dyn Future<Output = Result<Self::KeyType>> + 'a + Send>> {
.query_row_optional( Box::pin(async move {
r#" match context
SELECT private_key .sql
FROM keypairs .query_row_optional(
WHERE addr=(SELECT value FROM config WHERE keyname="configured_addr") r#"
AND is_default=1; SELECT private_key
"#, FROM keypairs
paramsv![], WHERE addr=(SELECT value FROM config WHERE keyname="configured_addr")
|row| { AND is_default=1;
let bytes: Vec<u8> = row.get(0)?; "#,
Ok(bytes) paramsv![],
}, |row| {
) let bytes: Vec<u8> = row.get(0)?;
.await? Ok(bytes)
{ },
Some(bytes) => Self::from_slice(&bytes), )
None => { .await?
let keypair = generate_keypair(context).await?; {
Ok(keypair.secret) 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 { fn to_asc(&self, header: Option<(&str, &str)>) -> String {