Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

rust - core::marker::Sized not implemented for Foo

I have this fairly straightforward Rust program:

use std::ops::Deref;

trait Foo {
  fn foo(&self);
}

impl Foo for () {
  fn foo(&self) {
    println!("hello world");
  }
}

impl<F> Foo for Box<F> where F: Foo {
  fn foo(&self) {
    let f: &F = self.deref();
    f.foo()
  }
}

fn call_foo<F>(foo: &F) where F: Foo {
  foo.foo()
}

fn main() {
  let foo: Box<Foo> = Box::new(());
  call_foo(&foo);
}

But I get a compilation errors:

$ rustc main.rs
main.rs:26:3: 26:11 error: the trait `core::marker::Sized` is not implemented for the type `Foo` [E0277]
main.rs:26   call_foo(&foo);
             ^~~~~~~~
main.rs:26:3: 26:11 help: run `rustc --explain E0277` to see a detailed explanation
main.rs:26:3: 26:11 note: `Foo` does not have a constant size known at compile-time
main.rs:26   call_foo(&foo);
             ^~~~~~~~
main.rs:26:3: 26:11 note: required by `call_foo`
main.rs:26   call_foo(&foo);
             ^~~~~~~~
error: aborting due to previous error

The error explanation for E0277 seems unrelated. How do I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error message wasn't stellar when this question was asked. The same code (Playground) shows a better error message today:

error[E0277]: the size for values of type `dyn Foo` cannot be known at compilation time
  --> src/main.rs:26:3
   |
26 |   call_foo(&foo);
   |   ^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn Foo`
   = 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 because of the requirements on the impl of `Foo` for `std::boxed::Box<dyn Foo>`
note: required by `call_foo`
  --> src/main.rs:20:1
   |
20 | fn call_foo<F>(foo: &F) where F: Foo {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The error message links to The Rust Programming Language Book, Chapter 19, section 4, "Dynamically sized types and the Sized trait". The problem arises because by default, a generic type parameter is assumed to be Sized. However, a trait object (the dyn Foo in Box<dyn Foo>) does not have a known size. That's acceptable in this case, so we modify the blanket implementation to allow boxes things with an unknown size:

impl<F: ?Sized> Foo for Box<F>
    where F: Foo

Here is the fixed code, updated to the Rust 2018 edition:

trait Foo {
    fn foo(&self);
}

impl Foo for () {
    fn foo(&self) {
        println!("hello world");
    }
}

impl<F: ?Sized> Foo for Box<F>
    where F: Foo
{
    fn foo(&self) {
        (**self).foo()
    }
}

fn call_foo<F>(foo: &F)
    where F: Foo
{
    foo.foo()
}

fn main() {
    let foo: Box<dyn Foo> = Box::new(());
    call_foo(&foo);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...