简体   繁体   English

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

[英]Why is the failure::Fail trait bound not satisfied by my Result type alias?

I'm trying to implement event hooks as demonstrated by "simple event hooks in Rust" while also using the Error + ErrorKind pattern of the failure crate. 我正在尝试实现事件钩子,如“ Rust中的简单事件钩子”所示,同时还使用了Error包装箱的Error + ErrorKind模式。

This is a stripped down version of my code: 这是我的代码的精简版:

#[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"

In a method of 'Feedly' I want to use the portal: 在“ Feedly”方法中,我想使用门户:

self.portal.get_something().context(PortalErrorKind::Unknown)?

But I get the following error: 但是我收到以下错误:

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`

Looking through the docs the failure::Fail trait has a bound 'static . 浏览文档, failure::Fail特征具有绑定的'static And the method context has a bound Self: Sized . 并且方法context具有绑定的Self: Sized

I'm not sure which trait is not satisfied here. 我不确定在这里不满足哪个特质。 The boxed Portal is neither Sized nor 'static , but the returned result should be, right? 装箱的Portal既不是Sized也不是'static ,但是返回的结果应该是吧?

This is the first time I'm handling boxes and lifetimes in Rust. 这是我第一次在Rust中处理盒子和生命周期。

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

Result has two type parameters: the success type and the error type. Result具有两个类型参数:成功类型和错误类型。 You have transposed them; 您已经移调了它们; you want: 你要:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用关联类型时不满足特征边界 - Trait bound is not satisfied when using associated type 编译器强迫我实现特征方法,但是对于我的类型,方法上的`Self`特征约束永远不会满足 - Compiler forces me to implement trait method but the `Self` trait bound on method is never satisfied for my type Rust中不满足特征限制 - The trait bound is not satisfied in Rust 特质界限不满足 - The trait bound is not satisfied 扩展失败类型的结果时,为什么会得到“该方法存在,但以下特征范围不满足”的信息? - Why do I get “the method exists but the following trait bounds were not satisfied” when extending Result for failure types? 为什么我会因为缺少类型注释而收到错误“trait bound FromStr is not满足”? - Why do I get the error “trait bound FromStr is not satisfied” because of a missing type annotation? 特质约束T:从 <Result<T, Error> &gt;不满意 - Trait bound T: From<Result<T, Error>> is not satisfied Result&lt;(), Box&lt;(dyn SomeTrait + &#39;static)&gt;&gt; 不满足 Trait Bound - Trait Bound is not satisfied for Result<(), Box<(dyn SomeTrait + 'static)>> 即使类型已派生(序列化),也不满足特征绑定序列化 - Trait bound Serialize is not satisfied even though the type has derive(Serialize) 为什么? 运算符报错误“特征绑定NoneError:错误不满足”? - Why does the ? operator report the error "the trait bound NoneError: Error is not satisfied"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM