I'm trying to create an enum on which you can await, which then opens a socket and authenticates, returning the TcpStream for further use. The problem is that the data inside the enum variants prevents the *self dereference. I'm guessing this is due to the TcpStream, but I have no idea how to fix this. Some guidance and options would be greatly appreciated .
enum SocketState{
Off(String, String),
Connecting(String, String),
WriteHello(TcpStream, Vec<u8>),
ReadHelloResponse(TcpStream),
Ready(Result<TcpStream,anyhow::Error>),
}
impl Future for SocketState{
type Output = Result<TcpStream,anyhow::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use SocketState::*;
loop{
match *self{
Off(imei_path, addr_string) => {
match read_to_string(imei_path){
Ok(imei_str) =>{
*self = Connecting(imei_str, addr_string);
return Poll::Pending;
}
Err(e) => {
*self = Ready(Err(Error::new(e)));
return Poll::Pending;
},
};
},
Connecting(imei, addr) => {
let connection_future = TcpStream::connect(addr);
tokio::pin!(connection_future);
match connection_future.poll(cx){
Poll::Ready(Ok(stream)) => {
let mut imei_hello:Vec<u8> = vec![0x00, 0x0F];
for imei_char in imei.chars(){
imei_hello.push(imei_char as u8);
}
*self = WriteHello(stream, imei_hello);
return Poll::Pending;
},
Poll::Ready(Err(e)) => {
*self = Ready(Err(Error::new(e)));
return Poll::Pending;
},
Poll::Pending => return Poll::Pending,
}
},
WriteHello(mut stream, hello_message) => {
let hello_write_future = stream.write(&hello_message);
tokio::pin!(hello_write_future);
match hello_write_future.poll(cx){
Poll::Ready(Ok(_)) => {
*self = ReadHelloResponse(stream);
return Poll::Pending;
}
Poll::Ready(Err(e)) => {
*self = Ready(Err(Error::new(e)));
return Poll::Pending;
}
Poll::Pending => return Poll::Pending,
}
},
ReadHelloResponse(mut stream ) => {
let mut server_response = Vec::<u8>::with_capacity(1);
let response_read_future = stream.read_buf(&mut server_response);
tokio::pin!(response_read_future);
match response_read_future.poll(cx){
Poll::Ready(Ok(_)) => {
match server_response[0]{
0x01 => {
*self = Ready(Ok(stream));
return Poll::Pending;
},
unexpected_code => {
*self = Ready(Err(Error::msg(format!("Unexpected server response {unexpected_code}"))));
}
}
},
Poll::Ready(Err(e)) => {
*self = Ready(Err(Error::new(e)));
return Poll::Pending;
},
Poll::Pending => return Poll::Pending,
}
}
Ready(connection_result) => {
return Poll::Ready(connection_result);
},
}
}
}
}
error[E0507]: cannot move out of dereference of `Pin<&mut SocketState>`
--> src/main.rs:42:19
|
42 | match *self{
| ^^^^^
43 | Off(imei_path, addr_string) => {
| --------- ----------- ...and here
| |
| data moved here
...
55 | Connecting(imei, addr) => {
| ---- ---- ...and here
| |
| ...and here
...
74 | WriteHello(mut stream, hello_message) => {
| ---------- ------------- ...and here
| |
| ...and here
...
89 | ReadHelloResponse(mut stream ) => {
| ---------- ...and here
...
112 | Ready(connection_result) => {
| ----------------- ...and here
|
= note: move occurs because these variables have types that don't implement the `Copy` trait
help: consider removing the dereference here
|
42 - match *self{
42 + match self{
|