What I want to do?:
- I want to send an HTTP request using the value of the Websocket response.(Examples are simplified.)
- making market making bot(crypto).
- If possible, I'd like to aggregate abstracted methods in the client.
What are you having trouble with?:
- Can I call struct method from closure?
- Or, Is the idea wrong in the first place?
Example:
use anyhow::Result;
use async_trait::async_trait;
use core::future::Future;
#[async_trait]
trait Client<'a> {
async fn new(s: &'a str) -> Self;
async fn callback<C, F>(&mut self, mut cb: C) -> Result<()>
where
C: FnMut(String) -> F + Send,
F: Future<Output = ()> + Send;
async fn hello(&self) -> Result<()>;
}
#[derive(Debug)]
struct A<'a> {
a: &'a str,
}
#[async_trait]
impl<'a> Client<'a> for A<'a> {
async fn new(a: &'a str) -> Self {
Self { a }
}
async fn callback<C, F>(&mut self, mut cb: C) -> Result<()>
where
C: FnMut(String) -> F + Send,
F: Future<Output = ()> + Send,
{
cb("callback".to_string()).await;
Ok(())
}
async fn hello(&self) -> Result<()> {
println!("\nhello from same struct method:\n{}", "hello world");
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut client: A = Client::new("hello").await;
println!("\nclient struct is:\n{:?}", client);
client
.callback(|res| async move {
println!("\ncallback response is:\n{:?}", res);
// TODO: How to call method as "client" struct?
client.hello().await;
})
.await;
Ok(())
}
Error:
error[E0507]: cannot move out of `client`, a captured variable in an `FnMut` closure
--> src/main.rs:45:25
|
41 | let mut client: A = Client::new("hello").await;
| ---------- captured outer variable
...
45 | .callback(|res| async move {
| ___________________-----_^
| | |
| | captured by this `FnMut` closure
46 | | println!("\ncallback response is:\n{:?}", res);
47 | |
48 | | // TODO: How to call method as "client" struct?
49 | | client.hello().await;
| | ------
| | |
| | variable moved due to use in generator
| | move occurs because `client` has type `A<'_>`, which does not implement the `Copy` trait
50 | | })
| |_________^ move out of `client` occurs here
error[E0505]: cannot move out of `client` because it is borrowed
--> src/main.rs:45:19
|
44 | / client
45 | | .callback(|res| async move {
| | ^^^^^ move out of `client` occurs here
46 | | println!("\ncallback response is:\n{:?}", res);
47 | |
48 | | // TODO: How to call method as "client" struct?
49 | | client.hello().await;
| | ------ move occurs due to use in closure
50 | | })
| |__________- borrow of `client` occurs here