I am a newcomer who just learned rust, how do I create RustOpaque <RwLock<Client>>?
mdddj opened this issue · comments
I found this function in the documentation, but I don't know how to create and return it
my code
use std::sync::RwLock;
use aria2_ws::Client;
use crate::frb_generated::{RustAutoOpaque, RustOpaque};
pub enum ConnectError {
Fail,
}
pub async fn connect_aria2_client(url: String, token: Option<&str>) -> RustOpaque<RwLock<Client>> {
let client = Client::connect(&url, token).await.unwrap();
return RustOpaque::new(RwLock::new(client));
}
Hi! Thanks for opening your first issue here! 😄
Hi, usually you do not need to construct it manually. The https://cjycode.com/flutter_rust_bridge/guides/types/arbitrary/rust-auto-opaque is often enough. Do you have special needs for it?
Thanks for your reply, I understand now, I modified it, wrote a simple function, and then ran the command flutter_rust_bridge_codegen generate --watch
, which automatically produced the code encountered an error
mod.ts
pub mod aria;
pub mod simple;
use aria2_ws::Client;
///connect aria2 server
pub async fn connect_aria2_client(url: String, token: Option<&str>) -> Client {
let client = Client::connect(&url, token).await.unwrap();
client
}
simple.rs
#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
pub fn greet(name: String) -> String {
format!("Hello, {name}!")
}
#[flutter_rust_bridge::frb(init)]
pub fn init_app() {
// Default utilities - feel free to customize
flutter_rust_bridge::setup_default_user_utils();
}
dependency version
flutter_rust_bridge = "=2.0.0-dev.28"
Hi, maybe try to pub use aria2_ws::Client;
Thanks, that's solved now.
You are welcome!