norman784 / gaiku

3d agnostic framework (game engine) terrain engine
40 stars 1 forks source link

Issues with with_size #39

Closed QuantumEntangledAndy closed 3 years ago

QuantumEntangledAndy commented 3 years ago

Perhaps my rust foo is not up to par but I am having issues with the method

fn with_size(width: u16, height: u16, depth: u16) -> Self {
    unimplemented!();
}

I want to add a trait that implements the Chunkify trait rather than an struct that implements the trait so as to reduce repeated code.

However the Sizable trait requires a method that returns Self. But for a trait Self has an unknown size

  --> src/mechanics/chunkable.rs:31:58
   |
31 |     fn with_size(width: u16, height: u16, depth: u16) -> Self {
   |                                                          ^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `(dyn Chunkable + 'static)`
   = note: the return type of a function must have a statically known size

I cannot implement the Sized trait as once implemented we lose access to &self required for the width etc

error[E0038]: the trait `SizedChunkable` cannot be made into an object
  --> src/mechanics/chunkable.rs:27:14
   |
27 |     fn width(&self) -> u16 {
   |              ^^^^^ `SizedChunkable` cannot be made into an object
   |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/refer
ence/items/traits.html#object-safety>
  --> src/mechanics/chunkable.rs:12:39
   |
12 | pub trait SizedChunkable: Chunkable + Sized {}
   |           --------------              ^^^^^ ...because it requires `Self: Sized`
   |           |
   |           this trait cannot be made into an object...

Could we seperate out the with_size method into its own trait AND for that trait implement the Sized trait for it like the Default trait does?

QuantumEntangledAndy commented 3 years ago

So googling around to improve my rust foo it seems that what I am trying to so is not possible anyways :(

QuantumEntangledAndy commented 3 years ago

Apparently rust dosen't do traits of traits like this so I will close this as a learning lesson for my rust.. sorry about this