为任何实现特征的东西实现特征?
Implementing a trait for anything that implements a trait?
我想为任何实现 Into<u64>
的特性实现特性 Add。我试过了,
impl<T> Add<Into<T>> for Sequence {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
todo!();
}
}
这给了我两个错误,
doesn't have a size known at compile-time
= help: the trait `Sized` is not implemented for `(dyn Into<T> + 'static)`
并且,
`Into` cannot be made into an object
= note: the trait cannot be made into an object because it requires `Self: Sized`
= 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/reference/items/traits.html#object-safety>
我试过用
替换它
impl<T> Add<dyn Into<T> + 'static + Sized> for Sequence {
但我仍然遇到错误
doesn't have a size known at compile-time
= help: the trait `Sized` is not implemented for `(dyn Into<T> + 'static)`
note: required by a bound in `Add`
这里的正确语法是什么,我要去兔子洞了?
您想要的语法是,
impl<T: Into<u64>> Add<T> for Sequence {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
...
}
这将为所有实施 Into<u64>
的 T
实施 Add<T>
I opened up a bug to get the compiler to provide more guidance
我想为任何实现 Into<u64>
的特性实现特性 Add。我试过了,
impl<T> Add<Into<T>> for Sequence {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
todo!();
}
}
这给了我两个错误,
doesn't have a size known at compile-time
= help: the trait `Sized` is not implemented for `(dyn Into<T> + 'static)`
并且,
`Into` cannot be made into an object
= note: the trait cannot be made into an object because it requires `Self: Sized`
= 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/reference/items/traits.html#object-safety>
我试过用
替换它impl<T> Add<dyn Into<T> + 'static + Sized> for Sequence {
但我仍然遇到错误
doesn't have a size known at compile-time
= help: the trait `Sized` is not implemented for `(dyn Into<T> + 'static)`
note: required by a bound in `Add`
这里的正确语法是什么,我要去兔子洞了?
您想要的语法是,
impl<T: Into<u64>> Add<T> for Sequence {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
...
}
这将为所有实施 Into<u64>
T
实施 Add<T>
I opened up a bug to get the compiler to provide more guidance