Skip to content

smol: Implement smol in tor-rtcompat

Due to the issue of async-std being discontinued and to be replaced with smol: #1901

I made an attempt to already implement smol in tor-rtcompat.

To implement smol in Arti a lot more needs to be done, but it seems like I am already able to create a TorClient using smol as runtime.

Example usage:

Code
use macro_rules_attribute::apply;
use smol_macros::main;
use anyhow::{Result, Context};
use arti_client::{config::TorClientConfig, TorClient};
use futures::io::{AsyncReadExt, AsyncWriteExt};

fn assert_equal_types<T>(_: &T, _: &T) {}

#[apply(main!)]
async fn main() -> Result<()> {
   
    let tor_client_config = TorClientConfig::default();
    let rt =
        tor_rtcompat::smol::SmolNativeTlsRuntime::create().expect("Failed to create SmolNativeTlsRuntime runtime.");

    let tor_client = TorClient::with_runtime(rt.clone())
        .config(tor_client_config)
        .create_unbootstrapped()
        .context("Error creating Tor client")?;

    tor_client
        .bootstrap()
        .await
        .context("Tor bootstrap failed")?;

    // Test if TorClient is configured to use `SmolNativeTlsRuntime` as runtime.
    assert_equal_types(&tor_client.runtime().clone(), &tor_rtcompat::smol::SmolNativeTlsRuntime::current().expect("Failed to get current SmolNativeTlsRuntime runtime."));

    let mut stream = tor_client.connect(("torproject.org", 80)).await?;
    stream
    .write_all(b"GET / HTTP/1.1\r\nHost: torproject.org\r\nConnection: close\r\n\r\n")
    .await?;

    stream.flush().await?;
    let mut buf = Vec::new();
    stream.read_to_end(&mut buf).await?;
    println!("{}", String::from_utf8_lossy(&buf));

    Ok(())
}
[package]
name = "test2"
version = "0.1.0"
edition = "2021"

[dependencies]
tor-rtcompat = { path = "../../crates/tor-rtcompat", default-features = false, features = ["smol"], version = "0.30.0" }
arti-client = { path = "../../crates/arti-client", features = ["full", "smol"] }
futures = "0.3.14"
anyhow = { version = "1.0.23"  }
smol-macros = "0.1.1"
macro_rules_attribute = "0.2.0"

This MR is WIP!

Edited by Niel Duysters

Merge request reports

Loading