#Lifetime error.
177 messages · Page 1 of 1 (latest)
Is this ratatui?
ideally cargo check's output
wait
mb
yeah
But the problem is related to lifetime and move stuff rather than rataui.
Oh, you can't do this. You're trying to create a self-referential struct. Just use Line<'static>.
doesn't static means it will live as long as program runs?
But it will be dropped after some work?
It means it would be able to remain valid for as long as the program runs, not that it necessarily lasts that long.
oh
can't i tie the lifetime with the struct? so, as long as struct is valid Line is valid too?
@carmine tinsel
In this case Line<'a> contains Span<'a> which contains Cow<'a, str>. Cow, which stands for Clone on Write, can be either borrowed or owned, but you really always need your base app to own this thing, so the cow will always be owned for you, which means the lifetime is irrelevant, and it should just be 'static.
it is getting over my head
Do you understand the difference between &str and String?
what's progress_feedback_data.as_line(..)'s signature ?
pub fn as_line(&self, bar_width: u16) -> Line<'_>
it isn't making any sense for me ;-;
Then what would be the solution and what was the root of all this problem?
So Cow<'a, str> is always one of either &'a str or String at any given time, and it can change whenever you want. However in your use case, it's hard to store borrowed data in a struct that lives as long as the app does because the borrow has to live longer than the app, so it's much easier to just always have owned data.
Base doesn't have any borrowed data?
doesn't seem like there is a nice Line<'a> -> Line<'static> conv though
Well currently you're saying the hint is borrowed, but that would imply it outlives the app.
that is technically close to the truth but also very very misleading comment
it could leave much longer if it is moved out
I want base to own the hint.
that is not how lifetimes work
at all
you can't own a lifetime
Exactly, which is why you should remove the lifetime and use Line<'static>.
Than whats the problem?
than doesn't 'static outlives the Base?
yes 'static outliives everything
Do you understand what I'm saying about Cow about how it's either borrowed or owned but you always want it to be owned?
then, isn't 'static a wrong lifetime? caz Base owns the Hit.
but with 'static, Hit outlives the Base, and must be valid as long as program runs
yeah kinda
A type having a lifetime parameter implies that that type doesn't own some part of the data within it that uses that lifetime.
with 'static, 'hint outlives Base<'hint>, which is always required and good
that's all
using 'static feels wrong ;-;
'static means Base<'static> is valid forever, which is good
well i'm sorry you feel that way, but it's not wrong
still, that only works if you can get a Line<'static>
So Cow is an enum with two variants. One of them is borrowed and one is owned. Only the borrowed one cares about the lifetime, the owned one ignores it, since your data will always be owned, the lifetime doesn't matter. So just make it 'static to make it maximally permissive.
Yes that's good.
well, i am not really getting it...
hopefully, i will learn this stuff as i go
Which part are you missing? If you ask specific questions about my explanation I can try to help.
btw @thick dagger can i see the whole function's code ?
and what's the siignature of draw
Can you show the definition of the feedback data?
What I understand from your explanation is that Cow essentially handles two kinds of data: one that is owned and one that is borrowed, and the borrowed data requires a lifetime.
thats all..
i am not getting other parts
Right so, you want the app to own the hint, right?
yep
doesn't look like everything.
for example
let mut base_app = Base::<&'static str>::default();
in the error message is missing
So you never want the Cow stored here to be in it's borrowed state, and since the lifetime only affects the borrowed state, it won't actually affect this Cow at all.
@carmine tinsel
wait
in a text file is fine
the move in terminal.draw(move |frame: &mut Frame| { is causing you issues
particularly the last error
what do you believe you need it for ?
Do you mean terminal.draw? Because that's from ratatui.
no, their function
thank you !
it wasn't move initially
but i had to make it move after making that spawn() thingy
It was working perfectly fine before dealing with future ;-;
let progress_feedback_data: Progress<'_> = {
let lock = progress_feedback_clone_for_renderer.lock().await;
lock.clone()
};
why do you clone lol
oh nvm i got it
you clone the value inside
that's weird
caz, i can't use await inside of sync?
and if i clone and drop the guard, the lock will be freed for that downloader to use.
Did you try cloning the line after creating it?
and as_line is a method on Progress ?
Before assigning to hint?
cloning can't change type
as i said before, there is no simple Line<'a> -> Line<'static> utility
.to_owned() then, maybe?
but all fields are public
huh no? I need to transfer the ownership of Line, I don't want to hold line
so you could make one
it could but no, because it is Clone, so it get to_owned for free that does a clone
@thick dagger ?
yep
ok
Do you think you could describe what this code is trying to achieve?
yeah
sure
wait
ok, then there is no way to avoid Line<'static> without refacto
so let's make your converter
I don't know why you would want to avoid that anyway. Surely it only makes sense for the hint there to be owned anyway.
if it can be borrowed it's nicer
Basically, when sync_handler() is executed, it performs two tasks: one is downloading an OS-compatible library from GitHub, and the other is handling the loading display. I added an animated loading screen, so I need to keep drawing it, which is why I offloaded the downloading task too.
Perhaps but if it's stored in a god object there's no way that's going to be possible.
This is what i am trying to do right now*
So what I wanted to know is like, when you do that line conversion and assign the hint, you're basically just trying to copy the text into the variable right?
yeah? i believe so
wait
just trying to copy the text into the variable right?
what text?
fn forever_line(l : Line<'_>) -> Line<'static> {
Line {
style : l.style,
alignment : l.alignement,
spans : s.spans.into_iter().map(forever_span).collect()
}
}
fn forever_span(s : Span<'_>) -> Span<'static> {
Span { style : s.style, content : s.contnt.into_owned().into()}
}
I am trying to make cargo style loader here so, it will be like <pre_text> [=====> ]
what if you use Span<'static> instead
I believe problem isn't with Progress or base_app, but rather that closure thingy, caz it was working before i added those tokio::spawn*
and Line<'static>
let me try
So then that as_line method is giving us some Line<'1> and we just want to make it Line<'static>.
ah no that's doesn't work
oops i told discord to dictate my code message
that was weird
It should be possible to do the conversion, it doesn't seem like ratatui provides it though. We can convert the line into an iterator of spans, then map the cows in the spans and to_owned them, and then recollect everything back into a line.
are you ignoring me on purpose
yeah that's cause of the move
you either remove it
fixed, I removed the move from the clouser.
or you do smth like
let mut base_app_mut = &mut base_app;
and use base_app_mut inside draw
No sorry I just got confused by the fact that link goes to a specific post and not just this thread in general.
ok np
cause you just explained what i did after i did it

you be substracting too hard
what's 33
spans.push(Span::raw(" ".repeat(bar_width as usize - fill_space))); i assume
It's hard for me to read code blocks on mobile because discord always formats the indentation super wrong, so my eyes kind of glaze over when I see them.
lol
well seems bar_width can be smaller than fill_space
you need to decide what to do when that happens
" ".repeat((bar_width as usize).abs_diff(fill_space))
?
i don't know
it's your ui code, not mine
if you think that's the correct solution, then go for it
yeah it seems fine
then great
nvm
I went out of the frame
its 3am ;-;
my math isn't mathing
let remaining_space = bar_width - fill_space.min(bar_width);
spans.push(Span::raw(" ".repeat(remaining_space as usize)));
fixed ig
that's .saturating_sub
tokio::spawn(async move -> anyhow::Result<()> {
}
why can't i do this?
@carmine tinsel
i can't pass closure either?
you cannot write the type there
what am i suppose to do?
just remove the arrow and the type
file.flush().await?;
I am keep getting type must be known at this pointrustcE0283 for all those ? operator
ah,yeah, i see, you can't do that indeed
async move || anyhow::Result<()> { ... } ()