返回通用结构的函数的签名是什么?

What would be the signature of a function returning a generic struct?

我正在检查 syslog 箱子,从我的函数中,我想 return 一个记录器对象,我认为它是一个通用结构 syslog::Logger。除了 return 部分和 unwrap().

之外,下面的大部分内容都取自 crate 文档
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::DisplayLogFormatsyslog crate 中定义的特征,定义如下。

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

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

这里唯一的可选参数是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");
}