Skip to content

Commit 06f74fc

Browse files
feat: migrate from async-std to tokio
depends on - [ ] chatmail/async-smtp#52 - [ ] chatmail/async-imap#21
1 parent fcdf766 commit 06f74fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1260
-1755
lines changed

Cargo.lock

+401-926
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+18-11
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ deltachat_derive = { path = "./deltachat_derive" }
1919

2020
ansi_term = { version = "0.12.1", optional = true }
2121
anyhow = "1"
22-
async-imap = { git = "https://github.com/async-email/async-imap" }
23-
async-native-tls = { version = "0.3" }
24-
async-smtp = { git = "https://github.com/async-email/async-smtp", branch="master", default-features=false, features = ["smtp-transport", "socks5"] }
25-
async-std-resolver = "0.21"
26-
async-std = { version = "1" }
27-
async-tar = { version = "0.4", default-features=false }
22+
async-imap = { git = "https://github.com/async-email/async-imap", branch = "runtimes", default-features = false, features = ["runtime-tokio"] }
23+
async-native-tls = { version = "0.4", default-features = false, features = ["runtime-tokio"] }
24+
async-smtp = { git = "https://github.com/async-email/async-smtp", branch="tokio", default-features = false, features = ["smtp-transport", "socks5", "runtime-tokio"] }
25+
trust-dns-resolver = "0.21"
26+
tokio = { version = "1", features = ["full"] }
27+
tokio-tar = { version = "0.3" } # TODO: integrate tokio into async-tar
2828
backtrace = "0.3"
2929
base64 = "0.13"
3030
bitflags = "1.3"
@@ -65,22 +65,24 @@ sha2 = "0.10"
6565
smallvec = "1"
6666
strum = "0.24"
6767
strum_macros = "0.24"
68-
surf = { version = "2.3", default-features = false, features = ["h1-client"] }
6968
thiserror = "1"
7069
toml = "0.5"
7170
url = "2"
7271
uuid = { version = "1", features = ["serde", "v4"] }
73-
fast-socks5 = "0.4"
72+
fast-socks5 = "0.8"
7473
humansize = "1"
7574
qrcodegen = "1.7.0"
7675
tagger = "4.3.3"
7776
textwrap = "0.15.0"
7877
zip = { version = "0.6.2", default-features = false, features = ["deflate"] }
78+
async-channel = "1.6.1"
79+
futures-lite = "1.12.0"
80+
tokio-stream = { version = "0.1.9", features = ["fs"] }
81+
reqwest = { version = "0.11.11", features = ["json"] }
7982

8083
[dev-dependencies]
8184
ansi_term = "0.12.0"
82-
async-std = { version = "1", features = ["unstable", "attributes"] }
83-
criterion = { version = "0.3.4", features = ["async_std"] }
85+
criterion = { version = "0.3.4", features = ["async_tokio"] }
8486
futures-lite = "1.12"
8587
log = "0.4"
8688
pretty_env_logger = "0.4"
@@ -132,5 +134,10 @@ harness = false
132134
default = ["vendored"]
133135
internals = []
134136
repl = ["internals", "rustyline", "log", "pretty_env_logger", "ansi_term", "dirs"]
135-
vendored = ["async-native-tls/vendored", "async-smtp/native-tls-vendored", "rusqlite/bundled-sqlcipher-vendored-openssl"]
137+
vendored = [
138+
"async-native-tls/vendored",
139+
"async-smtp/native-tls-vendored",
140+
"rusqlite/bundled-sqlcipher-vendored-openssl",
141+
"reqwest/native-tls-vendored"
142+
]
136143
nightly = ["pgp/nightly"]

benches/contacts.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use async_std::task::block_on;
21
use criterion::{black_box, criterion_group, criterion_main, Criterion};
32
use deltachat::contact::Contact;
43
use deltachat::context::Context;
@@ -27,12 +26,16 @@ async fn address_book_benchmark(n: u32, read_count: u32) {
2726
}
2827

2928
fn criterion_benchmark(c: &mut Criterion) {
29+
let rt = tokio::runtime::Runtime::new().unwrap();
30+
3031
c.bench_function("create 500 contacts", |b| {
31-
b.iter(|| block_on(async { address_book_benchmark(black_box(500), black_box(0)).await }))
32+
b.to_async(&rt)
33+
.iter(|| async { address_book_benchmark(black_box(500), black_box(0)).await })
3234
});
3335

3436
c.bench_function("create 100 contacts and read it 1000 times", |b| {
35-
b.iter(|| block_on(async { address_book_benchmark(black_box(100), black_box(1000)).await }))
37+
b.to_async(&rt)
38+
.iter(|| async { address_book_benchmark(black_box(100), black_box(1000)).await })
3639
});
3740
}
3841

benches/create_account.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use async_std::path::PathBuf;
2-
use async_std::task::block_on;
31
use criterion::{black_box, criterion_group, criterion_main, Criterion};
42
use deltachat::accounts::Accounts;
3+
use std::path::PathBuf;
54
use tempfile::tempdir;
65

76
async fn create_accounts(n: u32) {
@@ -18,7 +17,8 @@ async fn create_accounts(n: u32) {
1817

1918
fn criterion_benchmark(c: &mut Criterion) {
2019
c.bench_function("create 1 account", |b| {
21-
b.iter(|| block_on(async { create_accounts(black_box(1)).await }))
20+
let rt = tokio::runtime::Runtime::new().unwrap();
21+
b.to_async(&rt).iter(|| create_accounts(black_box(1)))
2222
});
2323
}
2424

benches/get_chat_msgs.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use async_std::path::Path;
1+
use std::path::Path;
22

3-
use criterion::async_executor::AsyncStdExecutor;
43
use criterion::{black_box, criterion_group, criterion_main, Criterion};
54

65
use deltachat::chat::{self, ChatId};
@@ -23,7 +22,9 @@ fn criterion_benchmark(c: &mut Criterion) {
2322
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many
2423
// messages, such as your primary account.
2524
if let Ok(path) = std::env::var("DELTACHAT_BENCHMARK_DATABASE") {
26-
let chats: Vec<_> = async_std::task::block_on(async {
25+
let rt = tokio::runtime::Runtime::new().unwrap();
26+
27+
let chats: Vec<_> = rt.block_on(async {
2728
let context = Context::new((&path).into(), 100, Events::new())
2829
.await
2930
.unwrap();
@@ -33,7 +34,7 @@ fn criterion_benchmark(c: &mut Criterion) {
3334
});
3435

3536
c.bench_function("chat::get_chat_msgs (load messages from 10 chats)", |b| {
36-
b.to_async(AsyncStdExecutor)
37+
b.to_async(&rt)
3738
.iter(|| get_chat_msgs_benchmark(black_box(path.as_ref()), black_box(&chats)))
3839
});
3940
} else {

benches/get_chatlist.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use criterion::async_executor::AsyncStdExecutor;
21
use criterion::{black_box, criterion_group, criterion_main, Criterion};
32

43
use deltachat::chatlist::Chatlist;
@@ -13,11 +12,11 @@ fn criterion_benchmark(c: &mut Criterion) {
1312
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many
1413
// messages, such as your primary account.
1514
if let Ok(path) = std::env::var("DELTACHAT_BENCHMARK_DATABASE") {
16-
let context = async_std::task::block_on(async {
17-
Context::new(path.into(), 100, Events::new()).await.unwrap()
18-
});
15+
let rt = tokio::runtime::Runtime::new().unwrap();
16+
let context =
17+
rt.block_on(async { Context::new(path.into(), 100, Events::new()).await.unwrap() });
1918
c.bench_function("chatlist:try_load (Get Chatlist)", |b| {
20-
b.to_async(AsyncStdExecutor)
19+
b.to_async(&rt)
2120
.iter(|| get_chat_list_benchmark(black_box(&context)))
2221
});
2322
} else {

benches/receive_emails.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use async_std::{path::PathBuf, task::block_on};
2-
use criterion::{
3-
async_executor::AsyncStdExecutor, black_box, criterion_group, criterion_main, BatchSize,
4-
Criterion,
5-
};
1+
use std::path::PathBuf;
2+
3+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
64
use deltachat::{
75
config::Config,
86
context::Context,
@@ -51,7 +49,7 @@ async fn create_context() -> Context {
5149
.unwrap()
5250
.join("delta-chat-backup.tar")
5351
.into();
54-
if backup.exists().await {
52+
if backup.exists() {
5553
println!("Importing backup");
5654
imex(&context, ImexMode::ImportBackup, &backup, None)
5755
.await
@@ -74,8 +72,10 @@ async fn create_context() -> Context {
7472
fn criterion_benchmark(c: &mut Criterion) {
7573
let mut group = c.benchmark_group("Receive messages");
7674
group.bench_function("Receive 100 simple text msgs", |b| {
77-
b.to_async(AsyncStdExecutor).iter_batched(
78-
|| block_on(create_context()),
75+
let rt = tokio::runtime::Runtime::new().unwrap();
76+
77+
b.to_async(&rt).iter_batched(
78+
|| rt.block_on(create_context()),
7979
|context| recv_all_emails(black_box(context)),
8080
BatchSize::LargeInput,
8181
);

benches/search_msgs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use async_std::task::block_on;
21
use criterion::{black_box, criterion_group, criterion_main, Criterion};
32
use deltachat::context::Context;
43
use deltachat::Events;
@@ -20,8 +19,10 @@ fn criterion_benchmark(c: &mut Criterion) {
2019
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many
2120
// messages, such as your primary account.
2221
if let Ok(path) = std::env::var("DELTACHAT_BENCHMARK_DATABASE") {
22+
let rt = tokio::runtime::Runtime::new().unwrap();
23+
2324
c.bench_function("search hello", |b| {
24-
b.iter(|| block_on(async { search_benchmark(black_box(&path)).await }))
25+
b.to_async(&rt).iter(|| search_benchmark(black_box(&path)))
2526
});
2627
} else {
2728
println!("env var not set: DELTACHAT_BENCHMARK_DATABASE");

deltachat-ffi/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ libc = "0.2"
2020
human-panic = "1"
2121
num-traits = "0.2"
2222
serde_json = "1.0"
23-
async-std = "1"
23+
tokio = { version = "1", features = ["full"] }
2424
anyhow = "1"
2525
thiserror = "1"
2626
rand = "0.7"
27+
lazy_static = "1.4.0"
2728

2829
[features]
2930
default = ["vendored"]

deltachat-ffi/src/lib.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ extern crate human_panic;
1515
use std::collections::BTreeMap;
1616
use std::convert::TryFrom;
1717
use std::fmt::Write;
18+
use std::future::Future;
1819
use std::ops::Deref;
1920
use std::ptr;
2021
use std::str::FromStr;
2122
use std::time::{Duration, SystemTime};
2223

2324
use anyhow::Context as _;
24-
use async_std::sync::RwLock;
25-
use async_std::task::{block_on, spawn};
2625
use deltachat::qr_code_generator::get_securejoin_qr_svg;
2726
use num_traits::{FromPrimitive, ToPrimitive};
2827
use rand::Rng;
28+
use tokio::runtime::Runtime;
29+
use tokio::sync::RwLock;
30+
use tokio::task::spawn;
2931

3032
use deltachat::chat::{ChatId, ChatVisibility, MuteDuration, ProtectionStatus};
3133
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
@@ -61,6 +63,17 @@ use deltachat::chatlist::Chatlist;
6163
/// Struct representing the deltachat context.
6264
pub type dc_context_t = Context;
6365

66+
lazy_static::lazy_static! {
67+
static ref RT: Runtime = Runtime::new().expect("unable to create tokio runtime");
68+
}
69+
70+
fn block_on<F, T>(fut: F) -> T
71+
where
72+
F: Future<Output = T>,
73+
{
74+
RT.block_on(fut)
75+
}
76+
6477
#[no_mangle]
6578
pub unsafe extern "C" fn dc_context_new(
6679
_os_name: *const libc::c_char,
@@ -4298,7 +4311,7 @@ pub unsafe extern "C" fn dc_accounts_migrate_account(
42984311
block_on(async move {
42994312
let mut accounts = accounts.write().await;
43004313
match accounts
4301-
.migrate_account(async_std::path::PathBuf::from(dbfile))
4314+
.migrate_account(std::path::PathBuf::from(dbfile))
43024315
.await
43034316
{
43044317
Ok(id) => id,

examples/repl/cmdline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
extern crate dirs;
22

3+
use std::path::Path;
34
use std::str::FromStr;
45

56
use anyhow::{bail, ensure, Result};
6-
use async_std::path::Path;
77
use deltachat::chat::{
88
self, Chat, ChatId, ChatItem, ChatVisibility, MuteDuration, ProtectionStatus,
99
};
@@ -492,7 +492,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
492492
let setup_code = create_setup_code(&context);
493493
let file_name = blobdir.join("autocrypt-setup-message.html");
494494
let file_content = render_setup_file(&context, &setup_code).await?;
495-
async_std::fs::write(&file_name, file_content).await?;
495+
tokio::fs::write(&file_name, file_content).await?;
496496
println!(
497497
"Setup message written to: {}\nSetup code: {}",
498498
file_name.display(),

examples/repl/main.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ extern crate deltachat;
99

1010
use std::borrow::Cow::{self, Borrowed, Owned};
1111
use std::io::{self, Write};
12+
use std::path::Path;
1213
use std::process::Command;
1314

1415
use ansi_term::Color;
1516
use anyhow::{bail, Error};
16-
use async_std::path::Path;
1717
use deltachat::chat::ChatId;
1818
use deltachat::config;
1919
use deltachat::context::*;
@@ -30,6 +30,7 @@ use rustyline::validate::Validator;
3030
use rustyline::{
3131
Cmd, CompletionType, Config, Context as RustyContext, EditMode, Editor, Helper, KeyEvent,
3232
};
33+
use tokio::runtime::Handle;
3334

3435
mod cmdline;
3536
use self::cmdline::*;
@@ -301,7 +302,7 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
301302
let context = Context::new(Path::new(&args[1]).to_path_buf(), 0, Events::new()).await?;
302303

303304
let events = context.get_event_emitter();
304-
async_std::task::spawn(async move {
305+
tokio::task::spawn(async move {
305306
while let Some(event) = events.recv().await {
306307
receive_event(event.typ);
307308
}
@@ -316,8 +317,8 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
316317
.output_stream(OutputStreamType::Stdout)
317318
.build();
318319
let mut selected_chat = ChatId::default();
319-
let (reader_s, reader_r) = async_std::channel::bounded(100);
320-
let input_loop = async_std::task::spawn_blocking(move || {
320+
let (reader_s, reader_r) = async_channel::bounded(100);
321+
let input_loop = tokio::task::spawn_blocking(move || {
321322
let h = DcHelper {
322323
completer: FilenameCompleter::new(),
323324
highlighter: MatchingBracketHighlighter::new(),
@@ -339,7 +340,7 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
339340
Ok(line) => {
340341
// TODO: ignore "set mail_pw"
341342
rl.add_history_entry(line.as_str());
342-
async_std::task::block_on(reader_s.send(line)).unwrap();
343+
Handle::current().block_on(reader_s.send(line)).unwrap();
343344
}
344345
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
345346
println!("Exiting...");
@@ -367,7 +368,7 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
367368
}
368369
}
369370
context.stop_io().await;
370-
input_loop.await?;
371+
input_loop.await??;
371372

372373
Ok(())
373374
}
@@ -458,11 +459,12 @@ async fn handle_cmd(
458459
Ok(ExitResult::Continue)
459460
}
460461

461-
fn main() -> Result<(), Error> {
462+
#[tokio::main]
463+
async fn main() -> Result<(), Error> {
462464
let _ = pretty_env_logger::try_init();
463465

464466
let args = std::env::args().collect();
465-
async_std::task::block_on(async move { start(args).await })?;
467+
start(args).await?;
466468

467469
Ok(())
468470
}

examples/simple.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn cb(event: EventType) {
2929
}
3030

3131
/// Run with `RUST_LOG=simple=info cargo run --release --example simple --features repl -- email pw`.
32-
#[async_std::main]
32+
#[tokio::main]
3333
async fn main() {
3434
pretty_env_logger::try_init_timed().ok();
3535

@@ -43,7 +43,7 @@ async fn main() {
4343
log::info!("info: {:#?}", info);
4444

4545
let events = ctx.get_event_emitter();
46-
let events_spawn = async_std::task::spawn(async move {
46+
let events_spawn = tokio::task::spawn(async move {
4747
while let Some(event) = events.recv().await {
4848
cb(event.typ);
4949
}
@@ -80,7 +80,7 @@ async fn main() {
8080
}
8181

8282
// wait for the message to be sent out
83-
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
83+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
8484

8585
log::info!("fetching chats..");
8686
let chats = Chatlist::try_load(&ctx, 0, None, None).await.unwrap();

0 commit comments

Comments
 (0)