gcmodule icon indicating copy to clipboard operation
gcmodule copied to clipboard

Box<dyn T>

Open mio-19 opened this issue 5 years ago • 3 comments

I somehow got this error:

error[E0277]: the size for values of type `dyn Values` cannot be known at compilation time
  --> src/lib.rs:54:49
   |
54 |         self.unsafe_write(THREADED_OBJECT_SPACE.create(Box::new(x))).await;
   |                                                 ^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn Values`
   = 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 `_::_gcmodule::Trace` for `std::boxed::Box<dyn Values>`

https://github.com/The-Lingo/The-Lingo/blob/73f5827b7cc8da1b3ab264cc0d986c917c54abc4/rust/the-lingo/src/lib.rs

mio-19 avatar Apr 03 '20 11:04 mio-19

The error is a bit confusing. It does not explain why dyn Values needs to be sized.

It is actually that Box<dyn Values> needs to implement Trace and in trace_impl.rs only Box<T: Trace + Sized> and Box<dyn Trace> implements Trace. Box<dyn Values> does not implement Trace, because dyn Values is not sized.

I think a solution would be adding a new type:

struct BoxValues(Box<dyn Values>);

Implement Trace on it, then use ThreadedCc<BoxValues>.

quark-zju avatar Apr 03 '20 16:04 quark-zju

https://github.com/quark-zju/gcmodule/blob/a8681f1452736d8ea6e14f352dff42736dee1e2f/src/trace_impls.rs#L89 But it's impl<T: Trace> Trace for Box<T>, not impl<T: Trace + Sized> Trace for Box<T>

mio-19 avatar Apr 04 '20 03:04 mio-19

By default trait bounds are Sized, unless explicitly specified + ?Sized, which allows unsized types.

quark-zju avatar Apr 04 '20 03:04 quark-zju