gpt4 book ai didi

iterator - 将迭代器 Item 类型不匹配解析为具有显式生命周期的指针

转载 作者:行者123 更新时间:2023-11-29 08:27:09 25 4
gpt4 key购买 nike

我正在尝试向 Iterator 添加函数,其中关联类型 Item 是对具有显式生命周期的结构的引用。

当我想修改迭代器状态或返回一个新值时,我没有遇到任何问题,但是当我尝试返回一个新的 Iterator where Item 时是一个具有显式生命周期的引用,编译器会提示。

例子

use std::marker::PhantomData;

/// First, an "Inner" struct to be contained in my custom iterator
pub struct Inner {
text: String,
}

/// Then, the "CustomIterator" in question. Notice that `Item` is `&'a Inner`.
pub struct CustomIterator<'a, I: Iterator<Item = &'a Inner>> {
iter: I,
_marker: PhantomData<&'a i8>,
}

/// Implementing Iterator for CustomIterator so as to define the `next()` function, as you do...
impl<'a, I: Iterator<Item = &'a Inner>> Iterator for CustomIterator<'a, I> {
type Item = &'a Inner;
fn next(&mut self) -> Option<Self::Item> {
println!("Custom next called");
self.iter.next()
}
}

/// Now, creating a custom trait definition called IterateMore that:
/// 1. inherits Iterator
/// 2. includes a default method called `more` which returns a `CustomIterator`
pub trait IterateMore<'a>: Iterator {
type Item;
fn more(self) -> CustomIterator<'a, Self>
where
Self: Sized;
}

/// Implementing `IterateMore` for an iterator of the specific type `Iterator<Item=&'a Inner>`
impl<'a, I: Iterator<Item = &'a Inner>> IterateMore<'a> for I
where
I: Iterator,
{
type Item = &'a Inner;
fn more(self) -> CustomIterator<'a, Self>
where
Self: Sized,
{
CustomIterator {
iter: self,
_marker: PhantomData,
}
}
}

fn main() {
let inner = Inner {
text: "Hello world".to_string(),
};
let inners = vec![&inner];
inners.iter().more().next();
}

(在 repl.it 上查看)

错误

error[E0271]: type mismatch resolving `<Self as std::iter::Iterator>::Item == &'a Inner`
--> src/main.rs:28:5
|
28 | / fn more(self) -> CustomIterator<'a, Self>
29 | | where
30 | | Self: Sized;
| |____________________^ expected associated type, found reference
|
= note: expected type `<Self as std::iter::Iterator>::Item`
found type `&'a Inner`
= note: required by `CustomIterator`

为什么 Item 没有在这里解析?这有点令人沮丧,因为如果我尝试将 &'a Inner 设置为特征定义中的默认 Item 类型,编译器也会提示,说:

error: associated type defaults are unstable (see issue #29661)

如何解决这个问题或采取不同的方式?

最佳答案

我不清楚您为什么要将包装迭代器限制为某种自定义类型(假设您每次使用该类型时仍然必须写下限制,尽管这可能会改变)。但也许你的“真实”next函数做了一些有趣的事情。

  • PhantomDatawhere 中使用生命周期时,似乎不再需要(再)“使用”生命周期-子句。
  • IterateMore不应该有 Item关联类型,给定 Iterator已经有了。 (如果您真的需要一种新类型,请选择不同的名称)
  • 作为IterateMore使用 CustomIterator键入它需要重复要求,在本例中为 Iterator<Item = &'a Inner> (这就是类型不匹配错误的原因);这与说 type Item = &'a Inner 不同在特征定义中。

Playground

/// an "Inner" struct to be contained in my custom iterator
pub struct Inner {
text: String,
}

pub struct CustomIterator<'a, I>
where
I: Iterator<Item = &'a Inner>,
{
iter: I,
}

impl<'a, I> Iterator for CustomIterator<'a, I>
where
I: Iterator<Item = &'a Inner>,
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
println!("Custom next called");
self.iter.next()
}
}

pub trait IterateMore<'a>: Iterator<Item = &'a Inner> + Sized {
fn more(self) -> CustomIterator<'a, Self>;
}

impl<'a, I> IterateMore<'a> for I
where
I: Iterator<Item = &'a Inner>,
{
fn more(self) -> CustomIterator<'a, Self> {
CustomIterator { iter: self }
}
}

fn main() {
let inner = Inner {
text: "Hello world".to_string(),
};
let inners = vec![inner];
inners.iter().more().next();
}

你也可以像这样删除所有地方的类型限制(并且只将它添加回你实际需要/想要的地方):

Playground

pub struct CustomIterator<I> {
iter: I,
}

impl<I> Iterator for CustomIterator<I>
where
I: Iterator,
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
println!("Custom next called");
self.iter.next()
}
}

pub trait IterateMore: Iterator + Sized {
fn more(self) -> CustomIterator<Self>;
}

impl<I> IterateMore for I
where
I: Iterator,
{
fn more(self) -> CustomIterator<Self>
{
CustomIterator { iter: self }
}
}

fn main() {
let inners = vec!["Hello world".to_string()];
inners.iter().more().next();
}

关于iterator - 将迭代器 Item 类型不匹配解析为具有显式生命周期的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47329967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com