【问题标题】:Why is it impossible to dereference a boxed iterator trait object? [duplicate]为什么不可能取消引用装箱的迭代器特征对象? [复制]
【发布时间】:2019-06-25 15:02:33
【问题描述】:

我有以下三个例子:

fn iterate_with_iterator<T: std::fmt::Display, I: Iterator<Item = T>>(iter: I) {
    for x in iter {
         println!("{}", x);
    }
}

fn iterate_with_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
    for x in iter {
         println!("{}", x);
    }
}

fn iterate_with_deref_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
    let diter = *iter;
    for x in diter {
         println!("{}", x);
    }
}

Playground

编译iterate_with_deref_boxed_iterator时出现如下错误:

error[E0277]: the size for values of type `dyn std::iter::Iterator<Item = T>` cannot be known at compilation time
  --> src/main.rs:14:9
   |
14 |     let diter = *iter;
   |         ^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::iter::Iterator<Item = T>`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `dyn std::iter::Iterator<Item = T>` cannot be known at compilation time
  --> src/main.rs:15:14
   |
15 |     for x in diter {
   |              ^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::iter::Iterator<Item = T>`
   = 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 `std::iter::IntoIterator::into_iter`

为什么取消引用会破坏编译?

【问题讨论】:

    标签: rust iterator


    【解决方案1】:

    不可能将任何特征对象(您的类型的dyn Iterator 部分)从指针(&amp;dyn IteratorBox&lt;dyn Iterator&gt;)后面移出。这是因为特征对象是无大小的——编译器没有关于所指对象的具体大小的信息,因此不允许将其移动到必须在编译时知道其大小的堆栈中。这就是您的错误消息(“std::marker::Sized 没有为 dyn std::iter::Iterator&lt;Item = T&gt; 实现特征”)的含义。

    Rust 目前不支持运行时变量堆栈分配,因此不允许将像 trait 对象这样的未调整大小的值移动到堆栈中。

    如果确实需要对迭代器进行拆箱,可以做一个泛型函数

    fn deref_boxed<I: Iterator<Item = T>, T: Display>(iter: Box<I>) {
        let mut i = *iter;
        for item in i {
            println!("{}", item);
        }
    }
    

    但您应该能够在不拆箱的情况下遍历参数:

    fn iter_boxed<'a, T: Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
        for item in iter {
            println!("{}", x);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多