Rust具有相同基本结构的不同返回类型

时间:2017-08-28 10:57:40

标签: rust

我想根据secure变量返回不同的IMAP连接,但如果我们使用SSL,imap::client::Client会返回不同的类型。 Client中的所有功能都由impl<T: Read + Write> Client<T>实现。

是否有更好,更有效的解决方案?

use error::*;
use imap::client::Client;
use openssl::ssl::{SslConnectorBuilder, SslMethod, SslStream};
use std::net::TcpStream;

pub enum ConnectionResult {
    Normal(Client<TcpStream>),
    Secure(Client<SslStream<TcpStream>>),
}

/// Mail account
#[derive(Debug, Deserialize)]
pub struct Account {
    pub username: String,
    pub password: String,
    pub domain: String,
    pub port: u16,
    pub secure: bool,
}

impl Account {
    pub fn connect(&self) -> Result<ConnectionResult> {
        if self.secure {
            let ssl_connector = SslConnectorBuilder::new(SslMethod::tls())
                .chain_err(|| "fail with ssl")?
                .build();
            let mut imap_socket = Client::secure_connect(
                (self.domain.as_str(), self.port),
                &self.domain,
                ssl_connector,
            );
            imap_socket
                .login(&self.username, &self.password)
                .chain_err(|| "fail when login")?;
            Ok(ConnectionResult::Secure(imap_socket))
        } else {
            let mut imap_socket = Client::connect((self.domain.as_str(), self.port))?;
            imap_socket
                .login(&self.username, &self.password)
                .chain_err(|| "fail when login")?;
            Ok(ConnectionResult::Normal(imap_socket))
        }
}

我只想返回Client结构,而不是包含不同Client s的枚举:

pub fn connect<T: Read + Write>(&self) -> Result<Client<T>> {
    // But this won't work
}

1 个答案:

答案 0 :(得分:2)

你的做法似乎并不坏。我的建议是使用composition:将enum封装在struct中,并使用match在每个方法中执行您想要的操作:

enum ConnectionResult { // not pub because intern implementation
    Normal(Client<TcpStream>),
    Secure(Client<SslStream<TcpStream>>),
}

pub struct MyClient {
    connection_result: ConnectionResult,
    // other fields
}

impl MyClient {
    pub fn do_something(&self) {
        match connection_result {
            Normal(client) => // do something with normal client
            Secure(client) => // do something with secure client
        }
    }
}

从用户的角度来看,两个客户端之间没有区别。

如果您不想要此解决方案,可以使用每晚-> impl功能:

#![feature(conservative_impl_trait)]

trait MyClient {
    // the methods you need
}

struct NormalClient {
    client: Client<TcpStream>,
    /*etc.*/
}
struct SecureClient {
    client: Client<SslStream<TcpStream>>,
    /*etc.*/
}

impl MyClient for NormalClient { /*etc.*/ }
impl MyClient for SecureClient { /*etc.*/ }

fn get_client() -> impl MyClient { /*etc.*/ }