- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试添加一个更简洁的 failure
crate 的 .with_context(|e| format!("foo: {}", e))
到我的代码。像这样playground :
use failure::{Context, Fail, ResultExt}; // 0.1.5
/// Extension methods for failure `Result`.
pub trait ResultContext<T, E> {
/// Wraps the error type in a context type generated by looking at the
/// error value. This is very similar to `with_context` but much more
/// concise.
fn ctx(self, s: &str) -> Result<T, Context<String>>;
}
impl<T, E> ResultContext<T, E> for Result<T, E>
where
E: Fail,
{
fn ctx(self, s: &str) -> Result<T, Context<String>> {
self.map_err(|failure| {
let context = format!("{}: {}", s, failure);
failure.context(context)
})
}
}
pub fn foo() -> Result<i32, failure::Error> {
Ok(5i32)
}
pub fn main() -> Result<(), failure::Error> {
// This works.
let _ = foo().with_context(|_| "foo".to_string())?;
// This doesn't.
foo().ctx("foo")?
}
我收到以下错误:
error[E0599]: no method named `ctx` found for type `std::result::Result<i32, failure::error::Error>` in the current scope
--> src/main.rs:31:11
|
31 | foo().ctx("foo")?
| ^^^
|
= note: the method `ctx` exists but the following trait bounds were not satisfied:
`std::result::Result<i32, failure::error::Error> : ResultContext<_, _>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `ctx`, perhaps you need to implement it:
candidate #1: `ResultContext`
我不知道为什么。我或多或少地复制了现有的 with_context
代码。
最佳答案
正如编译器告诉您的那样,Result<i32, failure::error::Error>
没有实现 ResultContext<_, _>
.您已经为您的实现添加了一个界限:
where
E: Fail,
但是 failure::Error
没有实现 failure::Fail
:
use failure; // 0.1.5
fn is_fail<F: failure::Fail>() {}
pub fn main() {
is_fail::<failure::Error>();
}
error[E0277]: the trait bound `failure::error::Error: std::error::Error` is not satisfied
--> src/main.rs:6:5
|
6 | is_fail::<failure::Error>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `failure::error::Error`
|
= note: required because of the requirements on the impl of `failure::Fail` for `failure::error::Error`
note: required by `is_fail`
--> src/main.rs:3:1
|
3 | fn is_fail<F: failure::Fail>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
您需要更改边界或类型。
关于error-handling - 为什么在扩展失败类型的结果时得到 "the method exists but the following trait bounds were not satisfied"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56580031/
我正在尝试创建一个类似 Twitter 的关注/关注系统。表格设计是这样的 - leader_id | following_id | is_mutual 1 |
尊敬的数据库专家/程序员: 我有一个mysql表,里面有用户信息,比如 id user_id name etc. 1 userA 2
我正在尝试设置我的 User GraphQL 模型以具有要查询的 followers 和 following 属性。但是,我无法在 Sequelize 中建立关系。我正在尝试使用 Follower 模
在我的网站上,我有一个类似 twitter 的概念,我有一个具有以下结构的用户连接表, ============================================= ==========
下面列出的区别是什么: 最佳答案 首先,你知道Meta Robots Tags吗?Meta Robots Tags 是告诉蜘蛛或爬虫要抓取或索引哪些页面。 跟随意味着:页面将被抓取。 No
我正在做一些项目,我在考虑关注者和像推特这样的关注系统。我在数据库中有成员表。我怎样才能做关注者系统。逻辑是什么?我做了类似的事情:我创建了名称跟随的新表,我有 3 行。这些行是 id、user_id
这是用于 C 代码检测。我正在尝试标记没有中断的 case 语句。当 break 语句之前有多行时,树的层次结构如下所示。这是 C 中的示例: switch (x) { case 1: i
大家好,我正在使用 MySQL 和 PHP 制作关注者/被关注系统,所以我想获取我关注的用户的所有视频并按 ID 描述排序。 数据库架构: 例如,如果我的 user_id = 1,并且如果我正在关注
我正在尝试根据#following #followers 找到一个计算推特用户“重要性”的方程式 我要考虑的事情: 1. #followers/#following越多,他越重要。 2. 在 20/2
这是我得到的错误 undefined method `followed_users?' for # 提取的源代码(大约第 3 行): 1: 2: 3: 4: 5: 6:
我有一个用户模型,可以创建关系以拥有关注者并关注其他人,我从 Rails 教程中学到的一切都非常有效。一件事是,我正在尝试通过添加能够看到其他人的关注者并能够拥有关注/关注按钮选项的选项来将其推进到下
我正在尝试通过 Twitter Javascript Api(请参阅 here )检查登录用户是否在 Twitter 上关注我。如果没有,我将显示一个关注按钮。现在我似乎无法找出命令 isFollow
我希望能够使用 iOS 5 的 Twitter API 将所有用户关注者和关注者用户名放入 NSDictionary... 不过我遇到了障碍。我不知道如何使用 Twitter API 来执行此操作..
希望在域之后但在其他任何内容之前获取值。 因此 URL 的示例是: www.domain.com/following#2 我只想从该 url 中获取后面的单词。 目前有这个可以重定向用户并在任何片段等
我有下表: 关系 id、follower_id、followee_id、状态 用户 id、姓名、电子邮件 我想查找正在关注或被特定用户关注的所有用户。 这是我到目前为止所拥有的,但速度非常慢: SEL
我是 state Management 的新手,正在使用 provider package 。产生这些类型的异常有多少种不同的原因,我该如何解决,此异常是在 didChangeDependencies
所以我在这里尝试制作一个类似 Instagram 的应用程序,但仅适用于汽车,我尝试单击“关注”和“关注者”来查看我关注的人和关注我的人,但我不确定为什么会有NPE吗... 我成功地可以关注用户,用户
我是 state Management 的新手,正在使用 provider package 。产生这些类型的异常有多少种不同的原因,我该如何解决,此异常是在 didChangeDependencies
我讨厌发布“这应该很容易,我错过了什么”问题,但是......这应该很容易,我错过了什么?我在这个测试代码上没有得到匹配,我不明白为什么。模式是(或应该是)“任何三个数字,后跟任何三个相同的数字,后跟
我正在研究 Chapter 11在 Hartl 的教程中,试图让用户 Controller 中的未登录用户访问关注者和关注页面通过。我遇到了一个错误,Rails 会提示未定义的 admin? 方法,尽
我是一名优秀的程序员,十分优秀!