返回泛型结构的函数的签名是什么?

时间:2019-07-05 13:25:35

标签: generics struct rust syslog

我正在遍历syslog板条箱,并且从我的函数中,我想返回一个记录器对象,我认为它是通用结构syslog::Logger的对象。下面的大部分内容都取自板条箱文档,除了返回部分和unwrap()之外。

extern crate syslog;
use syslog::{Facility, Error, Formatter3164};

pub fn get_logger() -> syslog::Logger{
    let formatter = Formatter3164 {
        facility: Facility::LOG_USER,
        hostname: None,
        process: "myprogram".into(),
        pid: 0,
    };
    syslog::unix(formatter).unwrap()
}

但是我看到的是

pub fn get_logger() -> syslog::Logger{
                       ^^^^^^^^^^^^^^ expected 3 type arguments

syslog::Logger结构的签名是

pub struct Logger<Backend: Write, T, Formatter: LogFormat<T>>

并且syslog::unix的签名是

pub fn unix<U: Display, F: Clone+LogFormat<U>>(formatter: F) -> Result<Logger<LoggerBackend, U, F>> {

其中Displaystd::fmt::Display,而LogFormatsyslog板条箱中定义的特征,其定义如下。

pub trait LogFormat<T> {
  fn format<W: Write>(&self, w: &mut W, severity: Severity, message: T)   -> Result<()>;

  fn emerg<W: Write>(&mut self, w: &mut W, message: T)   -> Result<()> {
    self.format(w, Severity::LOG_EMERG, message)
  }

  fn alert<W: Write>(&mut self, w: &mut W, message: T)   -> Result<()> {
    self.format(w, Severity::LOG_ALERT, message)
  }

  fn crit<W: Write>(&mut self, w: &mut W, message: T)    -> Result<()> {
    self.format(w, Severity::LOG_CRIT, message)
  }

  fn err<W: Write>(&mut self, w: &mut W, message: T)     -> Result<()> {
    self.format(w, Severity::LOG_ERR, message)
  }

  fn warning<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
    self.format(w, Severity::LOG_WARNING, message)
  }

  fn notice<W: Write>(&mut self, w: &mut W, message: T)  -> Result<()> {
    self.format(w, Severity::LOG_NOTICE, message)
  }

  fn info<W: Write>(&mut self, w: &mut W, message: T)    -> Result<()> {
    self.format(w, Severity::LOG_INFO, message)
  }

  fn debug<W: Write>(&mut self, w: &mut W, message: T)   -> Result<()> {
    self.format(w, Severity::LOG_DEBUG, message)
  }
}

这是一个通用结构,如果我想从函数中返回记录器对象,我应该返回的类型签名是什么?

1 个答案:

答案 0 :(得分:3)

这里唯一可选的参数是T。这是登录时所需的消息类型。例如。在下面的示例中,日志消息类型为&str

let mut logger = get_logger();

logger.info("This is a log message");

您可以选择get_logger内的类型,并在其签名中签名,也可以使get_logger通用,然后由呼叫站点决定。 第一种方法是这样的:

extern crate syslog;
use syslog::{Facility, Error, Formatter3164, LoggerBackend};

fn get_logger<'a>() -> syslog::Logger<LoggerBackend, &'a str, Formatter3164>{
    let formatter = Formatter3164 {
        facility: Facility::LOG_USER,
        hostname: None,
        process: "myprogram".into(),
        pid: 0,
    };
    syslog::unix(formatter).unwrap()
}

pub fn main(){
  let mut logger = get_logger();

  logger.info("This is a log message");
}

通用方法如下:

extern crate syslog;
use syslog::{Facility, Error, Formatter3164, LoggerBackend};

pub fn get_logger<T: std::fmt::Display>() -> syslog::Logger<LoggerBackend, T, Formatter3164>{
    let formatter = Formatter3164 {
        facility: Facility::LOG_USER,
        hostname: None,
        process: "myprogram".into(),
        pid: 0,
    };
    syslog::unix(formatter).unwrap()
}

pub fn main(){
  let mut logger = get_logger();

  logger.info("This is a log message");
}