I only see two options here to address this issue, even tho I would argue that none of them is ideal:
the first one by having :
impl Displayable for ArticleView {
type SomeState = MyMutableState<'static>;
fn show(&self, data: &mut MyMutableState<'_>) -> String {
format!("👋 {}, {}", data.third.title, data.buffer.len())
}
}
This fixes the error at this level, but it makes MyMutableState
only working with a &'static str
for ThirdPartyState
.
The second one, as first suggested by @GiM in his comment, required you to have a lifetime for ArticleView
:
Comment of @GiM :
@ChayimFriedman yeah adding lifetime (and phantom data) seems to work, although that doesn't seem like a great solution :/ play.rust-lang.org/?gist=66e4d23b7439ce60243201eb3509a953
So the result will be :
use std::marker::PhantomData;
struct ArticleView<'a> {
// Note that the version of @DiM use `phantom: PhantomData<&'a u32>` which has no difference except personal preferences
_phantom: PhantomData<&'a ()>,
}
impl<'a> Displayable for ArticleView<'a> {
type SomeState = MyMutableState<'a>;
fn show(&self, data: &mut MyMutableState<'_>) -> String {
format!("👋 {}, {}", data.third.title, data.buffer.len())
}
}
This unfortunately changes the way ArticleView
is constructed (ArticleView { _phantom: PhantomData }
instead of ArticleView
)
Otherwise, I actually don't thinks there is a way to constrains a lifetime to this impl
without being able to modify the trait
you want to implements