rust - 将范围作为结构域

标签 rust traits

有多种Range类型。一些Range类型实现Iterator。我想将实现Range的所有Iterator类型作为结构字段。
这是我的方法:

pub trait RangeBoundsExt<T: PartialOrd<T>>: Iterator<Item = T> {
    // some methods
}

impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> {}

impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}

impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}

pub struct Foo<T> {
    range: Box<dyn RangeBoundsExt<T>>
}
Playground
但我收到此错误:
  Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
 --> src/lib.rs:7:24
  |
7 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> {}
  |                        ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
  |
  = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::Range<T>`
help: consider further restricting this bound
  |
7 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::Range<T> {}
  |                       ^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
 --> src/lib.rs:9:24
  |
9 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}
  |                        ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
  |
  = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::RangeFrom<T>`
help: consider further restricting this bound
  |
9 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}
  |                       ^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
  --> src/lib.rs:11:24
   |
11 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}
   |                        ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
   |
   = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::RangeInclusive<T>`
help: consider further restricting this bound
   |
11 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}
   |                       ^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

最佳答案

Range不保证Iterator实现。如果该类型实现Step,则仅提供一个。同样,RangeTo不能保证缺少Iterator实现。默认情况下,它只是不提供一个。要解决您的错误,您只需要要求该范围的迭代器定义即可:

// note the additional 'where' requirement
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> where
    std::ops::Range<T>: Iterator<Item = T>
{
}

impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> where
    std::ops::RangeFrom<T>: Iterator<Item = T>
{
}

impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> where
    std::ops::RangeInclusive<T>: Iterator<Item = T>
{
}

关于rust - 将范围作为结构域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64739239/

相关文章:

rust - 如何在Rust中创建Match宏?

iterator - 将迭代器与(固定大小的)数组进行比较

generics - 为什么我不能在带有类型参数的特征上添加一揽子暗示?

Scala:导入对象 Foo._ 和 trait Bar 的简写

scala - 有没有办法从 Scala 中的实例中删除特征?

rust - 有没有办法将多个派生别名作为一个派生?

scala - 为什么一个类不能用相同签名的方法扩展特征?

rust - 如何将借用的内容移至关闭连接

arrays - [_; 中的数组长度 N 允许使用什么表达式? N]?

rust - 如何将环境变量中的配置值反序列化为Vec?