预期为“&Arc<ProxyPool>”,找到“&ProxyPool”

0 proxy networking reference rust

我尝试从&ProxyPoolto克隆引用&Arc<ProxyPool>,但仍然收到错误,引用的类型仍然是&ProxyPool

pub struct ProxyPool {
    connections: Mutex<Vec<TcpStream>>,
    proxy_addresses: Arc<Vec<SocketAddr>>,
}
pub struct PooledConnection {
    stream: TcpStream,
    pool: Arc<ProxyPool>,
}

impl ProxyPool {
    async fn get_connection(&self) -> Result<PooledConnection> {
        loop {
            if let Some(conn) = {
                let mut connections = self.connections.lock().unwrap();
                connections.pop()
            } {
                return Ok(PooledConnection {
                    stream: conn,
                    pool: Arc::clone(self), // converting &ProxyPool to Arc<ProxyPool>
                });
            }
            // If no connection is available, wait a bit and try again.
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    fn return_connection(&self, conn: TcpStream) {
        let mut connections = self.connections.lock().unwrap();
        connections.push(conn);
    }

    fn get_random_proxy_address(&self) -> SocketAddr {
        let idx = rand::random::<usize>() % self.proxy_addresses.len();
        self.proxy_addresses[idx]
    }
}
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会发生这种情况?

pub struct ProxyPool {
    connections: Mutex<Vec<TcpStream>>,
    proxy_addresses: Arc<Vec<SocketAddr>>,
}
pub struct PooledConnection {
    stream: TcpStream,
    pool: Arc<ProxyPool>,
}

impl ProxyPool {
    async fn get_connection(&self) -> Result<PooledConnection> {
        loop {
            if let Some(conn) = {
                let mut connections = self.connections.lock().unwrap();
                connections.pop()
            } {
                return Ok(PooledConnection {
                    stream: conn,
                    pool: Arc::clone(self), // converting &ProxyPool to Arc<ProxyPool>
                });
            }
            // If no connection is available, wait a bit and try again.
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    fn return_connection(&self, conn: TcpStream) {
        let mut connections = self.connections.lock().unwrap();
        connections.push(conn);
    }

    fn get_random_proxy_address(&self) -> SocketAddr {
        let idx = rand::random::<usize>() % self.proxy_addresses.len();
        self.proxy_addresses[idx]
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试创建一个新的弧和自我引用的克隆

caf*_*e25 5

将任意引用转换为 anArc是不可能的,毕竟所有者可能不是一Arc开始的。您可以通过将接收器更改为Arc<Self>

    async fn get_connection(self: Arc<Self>) -> Result<PooledConnection, ()> {
        loop {
            if let Some(conn) = {
                let mut connections = self.connections.lock().unwrap();
                connections.pop()
            } {
                return Ok(PooledConnection {
                    stream: conn,
                    pool: self,
                });
            }
            // If no connection is available, wait a bit and try again.
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }
Run Code Online (Sandbox Code Playgroud)

并调整呼叫站点。

我更喜欢在这里接管,Arc<Self>因为&Arc<Self>你知道你需要一个拥有的Arc,这样呼叫者可以决定将他们已经拥有的一个传递给你,或者Arc::clone如果他们想保留它或只拥有一个参考,则将其传递给你自己。