After having had some struggles with TryFrom
in the past, I tried again to use them. And I came across the following problem::
#![feature(generic_associated_types)]
#![allow(unused)]
trait Machine {
type Datum<'a>
where
Self: 'a;
fn get_datum<'a>(&'a self) -> Self::Datum<'a>;
}
struct ConversionError<T> {
original: T,
}
impl<T> ConversionError<T> {
fn explain(&self) {}
}
struct NongenericError {}
impl NongenericError {
fn explain(&self) {}
}
fn foo<M>(machine: M)
where
M: Machine,
// This won't work:
for<'a> String: TryFrom<
<M as Machine>::Datum<'a>,
Error = ConversionError<<M as Machine>::Datum<'a>>,
>,
// But this works:
//for<'a> String: TryFrom<<M as Machine>::Datum<'a>, Error = NongenericError>,
{
let result: Result<String, _> = machine.get_datum().try_into();
if let Err(err) = result {
err.explain();
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0582]: binding for associated type `Error` references lifetime `'a`, which does not appear in the trait input types
--> src/lib.rs:31:9
|
31 | Error = ConversionError<<M as Machine>::Datum<'a>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0582`.
error: could not compile `playground` due to previous error
Is it impossible what I attempt to do? What's wrong with:
for<'a> String: TryFrom<
<M as Machine>::Datum<'a>,
Error = ConversionError<<M as Machine>::Datum<'a>>,
>,
If I have a TryFrom::Error
that doesn't depend on 'a
it works fine.