Skip to content

Commit 9f0fdb2

Browse files
fix repl and ffi
1 parent 6c677d5 commit 9f0fdb2

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deltachat-ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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;
@@ -24,8 +25,9 @@ use anyhow::Context as _;
2425
use deltachat::qr_code_generator::get_securejoin_qr_svg;
2526
use num_traits::{FromPrimitive, ToPrimitive};
2627
use rand::Rng;
28+
use tokio::runtime::Runtime;
2729
use tokio::sync::RwLock;
28-
use tokio::task::{block_on, spawn};
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,

examples/repl/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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::*;
@@ -316,7 +317,7 @@ 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) = tokio::channel::bounded(100);
320+
let (reader_s, reader_r) = async_channel::bounded(100);
320321
let input_loop = tokio::task::spawn_blocking(move || {
321322
let h = DcHelper {
322323
completer: FilenameCompleter::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-
tokio::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-
tokio::task::block_on(async move { start(args).await })?;
467+
start(args).await?;
466468

467469
Ok(())
468470
}

teset

236 KB
Binary file not shown.

0 commit comments

Comments
 (0)