Hi,
I am trying to access a TcpStream object by passing a mutable reference and then dereferencing it, but the following error comes up:
cannot move out of *tcp_ph
which is behind a mutable reference
move occurs because *tcp_ph
has type std::net::TcpStream
, which does not implement the Copy
trait
help: consider borrowing here: &*tcp_ph
Below is the code:
extern crate clap;
use bytes::Bytes;
use std::convert::TryInto;
use std::io::{self, Write};
use std::net::Shutdown;
use std::net::TcpStream;
use std::time::SystemTime;
use hdrhistogram::Histogram;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
struct LoopStruct {
tcp: TcpStream,
current_pointer: usize,
hist: Histogram<u64>,
}
fn main() -> io::Result<()> {
let addr = String::from("127.0.0.1:8000");
let loop_max = 100;
let xnow = SystemTime::now();
// tcp and tcp2 are the connection pool
let mut tcp = TcpStream::connect(&addr);
let tcp2 = TcpStream::connect(&addr);
let mut flag = 1;
let mut tcp_timer_x = SystemTime::now();
match tcp {
Err(e) => {
println!("Cannot connect {:?}", e);
}
Ok(ref mut tcp_ph) => {
let current_pointer: usize = 0;
let hist: Histogram<u64> = Histogram::<u64>::new(2).unwrap();
//below line doesn't doesn't work
let tcp = *tcp_ph;
let mut t1 = LoopStruct {
tcp,
current_pointer,
hist,
};
let mut loop_count = 1;
loop {
let mut i = 0;
loop{
let tcp_timer_y: u64 = tcp_timer_x.elapsed().unwrap().as_secs().try_into().unwrap();
let mut count =1;
if tcp_timer_y > 5{
println!("Switching now -------- $$$$$$$$$$$$$");
t1.tcp.shutdown(Shutdown::Both);
println!("shutdown old conn -------- $$$$$$$$$$$$$");
if flag ==1 {
//t1.tcp = *(&mut tcp2).unwrap();
t1.tcp = tcp2.unwrap();
//should reconnect here
flag=2;
}
else {
t1.tcp = tcp;
flag=1;
}
//t1.tcp = curr_tcp_conn;
count= count+1;
println!("started new conn -------- $$$$$$$$$$$$$ {:?}", count);
tcp_timer_x = SystemTime::now();
}
}
}
loop_count += 1;
if loop_count > loop_max {
let xnow1: u64 = xnow.elapsed().unwrap().as_millis().try_into().unwrap();
t1.tcp
.shutdown(Shutdown::Both)
.expect("shutdown call failed");
}
}
}
Ok(())
}