• Rust 生成器


    1. 生成器类一般与原始类有相同的属性
    type ms = u32;
    
    #[derive(Clone)]
    struct TLSCert {
        key: String,
        cert: String,
    }
    
    pub struct Server {
        host: String,
        port: u16,
        tls: Option<TLSCert>,
        hot_reload: bool,
        timeout: ms,
    }
    
    
    pub struct ServerBuilder {
        host: String,
        port: u16,
        tls: Option<TLSCert>,
        hot_reload: bool,
        timeout: ms,
    }
    
    1. 生成器类构造器new方法的入参为最基本必要参数,其他属性附上默认值
    impl ServerBuilder {
        fn new(host: String, port: u16) -> ServerBuilder {
            ServerBuilder {
                host,
                port,
                tls: None,
                hot_reload: false,
                timeout: 5000,
            }
        }
    }
    
    1. 添加生成器类其他属性赋值方法
    impl ServerBuilder {
        fn tls(&mut self, tls: TLSCert) -> &mut Self {
            self.tls = Some(tls);
            self
        }
    
        fn hot_reload(&mut self, hot_reload: bool) -> &mut Self {
            self.hot_reload = hot_reload;
            self
        }
    
        fn timeout(&mut self, timeout: ms) -> &mut Self {
            self.timeout = timeout;
            self
        }
    }
    
    1. 生成器类build方法返回原始类
    impl ServerBuilder {
        fn build(&mut self) -> Server {
            Server {
                host: self.host.clone(),
                port: self.port,
                tls: self.tls.clone(),
                hot_reload: self.hot_reload,
                timeout: self.timeout,
            }
        }
    }
    
    1. 生成器类的使用
    fn main() {
        let host = "localhost".to_owned();
        let port = 8080;
        let cert = TLSCert {
            key: "...".to_owned(),
            cert: "...".to_owned(),
        };
    
        // Basic server
        let basic_server = ServerBuilder::new(host.clone(), port).build();
    
        // Server with TLS
        let tls_server = ServerBuilder::new(host.clone(), port)
            .tls(cert.clone())
            .build();
    
        // Fully configured server
        let server = ServerBuilder::new(host.clone(), port)
            .tls(cert.clone())
            .hot_reload(true)
            .timeout(5000)
            .build();
    }
    

    参考

  • 相关阅读:
    【BZOJ4868】期末考试 [三分][贪心]
    【BZOJ4880】排名的战争 [暴力]
    【BZOJ1449&&2895】球队预算 [费用流]
    【BZOJ1221】【HNOI2001】软件开发 [费用流]
    【BZOJ4837】LRU算法 [模拟]
    Leetcode题解(30)
    Leetcode题解(29)
    Leetcode题解(28)
    Leetcode题解(27)
    Leetcode题解(26)
  • 原文地址:https://www.cnblogs.com/yangqionggo/p/16120976.html
Copyright © 2020-2023  润新知