I'm very intrigued by the error below. Somehow, the present of coefficients: Box<dyn M<'a, T, I> + 'a>,
makes it impossible to borrow with as_my_iterator
, but if you comment it out, it works. I truly have no idea why the mere presence of such member makes this error, but if you comment this line, you see that everything works.
use std::marker::PhantomData;
impl<'a, T, I> std::fmt::Debug for MyIterable<'a, T, I> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let x = self.as_my_iterator();
Ok(())
}
}
pub trait M<'r, T: 'r, I>
{
}
pub struct MyIterable<'a, T, I> {
//Comment this line:
coefficients: Box<dyn M<'a, T, I> + 'a>,
_phantom1: PhantomData<&'a T>,
_phantom2: PhantomData<&'a I>
}
pub struct MyIterator<'a, T> {
pub(crate) coefficients: &'a [T],
}
pub trait AsMyIterator<'a, T> {
fn as_my_iterator(&'a self) -> Result<MyIterator<'a, T>, ()>;
}
impl<'a, T, I> AsMyIterator<'a, T> for MyIterable<'a, T, I> {
fn as_my_iterator(
&'a self
) -> Result<
MyIterator<'a, T>,
(),
> {
todo!();
}
}
Error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/lib.rs:8:16
|
8 | let x = self.as_my_iterator();
| ^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
What is happening?