Я пытаюсь привести структуру, которая реализует черту к объекту черты той же черты. Проблема в том, что объект черты должен быть заключен в Arc
, и я не могу реализовать черту Into
для Arc
. Из-за того, что следующий код не компилируется:
use std::sync::Arc;
trait Foo { }
struct Bar {}
impl Foo for Bar { }
impl Into<Arc<dyn Foo>> for Arc<Bar> {
fn into(self) -> Arc<dyn Foo> { self }
}
fn bar_to_foo(bar: Arc<Bar>) -> Arc<dyn Foo> {
bar.into()
}
Это происходит со следующим сообщением компилятора:
error[E0119]: conflicting implementations of trait `std::convert::Into<std::sync::Arc<(dyn Foo + 'static)>>` for type `std::sync::Arc<Bar>`:
--> src/lib.rs:9:1
|
9 | impl Into<Arc<dyn Foo>> for Arc<Bar> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T, U> std::convert::Into<U> for T
where U: std::convert::From<T>;
= note: upstream crates may add a new impl of trait `std::convert::From<std::sync::Arc<Bar>>` for type `std::sync::Arc<(dyn Foo + 'static)>` in future versions
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> src/lib.rs:9:1
|
9 | impl Into<Arc<dyn Foo>> for Arc<Bar> {
| ^^^^^------------------^^^^^--------
| | | |
| | | `std::sync::Arc` is not defined in the current crate
| | `std::sync::Arc` is not defined in the current crate
| impl doesn't use only types from inside the current crate
|
= note: define and implement a trait or new type instead
error: aborting due to 2 previous errors
Однако я не могу сделать следующее:
use std::sync::Arc;
trait Foo { }
struct Bar {}
impl Foo for Bar { }
impl Into<dyn Foo> for Bar {
fn into(self) -> dyn Foo { self }
}
fn bar_to_foo(bar: Arc<Bar>) -> Arc<dyn Foo> {
bar.into()
}
Потому что это приводит к:
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
--> src/lib.rs:9:6
|
9 | impl Into<dyn Foo> for Bar {
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn Foo + 'static)`
Я, вероятно, упускаю что-то очевидное, но любая помощь будет принята с благодарностью!