Home > Software design >  different function type when using if statement in Rust
different function type when using if statement in Rust

Time:11-17

I'm new in rust and i need to make a small if statement on function option for example

use isahc::{
    HttpClient, 
    config::{
        RedirectPolicy, 
        VersionNegotiation,
        SslOption}, 
    prelude::*
};

use std::{
    time::Duration
};

pub struct http {
    pub timeout: u64    
}





impl http {
    pub fn send(&self) -> HttpClient  {
        let client = 
            HttpClient::builder()
                .version_negotiation(VersionNegotiation::http11())
                .redirect_policy(RedirectPolicy::None)
                .timeout(Duration::from_secs(self.timeout));
                .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
        return client.build().unwrap();
    }

    }

fn main(){
    let req = http{ timeout:"20".parse().unwrap()};
    let test = req.send();
    test.get("https://www.google.com");
}

now in my program the user will give me the options of the request (eg: follow redirects or not ) and this need if statement on these options so i tried to use it on this case but i always get a different function return type

impl http {
    pub fn send(&self) -> HttpClient  {
        let client = 
            HttpClient::builder()
                .version_negotiation(VersionNegotiation::http11())
                .redirect_policy(RedirectPolicy::None)
                .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
        if 1 == 1 {
            client.timeout(Duration::from_secs(self.timeout));
        }
        return client.build().unwrap();
    }

    }
  • Cargo Output
warning: type `http` should have an upper camel case name
  --> src/sender.rs:14:12
   |
14 | pub struct http {
   |            ^^^^ help: convert the identifier to upper camel case: `Http`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

error[E0382]: use of moved value: `client`
  --> src/sender.rs:32:16
   |
24 |         let client = 
   |             ------ move occurs because `client` has type `HttpClientBuilder`, which does not implement the `Copy` trait
...
30 |             client.timeout(Duration::from_secs(self.timeout));
   |             ------ value moved here
31 |         }
32 |         return client.build().unwrap();
   |                ^^^^^^ value used here after move

so what I'm doing wrong ? i tired to change the function type exception but i can't use the function of the class client.get() for example

  • this a clear example in python for explain what i need to do
options : dict = {
  "redirects": False,
  "ssl_path":"~/cer.pem"
}


def send(opts):
   # not real httplib ! 
   r = httplib.get("http://stackoverflow.com")
   if opts.get('redirects') == True:
       r.redirects = True
   if opts.get('cert_path',''):
       r.ssl = opts.get('cert_path')
   return r.send()

def main():
   send(options)

Thanks

CodePudding user response:

Since this is a builder pattern, each function call consumes the builder and returns it back. So you need to capture client from the return value of the timeout function in order to continue using it. Note that you also need to make client mutable.

Something like

impl http {
    pub fn send(&self) -> HttpClient  {
        let mut client = 
            HttpClient::builder()
                .version_negotiation(VersionNegotiation::http11())
                .redirect_policy(RedirectPolicy::None)
                .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS);
        if 1 == 1 {
            client = client.timeout(Duration::from_secs(self.timeout));
        }
        return client.build().unwrap();
    }
}
  • Related