我正在尝试为元素的任何序列实现特征,以使其适用于向量,数组和切片。到目前为止,我已经尝试了几种方法,但是我无法编译其中的任何一种方法:(
我有这个特征,一个使用它的函数,以及一个实现该特征的基本数据类型:
trait Hitable {
fn hit(&self, val: f64) -> bool;
}
fn check_hit<T: Hitable>(world: &T) -> bool {
world.hit(1.0)
}
struct Obj(f64);
impl Hitable for Obj {
fn hit(&self, val: f64) -> bool {
self.0 > val
}
}
我希望能够为Obj
的序列实现该特征。
如果我仅将其限制为向量,则效果很好:
impl<T> Hitable for Vec<T>
where
T: Hitable,
{
fn hit(&self, val: f64) -> bool {
self.iter().any(|h| h.hit(val))
}
}
fn main() {
let v = vec![Obj(2.0), Obj(3.0)];
println!("{}", check_hit(&v));
}
但是我想使其更通用,以便它适用于数组和切片;我该怎么办?
我尝试了以下四次尝试:
// It's not clear how to call it:
// vec.iter().hit(...) does not compile
// vec.into_iter().hit(...) does not compile
//
impl<T, U> Hitable for T
where
T: Iterator<Item = U>,
U: Hitable,
{
fn hit(&self, val: f64) -> bool {
self.any(|h| h.hit(val))
}
}
// Does not compile as well:
//
// self.into_iter().any(|h| h.hit(val))
// ^^^^ cannot move out of borrowed content
//
impl<T, U> Hitable for T
where
T: IntoIterator<Item = U>,
U: Hitable,
{
fn hit(&self, val: f64) -> bool {
self.into_iter().any(|h| h.hit(val))
}
}
// This usage doesn't compile:
// let v = vec![Obj(2.0), Obj(3.0)];
// println!("{}", check_hit(&v));
//
// It says that Hitable is not implemented for vectors.
// When I convert vector to slice, i.e. &v[..], complains about
// unknown size in compilation time.
impl<T> Hitable for [T]
where
T: Hitable,
{
fn hit(&self, val: f64) -> bool {
self.iter().any(|h| h.hit(val))
}
}
// let v = vec![Obj(2.0), Obj(3.0)];
// println!("{}", check_hit(&v.iter()));
//
// does not compile:
// println!("{}", check_hit(&v.iter()));
// ^^^^^^^^^ `&Obj` is not an iterator
//
impl<T, U> Hitable for T
where
T: Iterator<Item = U> + Clone,
U: Hitable,
{
fn hit(&self, val: f64) -> bool {
self.clone().any(|h| h.hit(val))
}
}
答案 0 :(得分:1)
Iterator
这行不通,因为迭代器需要可变才能前进,但是您的特征需要&self
。
IntoIterator
我将更改特征以按值获取self
,然后仅将其实现为对Obj
的引用。这也允许为实现IntoIterator
的任何类型实现它:
trait Hitable {
fn hit(self, val: f64) -> bool;
}
fn check_hit<T: Hitable>(world: T) -> bool {
world.hit(1.0)
}
struct Obj(f64);
impl Hitable for &'_ Obj {
fn hit(self, val: f64) -> bool {
self.0 > val
}
}
impl<I> Hitable for I
where
I: IntoIterator,
I::Item: Hitable,
{
fn hit(self, val: f64) -> bool {
self.into_iter().any(|h| h.hit(val))
}
}
fn main() {
let o = Obj(2.0);
let v = vec![Obj(2.0), Obj(3.0)];
println!("{}", check_hit(&o));
println!("{}", check_hit(&v));
}
另请参阅:
我发现阅读整个错误消息,而不仅仅是一行摘要,可以帮助您:
error[E0277]: the size for values of type `[Obj]` cannot be known at compilation time
--> src/main.rs:28:20
|
28 | println!("{}", check_hit(&v[..]));
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[Obj]`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
note: required by `check_hit`
--> src/main.rs:5:1
|
5 | fn check_hit<T: Hitable>(world: &T) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
具体来说,该位:注意:check_hit
必需— check_hit
要求T
为Sized
。取消该限制后,该版本即可运行:
fn check_hit<T: Hitable + ?Sized>(world: &T) -> bool {
// ^~~~~~~~
world.hit(1.0)
}
另请参阅: