Rust 相同基础结构的不同返回类型

Rust 相同基础结构的不同返回类型,rust,Rust,我想根据secure变量返回不同的IMAP连接,但如果使用或不使用SSL,则返回不同的类型。Client中的所有功能都由impl-Client实现 有没有更好、更有效的解决方案 use error::*; use imap::client::Client; use openssl::ssl::{SslConnectorBuilder, SslMethod, SslStream}; use std::net::TcpStream; pub enum ConnectionResult {

我想根据
secure
变量返回不同的IMAP连接,但如果使用或不使用SSL,则返回不同的类型。
Client
中的所有功能都由
impl-Client
实现

有没有更好、更有效的解决方案

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))
        }
}

你的方法看起来不错。我的建议是使用:将您的
枚举
封装在
结构中
,并在每个方法中使用
匹配
执行您想要的操作:

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
        }
    }
}

我只想返回一个客户端结构,而不是一个具有不同客户端的枚举
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
        }
    }
}
#![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.*/ }