For context, I'm working on a pet project that includes a Span
type that annotates a value with a start
and end
in some input:
struct Span<T> {
start: usize,
end: usize,
value: T
}
This Span
is used to wrap errors (e.g. Result<_, Span<E>>
), which I would like to be compatible with ?
coercion. In order for this to work, I think I need to implement:
impl<T, U> From<Span<T>> for Span<U>
where
U: From<T>
{
fn from(span: Span<T>) -> Self {
Span {
start: span.start,
end: span.end,
value: U::from(span.value)
}
}
}
This fails with:
conflicting implementations of trait `std::convert::From<span::Span<_>>` for type `span::Span<_>`
note: conflicting implementation in crate `core`:
- impl<T> std::convert::From<T> for T;
I understand what this means, and suspect that I am out of luck. However I'm struggling to find out definitively whether it's possible to specify that T
and U
must be different and therefore avoid the conflict.
If not, this doesn't seem like an unintuitive use-case, so perhaps someone knows of another approach that could solve the use-case of propagating wrapper types through ?
coercion?