If you try to use Box<dyn Converter> directly you will be told that Converter is not object safe because of static_common_func. When are you using static_common_func? Does it need to be called from the different implementers? If so, you could restrict it to only implementers that have a size, but then I believe that static function will get materialized for every implementer. Given that this is a static function, you might want to move it to a different trait or turn it into a function, but it all depends on what you want to accomplish and what tradeoffs you're ok with.
I want to create two converter structures - which will process data in different ways.
But these structures have common functions - e.g. for processing arguments from clap lib.
That doesn't compile at all due to several syntax errors. Can you show an example that gives just this error?
error[E0161]: cannot move a value of type dyn Converter: the size of dyn Converter cannot be statically determined
If the example has several syntax errors, you can't rely on type errors to be correct. The compiler might completely mis-interpret your code, and give unrelated errors.
Compiling poly_test v0.1.0 (/home/mhanusek/work/VersionControl/poly_test)
error[E0277]: `dyn Converter` cannot be sent between threads safely
--> src/main.rs:54:13
|
54 | tokio::spawn(async move {
| ^^^^^^^^^^^^ `dyn Converter` cannot be sent between threads safely
|
::: /home/mhanusek/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/task/spawn.rs:127:21
|
127 | T: Future + Send + 'static,
| ---- required by this bound in `tokio::task::spawn::spawn`
|
= help: the trait `std::marker::Send` is not implemented for `dyn Converter`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<dyn Converter>`
= note: required because it appears within the type `std::boxed::Box<dyn Converter>`
= note: required because it appears within the type `[static generator@src/main.rs:54:37: 56:14 converter:std::boxed::Box<dyn Converter> _]`
= note: required because it appears within the type `std::future::from_generator::GenFuture<[static generator@src/main.rs:54:37: 56:14 converter:std::boxed::Box<dyn Converter> _]>`
= note: required because it appears within the type `impl std::future::Future`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
When you wrote Box<dyn Converter>, you told the compiler to forget that the contents were Send, so it couldn't make use of that fact anymore. You can tell it that it should be Send with Box<dyn Converter + Send>.
Thx for reply.
One more error.
I added a function:
async fn run(mut self)
to Converter trait.
error[E0161]: cannot move a value of type dyn Converter + std::marker::Send: the size of dyn Converter + std::marker::Send cannot be statically determined
--> src/main.rs:1078:9
|
1078 | converter.run().await;
| ^^^^^^^^^