为什么我的Result类型别名不满足failure :: Fail特质的约束?

时间:2018-07-28 11:54:57

标签: rust traits lifetime

我正在尝试实现"simple event hooks in Rust"所示的事件挂钩,同时也使用故障包装箱的Error + ErrorKind模式。

这是我的代码的精简版本:

properties
this.cart.forEach((item) => {
  // if cart had only one item it would work
  // I need to somehow create a array that contains `payload object` for each item.
  let payload = {
    name: item.name,
    amount: item.amount,
    price: item.price
  }
  return payload
})

在“ Feedly”方法中,我想使用门户网站:

#[macro_use]
extern crate failure;

use failure::{Backtrace, Context, Error, Fail};
use std::fmt;

#[derive(Debug)]
pub struct PortalError {
    inner: Context<PortalErrorKind>,
}

impl Fail for PortalError {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for PortalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

#[derive(Copy, Clone, PartialEq, Debug, Fail)]
pub enum PortalErrorKind {
    #[fail(display = "Unknown Error")]
    Unknown,
}

//----------------------------------------------------------

pub type PortalResult<T> = Result<PortalError, T>;
pub trait Portal {
    fn get_something(&self) -> PortalResult<Vec<u32>>;
}

//----------------------------------------------------------

pub trait FeedApi<'a> {
    type T: FeedApi<'a>;

    fn new<P: Portal + 'a>(portal: P) -> Result<Self::T, Error>;
}

//----------------------------------------------------------

pub struct Feedly<'a> {
    portal: Box<Portal + 'a>,
}

impl<'a> FeedApi<'a> for Feedly<'a> {
    type T = Feedly<'a>;

    fn new<P: Portal + 'a>(portal: P) -> Result<Self::T, Error> {
        Ok(Feedly {
            portal: Box::new(portal),
        })
    }
}

impl<'a> Feedly<'a> {
    pub fn demo_function(&self) -> Result<(), Error> {
        let _ = self.portal.get_something().context(PortalErrorKind::Unknown)?;
        Ok(())
    }
}

fn main() {
    println!("Hello, world!");
}

但是出现以下错误:

[dependencies]
failure = "0.1.1"

在文档中查看self.portal.get_something().context(PortalErrorKind::Unknown)? 特质具有约束的error[E0599]: no method named `context` found for type `std::result::Result<PortalError, std::vec::Vec<u32>>` in the current scope --> src/main.rs:67:45 | 67 | let _ = self.portal.get_something().context(PortalErrorKind::Unknown)?; | ^^^^^^^ | = note: the method `context` exists but the following trait bounds were not satisfied: `std::result::Result<PortalError, std::vec::Vec<u32>> : failure::Fail` `&std::result::Result<PortalError, std::vec::Vec<u32>> : failure::Fail` `&mut std::result::Result<PortalError, std::vec::Vec<u32>> : failure::Fail` 。并且方法failure::Fail具有绑定的'static

我不确定这里没有满足哪个特征。带框的context既不是Self: Sized也不是Portal,但是返回的结果应该是吧?

这是我第一次在Rust中处理盒子和生命周期。

1 个答案:

答案 0 :(得分:2)

pub type PortalResult<T> = Result<PortalError, T>;

Result有两个类型参数:成功类型和错误类型。您已经移调了它们;你想要的:

pub type PortalResult<T> = Result<T, PortalError>;