- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
rust quickcheck documentation注意对于任何实现 Arbitrary
They must also be sendable and static since every test is run in its own thread using thread::Builder::spawn, which requires the Send + 'static bounds.
如果我需要为包含引用的结构生成数据,我该怎么做呢?例如:
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(quickcheck_macros))]
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
use quickcheck::{Arbitrary,Gen};
#[allow(dead_code)]
#[derive(Debug,Clone)]
pub struct C<'a> {
s: &'a str,
b: bool
}
#[cfg(test)]
impl<'a> Arbitrary for C<'a> {
fn arbitrary<G: Gen>(g: &mut G) -> C<'a> {
let s = g.gen::<&str>();
C{s: s, b: (s.len() > 0)}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[quickcheck]
fn len_checks_out(c: C) -> bool {
(c.s.len() > 0) == c.b
}
}
失败了
cargo test
Compiling qcq v0.1.0 (file:///Users/blt/projects/us/troutwine/qcquestion)
src/lib.rs:18:10: 18:19 error: the type `C<'a>` does not fulfill the required lifetime [E0477]
src/lib.rs:18 impl<'a> Arbitrary for C<'a> {
^~~~~~~~~
note: type must outlive the static lifetime
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
error: Could not compile `qcq`.
这是一个有点人为的例子,但它与原始问题的精神是一样的。生命周期注解可以解决,但正在测试中。
最佳答案
您不能这样做有两个原因。一、Arbitrary
有一个 'static
绑定(bind),这意味着实现 Arbitrary
的类型可能没有引用,除非它们的生命周期是 'static
.这确保实例不会引用它们不“拥有”的对象。
其次,为了返回一个C<'a>
其中 'a
是 'static
以外的任何东西,大多数时候你还需要一个包含具有相同生命周期参数的引用的参数(这并不总是必要的,例如,当使用生命周期参数的字段可以稍后初始化时,但这里不适用)。因此,您需要一个像这样定义的函数:
fn arbitrary<'a, G: Gen>(g: &'a mut G) -> C<'a> {
let s = g.gen::<&str>();
C { s: s, b: (s.len() > 0) }
}
(注意 'a
是在函数上定义的,而不是在 impl
上定义的。)
这有两个大问题:
Arbitrary::arbitrary()
返回 Self
.这意味着该函数必须返回 Arbitrary
所在的类型。实现。然而,在这里,C<'a>
取决于在函数上定义的生命周期参数; C<'a>
不可能与 impl
相同目标,因为该类型不能使用该生命周期参数。Rng::gen()
只需调用 Rand::rand()
,它也返回 Self
,因此遇到与 Arbitrary::arbitrary()
相同的问题.另外,Rand
未针对 &str
实现(甚至是 String
)。那你能做什么?而不是存储 &str
在你的结构中,你应该存储一个 String
.这使得你的结构 'static
, 你可以使用 Arbitrary
的实现对于 String
生成测试值。
但是如果您不想使用 String
怎么办?在您的实际应用程序代码中?您可以通过接受 &str
来使您的结构通用或 String
.标准库中有两个特性可以帮助您做到这一点: AsRef
和 Borrow
.这是一个使用 Borrow
的示例:
use std::borrow::Borrow;
#[derive(Debug, Clone)]
pub struct C<S: Borrow<str>> {
s: S,
b: bool
}
现在,您可以使用 C<&str>
或 C<String>
,取决于需要什么。显然,您无法实现 Arbitrary
对于 C<&str>
, 但您可以为 C<String>
实现它.实际上,为什么不为所有实现 Arbitrary
的类型实现它呢? ?
impl<S: Borrow<str> + Arbitrary> Arbitrary for C<S> {
fn arbitrary<G: Gen>(g: &mut G) -> C<S> {
let s: S = Arbitrary::arbitrary(g);
let b = s.borrow().len() > 0;
C { s: s, b: b }
}
}
关于rust - 如何创建包含引用的结构的 Quickcheck Arbitrary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390808/
我正在使用 QuickCheck 1,我有以下数据类型: data A = ... instance Arbitrary A where ... data B = ... instance Arbit
背景: Vim 有很多很棒的教程和“技巧”页面,但是非常困难的一件事是找到关于如何做一些可以在自己熟悉的文本编辑器 IDE 中轻松完成的任意事情的具体说明。因此,我要求逐步说明我将如何在 Vim 中执
我不确定正确的名称是什么,到目前为止我理解使用可变模板参数的概念(例如,与 std::initializer_list 相比)。 所以让我们假设有任意数量的类 k 和任意数量的参数 i 这取决于每个类
是否有任何 TypeScript 解决方案可以帮助我拥有“无限”数量的泛型类型,当然不必键入所有这些类型? 示例如下: type Args> = { component: T, presetProps
我有简单的元组(例如从数据库中读取),我不知道元素的数量和内容。例如。(String, Int, Int)或 (String, Float, String, Int) . 我想编写一个通用函数,它将采
rust quickcheck documentation注意对于任何实现 Arbitrary 的类型 They must also be sendable and static since ever
我有以下数据和函数 data Foo = A | B deriving (Show) foolist :: Maybe Foo -> [Foo] foolist Nothing = [A] fool
我尝试计算 math.exp(9500) 但遇到了 OverflowError: math range error(大约是 6.3e4125)。来自 this question似乎是由于 float
我想知道为什么需要任意,因为自动化属性测试需要属性定义,例如 val prop = forAll(v: T => check that property holds for v) 和值 v 生成器。用
@SuppressWarnings("unchecked") public static final Ordering ARBITRARY_ORDERING = (Ordering)Ordering.
我在替换“任意”定界符之间的文本文件部分时遇到一些问题。 这是我想要做的:我有如下标记: // [ADX START] ANYTHING // [ADX END] ANYTHING /*
据我所知,在 HTML5 中组成标签名称是完全合法的,并且它们可以正常使用 CSS 样式和嵌套等。 当然,我的任意标记名称对不理解它们的浏览器没有任何影响,但它大大提高了我的代码的可读性,从而更易于维
这个问题在这里已经有了答案: Typescript Function with Generic Return Type (2 个回答) 4 个月前关闭。 以下代码抛出错误: 'T' could be
请参阅以下 34 个字符的 ASCII 字符串。我试图在这个字符串上有 3 个 MD5 哈希匹配。 8AC905DD4AB2038E5F7EABEAE792AC41BC [A-F0-9]{32}仅匹配
假设我有一个充满 javascript 文件的目录: . +-- my_dir +-- apple.js +-- banana.js +-- main.js 子目录中的每个文件都包含一
找不到如何提出关于 Steve Pomeroy 发布的“使用 Android 播放任意音调”的问题,所以开始了一篇 here . 是否有任何代码需要添加到 xml 文件中? 无法让 sim 发出声音。
SO_RCVTIMEO 根本没有在我的输出函数中创建超时条件。我正在使用 ARQ 滑动窗口协议(protocol)设计文件传输服务。为了保持一切有序,我从基本的“停等”开始。 这是我对 timeval
我将在当前工作的网站上创建竞赛。每个比赛都不会相同,并且可能有不同数量的输入字段,用户必须输入这些字段才能成为比赛的一部分,例如。 比赛 1 可能只需要一个名字 比赛 2 可能需要名字、姓氏和电子邮件
我有一个在 Android OpenGL-ES 上渲染的点云。我可以正确翻译它(我认为)但是当我旋转它时,我无法让它按预期工作。我希望它围绕点云的中心旋转(我有这个 3D 点),但我不知道该怎么做。
是否可以为某些 PostgreSQL 函数定义任意返回类型? 这个想法是,根据调用的不同,可能会返回不同的输出。 例如,假设我们有: TypeA : (name, email) TypeB : (na
我是一名优秀的程序员,十分优秀!