编写一个返回流流的函数

发布于 2025-01-23 20:59:01 字数 1192 浏览 2 评论 0原文

我正在尝试实现“连接”地址并从rustls

impl Connector {
  ...
  
  fn connect(&self, address: String) -> rustls::Stream<ClientConnection, TcpStream> {
    let target_address = address.parse().unwrap();
    let server_name = rustls::ServerName::from(address).unwrap();
    let mut tcp_stream = TcpStream::connect(target_address).unwrap();
    let mut client_conn = rustls::ClientConnection::new(Arc::new(self.client_config), server_name).unwrap();
    rustls::Stream::new(&mut client_conn, &mut tcp_stream)
  }

这不起作用,因为client_conntcp_stream是借用的,这是不起作用的在功能中。和具有生命周期:

https:httpps:// docs。 rs/rustls/last/rustls/struct.stream.html

#[derive(Debug)]
pub struct Stream<'a, C: 'a + ?Sized, T: 'a + Read + Write + ?Sized> {
    /// Our TLS connection
    pub conn: &'a mut C,

    /// The underlying transport, like a socket
    pub sock: &'a mut T,
}

因此,如何在连接函数中创建此stream对象,并以LifeTime&gt;

I am trying to implement "connect" that takes address and returns the TLS connection from rustls:

impl Connector {
  ...
  
  fn connect(&self, address: String) -> rustls::Stream<ClientConnection, TcpStream> {
    let target_address = address.parse().unwrap();
    let server_name = rustls::ServerName::from(address).unwrap();
    let mut tcp_stream = TcpStream::connect(target_address).unwrap();
    let mut client_conn = rustls::ClientConnection::new(Arc::new(self.client_config), server_name).unwrap();
    rustls::Stream::new(&mut client_conn, &mut tcp_stream)
  }

This does not work because client_conn and tcp_stream were borrowed within the function. And Stream has lifetime:

https://docs.rs/rustls/latest/rustls/struct.Stream.html

#[derive(Debug)]
pub struct Stream<'a, C: 'a + ?Sized, T: 'a + Read + Write + ?Sized> {
    /// Our TLS connection
    pub conn: &'a mut C,

    /// The underlying transport, like a socket
    pub sock: &'a mut T,
}

So, how do I create this Stream object within the connect function and returns accordingly with lifetime>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2025-01-30 20:59:02

我认为是错误的数据类型。据我所知,它没有拥有连接对象的所有权。

在文档中进行快速搜索使我相信您要搜索的是流式

I think Stream is the wrong data type. It does not take ownership of the connection objects as far as I can see.

A quick search in the documentation makes me believe that what you are searching for is StreamOwned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文