我有这两个结构
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Vin {
data: [u8; 17],
}
impl Vin {
pub fn new(data: [u8; 17]) -> Vin {
Vin { data }
}
}
impl Into<[u8;17]> for Vin {
fn into(self) -> [u8;17] {
self.data
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Vcu {
data: [u8; 11],
}
impl Vcu {
pub fn new(data: [u8; 11]) -> Vcu {
Vcu { data }
}
pub fn as_string_utf8(&self) -> String {
std::str::from_utf8(&self.data).unwrap().to_owned()
}
}
impl Into<[u8;11]> for Vcu {
fn into(self) -> [u8;11] {
self.data
}
}
但是当我尝试使用 Into 的实现时
let mut packet_data: [u8; 36] = [0; 36];
let a: &[u8] = <[u8;17]>::from(vin_number.clone().into()).as_ref();
let b: &[u8] = <[u8;11]>::from(serial_ecu.clone().into()).as_ref();
对于 Vin 类型我没有任何问题,但对于 Vcu 类型它给了我以下错误:
error[E0283]: type annotations needed
-->
|
35 | let b: &[u8] = <[u8;11]>::from(serial_ecu.clone().into()).as_ref();
| --------------- ^^^^
| |
| required by a bound introduced by this call
|
= note: multiple `impl`s satisfying `[u8; 11]: From<_>` found in the `core` crate:
- impl<T> From<!> for T;
- impl<T> From<(T, T, T, T, T, T, T, T, T, T, T)> for [T; 11];
- impl<T> From<T> for T;
help: try using a fully qualified path to specify the expected types
|
35 | let b: &[u8] = <[u8;11]>::from(<motorcycle::Vcu as Into<T>>::into(serial_ecu.clone())).as_ref();
| +++++++++++++++++++++++++++++++++++ ~
error[E0283]: type annotations needed
-->
|
35 | let b: &[u8] = <[u8;11]>::from(serial_ecu.clone().into()).as_ref();
| ^^^^
|
note: multiple `impl`s satisfying `motorcycle::Vcu: Into<_>` found
-->
|
112 | impl Into<[u8;11]> for Vcu {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: and another `impl` found in the `core` crate:
- impl<T, U> Into<U> for T
where U: From<T>;
help: try using a fully qualified path to specify the expected types
|
35 | let b: &[u8] = <[u8;11]>::from(<motorcycle::Vcu as Into<T>>::into(serial_ecu.clone())).as_ref();
| +++++++++++++++++++++++++++++++++++ ~
我已解决问题如下
let a: &[u8] = <[u8;17]>::from(vin_number.clone().into()).as_ref();
let b: &[u8] = <[u8;11]>::from(<self::Vcu as Into<[u8;11]>>::into(serial_ecu.clone())).as_ref();
但我的问题是,为什么对于一种类型我没有任何问题,而对于另一种类型却有任何问题?
我做错了什么?
抱歉,如果问题很愚蠢,但我无法解决。
出现歧义是因为
From<(T, T, T, …)> for [T; N]
和 From<[T;N]> for (T, T, T, …)
是针对 N = 12
之前的元组实现的,因此它们使您的 [T; 11]
但不是
[T; 17]
的情况变得模糊。