Well, I finally got it to compile how I wanted, and it actually works ! (took me about 2 days to understand how it works and to realize that it didn't work the first time because I forgot to pass the tcp stream as a reference)
pub enum Type<'a> {
Sftp {
session: &'a ssh2::Session,
sftp: ssh2::Sftp<'a>,
}
}
impl<'a> Type<'a> {
pub fn new_sftp(sess: &'a mut ssh2::Session, username: &'a str, password: &'a str, tcp: &TcpStream) -> Type<'a> {
sess.handshake(tcp).expect("No handshake, no like");
sess.userauth_password(username, password).expect("Wrong username / password");
Type::Sftp {
session: sess,
sftp: sess.sftp().unwrap(),
}
}
}
(This is an Enum because there will be more that just Sftp. Ftps will soon be added too. I thought it would be great to have everything at the same place. Then I make private functions to manipulate files and get the list of directories, call them in my action_handler
function (that gets a json from the client) and I'm all good.)
This almost works like I want. The only thing is that I need to create the tcp stream and ssh2 session outside.
This will be good enough, I can now say that this issue is solved. Sftp still takes .3 seconds to connect but, since I've been working on rust-ftp to make it work, this won't be an issue.