Coder Social home page Coder Social logo

Too strict lifetimes about tokio-socks HOT 5 CLOSED

apatrushev avatar apatrushev commented on September 27, 2024
Too strict lifetimes

from tokio-socks.

Comments (5)

sticnarf avatar sticnarf commented on September 27, 2024 1

@apatrushev clone your destination please

Thank you for the quick reply. I tried almost all imaginable variants of clone/copy/slice/etc. Nothing helps.

UPD: I found the solution (&destination[..]).to_string(), not the simple one, but I will try to understand whats going on myself. Thank you for your help!

A simpler solution is: (notice the to_owned)

tokio_socks::tcp::Socks5Stream::connect(proxy, destination.to_owned())

from tokio-socks.

sticnarf avatar sticnarf commented on September 27, 2024

@apatrushev Sorry, my fault. Now I've uploaded a new version to solve the problem. You can simply pass in an owned String and get a SocksStream which has 'static lifetime.

from tokio-socks.

apatrushev avatar apatrushev commented on September 27, 2024

Unfortunately it does not help to find a solution for the code in second example. tokio-socks still claims about lifetimes:

use futures::future::{Future, IntoFuture};
use std::{
    error::Error,
    io::{Error as IoError, ErrorKind},
    net::SocketAddr,
};

type ConnectFuture = Box<dyn Future<Item = tokio::net::TcpStream, Error = IoError> + Send>;

fn connect(address: &String) -> Result<ConnectFuture, Box<dyn Error>> {
    let parts: Vec<_> = address.split("#").collect();
    let (proxy, destination) = (parts[0], parts[1]);
    let proxy = proxy.parse::<SocketAddr>()?;
    Ok(Box::new(
        tokio_socks::tcp::Socks5Stream::connect(proxy, destination)
            .into_future()
            .flatten()
            .map(|tcp| {
                println!("Connected to proxy");
                tcp.into_inner()
            })
            .map_err(|_| IoError::new(ErrorKind::Other, "socks problem")),
    ))
}

fn main() {
    let address = std::env::args().nth(1).unwrap();
    tokio::run(connect(&address).unwrap().map(|_| ()).map_err(|_| ()));
}

Error:

   Compiling tokio-socks v0.1.3 ([...]/tokio-socks)
error[E0621]: explicit lifetime required in the type of `address`
  --> examples/lifetime.rs:14:5
   |
10 |   fn connect(address: &String) -> Result<ConnectFuture, Box<dyn Error>> {
   |                       ------- help: add explicit lifetime `'static` to the type of `address`: `&'static std::string::String`
...
14 | /     Ok(Box::new(
15 | |         tokio_socks::tcp::Socks5Stream::connect(proxy, destination)
16 | |             .into_future()
17 | |             .flatten()
...  |
22 | |             .map_err(|_| IoError::new(ErrorKind::Other, "socks problem")),
23 | |     ))
   | |______^ lifetime `'static` required

error: aborting due to previous error

For more information about this error, try `rustc --explain E0621`.
error: Could not compile `tokio-socks`.

To learn more, run the command again with --verbose.

from tokio-socks.

sticnarf avatar sticnarf commented on September 27, 2024

@apatrushev clone your destination please

from tokio-socks.

apatrushev avatar apatrushev commented on September 27, 2024

@apatrushev clone your destination please

Thank you for the quick reply. I tried almost all imaginable variants of clone/copy/slice/etc. Nothing helps.

UPD: I found the solution (&destination[..]).to_string(), not the simple one, but I will try to understand whats going on myself. Thank you for your help!

from tokio-socks.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.