I use TryFrom
to convert my type to some concrete types.
I tried to implement TryFrom<T>
for Option<T>
as a blanket implementation where T
refers to those concrete types, but I couldn’t make it work.
Simplified example (without failure paths for the sake of example):
#[derive(Debug, PartialEq, Eq)]
pub struct MyStruct;
impl TryFrom<&MyStruct> for u32 {
type Error = ();
fn try_from(_value: &MyStruct) -> Result<Self, Self::Error> {
Ok(42)
}
}
/*impl<'a, T: TryFrom<&'a MyStruct>> TryFrom<&'a MyStruct> for Option<T> {
type Error = <T as TryFrom<&'a MyStruct>>::Error;
fn try_from(value: &'a MyStruct) -> Result<Self, Self::Error> {
<T as TryFrom<&'a MyStruct>>::try_from(value).map(Some)
}
}*/
#[test]
fn test_option() {
assert_eq!(Option::<u32>::try_from(&MyStruct), Ok(Some(42)));
}
error[E0277]: the trait bound `Option<u32>: TryFrom<&MyStruct>` is not satisfied
--> src/lib.rs:22:16
|
22 | assert_eq!(Option::<u32>::try_from(&MyStruct), Ok(Some(42)));
| ^^^^^^^^^^^^^ the trait `From<&MyStruct>` is not implemented for `Option<u32>`, which is required by `Option<u32>: TryFrom<_>`
|
= help: the following other types implement trait `From<T>`:
<Option<&'a T> as From<&'a Option<T>>>
<Option<&'a mut T> as From<&'a mut Option<T>>>
<Option<T> as From<T>>
= note: required for `&MyStruct` to implement `Into<Option<u32>>`
= note: required for `Option<u32>` to implement `TryFrom<&MyStruct>`
error[E0277]: the trait bound `Option<u32>: From<&MyStruct>` is not satisfied
--> src/lib.rs:22:16
|
22 | assert_eq!(Option::<u32>::try_from(&MyStruct), Ok(Some(42)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&MyStruct>` is not implemented for `Option<u32>`, which is required by `Option<u32>: TryFrom<&MyStruct>`
|
= help: the following other types implement trait `From<T>`:
<Option<&'a T> as From<&'a Option<T>>>
<Option<&'a mut T> as From<&'a mut Option<T>>>
<Option<T> as From<T>>
= note: required for `&MyStruct` to implement `Into<Option<u32>>`
= note: required for `Option<u32>` to implement `TryFrom<&MyStruct>`
For more information about this error, try `rustc --explain E0277`.
Playground
If I enable commented-out part:
#[derive(Debug, PartialEq, Eq)]
pub struct MyStruct;
impl TryFrom<&MyStruct> for u32 {
type Error = ();
fn try_from(_value: &MyStruct) -> Result<Self, Self::Error> {
Ok(42)
}
}
impl<'a, T: TryFrom<&'a MyStruct>> TryFrom<&'a MyStruct> for Option<T> {
type Error = <T as TryFrom<&'a MyStruct>>::Error;
fn try_from(value: &'a MyStruct) -> Result<Self, Self::Error> {
<T as TryFrom<&'a MyStruct>>::try_from(value).map(Some)
}
}
#[test]
fn test_option() {
assert_eq!(Option::<u32>::try_from(&MyStruct), Ok(Some(42)));
}
error[E0119]: conflicting implementations of trait `TryFrom<&MyStruct>` for type `Option<_>`
--> src/lib.rs:12:1
|
12 | impl<'a, T: TryFrom<&'a MyStruct>> TryFrom<&'a MyStruct> for Option<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T, U> TryFrom<U> for T
where U: Into<T>;
For more information about this error, try `rustc --explain E0119`.
Playground
I receive missing implementation error for the former example, while I receive conflicting implementation error for the latter. What am I missing here?