rust - "Sized is not implemented"是什么意思?

标签 rust

我写了下面的代码:

use std::io::{IoResult, Writer};
use std::io::stdio;

fn main() {
    let h = |&: w: &mut Writer| -> IoResult<()> {
        writeln!(w, "foo")
    };
    let _ = h.handle(&mut stdio::stdout());
}

trait Handler<W> where W: Writer {
    fn handle(&self, &mut W) -> IoResult<()>;
}

impl<W, F> Handler<W> for F
where W: Writer, F: Fn(&mut W) -> IoResult<()> {
    fn handle(&self, w: &mut W) -> IoResult<()> { (*self)(w) }
}

然后在我的终端中使用 rustc:

$ rustc writer_handler.rs
writer_handler.rs:8:15: 8:43 error: the trait `core::marker::Sized` is not implemented for the type `std::io::Writer`
writer_handler.rs:8     let _ = h.handle(&mut stdio::stdout());
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
writer_handler.rs:8:15: 8:43 error: the trait `core::marker::Sized` is not implemented for the type `std::io::Writer`
writer_handler.rs:8     let _ = h.handle(&mut stdio::stdout());
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

为什么这个Writer需要实现Sized?在我看来,不需要 Sized。在保持 trait Handler 具有此通用参数的同时我应该做什么?


在 Rust 1.0 中,这段类似的代码会产生同样的问题:

use std::io::{self, Write};

fn main() {
    handle(&mut io::stdout());
}

fn handle(w: &mut Write) -> io::Result<()> {
    handler(w)
}

fn handler<W>(w: &mut W) -> io::Result<()>
where
    W: Write,
{
    writeln!(w, "foo")
}

出现错误:

error[E0277]: the trait bound `std::io::Write: std::marker::Sized` is not satisfied
 --> src/main.rs:8:5
  |
8 |     handler(w)
  |     ^^^^^^^ `std::io::Write` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `std::io::Write`
  = note: required by `handler`

Rust 的更高版本有错误

error[E0277]: the size for values of type `dyn std::io::Write` cannot be known at compilation time
  --> src/main.rs:8:13
   |
8  |     handler(w)
   |             ^ doesn't have a size known at compile-time
...
11 | fn handler<W>(w: &mut W) -> io::Result<()>
   |    ------- - required by this bound in `handler`
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::io::Write`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

最佳答案

Sized trait 相当特殊,特殊到在大多数情况下它是类型参数的默认绑定(bind)。它表示在编译时具有已知固定大小的值,例如 u8 (1 字节)或 &u32 (在具有 64 位指针的平台上为 8 个字节)等。这些值是灵活的:它们可以放在堆栈上并移动到堆上,并且通常按值传递,因为编译器知道它在哪里需要多少空间-无论值(value)如何。

未确定大小的类型受到更多限制,并且类型为 Writer 的值大小不一:它抽象地表示一些实现了 Writer 的未指定类型,不知道实际类型是什么。由于不知道实际类型,因此无法知道大小:一些大类型是 Writer s,一些小类型是。 Writer是特征对象的一个​​例子,目前它只能出现在指针后面的已执行代码中。常见示例包括 &Writer , &mut Writer , 或 Box<Writer> .

这解释了为什么 Sized是默认值:它通常是人们想要的。

无论如何,对于您的代码,这是弹出的,因为您正在使用 handleh ,这是一个 Fn(&mut Writer) -> IoResult<()> .如果我们将其与 F: Fn(&mut W) -> IoResult<()> 进行匹配输入 Handle实现是因为我们发现 W = Writer ,也就是说,我们正在尝试使用 handle使用特征对象 &mut Writer , 不是 &mut W对于一些具体类型 W .这是非法的,因为 W trait 和 impl 中的参数默认为 Sized绑定(bind),如果我们用 ?Sized 手动覆盖它然后一切正常:

use std::io::{IoResult, Writer};
use std::io::stdio;

fn main() {
    let h = |&: w: &mut Writer| -> IoResult<()> {
        writeln!(w, "foo")
    };
    let _ = h.handle(&mut stdio::stdout());
}

trait Handler<W: ?Sized> where W: Writer {
    fn handle(&self, &mut W) -> IoResult<()>;
}

impl<W: ?Sized, F> Handler<W> for F
where W: Writer, F: Fn(&mut W) -> IoResult<()> {
    fn handle(&self, w: &mut W) -> IoResult<()> { (*self)(w) }
}

对于 Rust 1.0 代码:

use std::io::{self, Write};

fn main() {
    handle(&mut io::stdout());
}

fn handle(w: &mut Write) -> io::Result<()> {
    handler(w)
}

fn handler<W: ?Sized>(w: &mut W) -> io::Result<()>
where
    W: Write,
{
    writeln!(w, "foo")
}

我还写了一个blog post about Sized 和一般特征对象,其中有更多细节。

关于rust - "Sized is not implemented"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45148102/

相关文章:

recursion - 这种递归联接如何在Rust中工作

rust - 如何排除在 OS X 上构建文件?

compiler-errors - "cannot move out of borrowed content"用于 Vec 但不是 u64

function - 你如何返回不可复制的类型?

struct - 如何在不出现 "use moved value"错误的情况下绑定(bind)盒装结构的多个字段?

struct - rust -如何使用派生宏来解析结构的元信息?

rust - 借用检查器阻止迭代器在递归函数内调用 for_each

Rust(新手): Read and write (mutable) access to the same underlying array from multiple threads for in-memory database?

syntax - 是否有在相似结构之间移动字段的语法?

generics - 如何迭代实现 Index 和 IntoIterator 的通用集合的索引?