Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the trait `core::kinds::Sized` is not implemented for the type `<generic #0>` in rust?

Tags:

rust

I expected this to work:

trait Task<R, E> {
  fn run(&self) -> Result<R, E>;
}

mod test {

  use super::Task;

  struct Foo;
  impl<uint, uint> Task<uint, uint> for Foo {
    fn run(&self) -> Result<uint, uint> {
      return Err(0);
    }
  }

  fn can_have_task_trait() {
    Foo;
  }
}

fn main() {
  test::can_have_task_trait();
}

...but it does not:

<anon>:10:3: 14:4 error: the trait `core::kinds::Sized` is not implemented for the type `<generic #0>`
<anon>:10   impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11     fn run(&self) -> Result<uint, uint> {
<anon>:12       return Err(0);
<anon>:13     }
<anon>:14   }
<anon>:10:3: 14:4 note: the trait `core::kinds::Sized` must be implemented because it is required by `Task`
<anon>:10   impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11     fn run(&self) -> Result<uint, uint> {
<anon>:12       return Err(0);
<anon>:13     }
<anon>:14   }
<anon>:10:3: 14:4 error: the trait `core::kinds::Sized` is not implemented for the type `<generic #1>`
<anon>:10   impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11     fn run(&self) -> Result<uint, uint> {
<anon>:12       return Err(0);
<anon>:13     }
<anon>:14   }
<anon>:10:3: 14:4 note: the trait `core::kinds::Sized` must be implemented because it is required by `Task`
<anon>:10   impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11     fn run(&self) -> Result<uint, uint> {
<anon>:12       return Err(0);
<anon>:13     }
<anon>:14   }
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Program ended.

playpen: http://is.gd/kxDt0P

So, what's going on?

I have no idea what this error means.

Is it that I'm using Result and that requires that U, V are not Sized? In which case, why are they Sized? I didn't write:

Task<Sized? R, Sized? E>

Are all generics now dynamically sized or something? (in which case, what does Sized? even mean?)

What's going on?

like image 282
Doug Avatar asked Mar 18 '23 09:03

Doug


1 Answers

You just need to remove the <uint, uint> in impl<uint, uint>, because type parameters go there and not concrete types:

impl Task<uint, uint> for Foo { ... }

I think the errors you're getting are the compiler getting confused over unused type parameters. It also occurs with this super-reduced version:

trait Tr {}
impl<X> Tr for () {}
like image 55
ben Avatar answered Apr 22 '23 04:04

ben