#Lifetime error.

177 messages · Page 1 of 1 (latest)

carmine tinsel
#

please give the full diagnostic

nocturne trout
#

Is this ratatui?

carmine tinsel
#

ideally cargo check's output

thick dagger
carmine tinsel
#

mb

thick dagger
thick dagger
nocturne trout
#

Right, yeah.

#

What type is base_app.hint?

thick dagger
#

@nocturne trout

#

Line is from rataui.

nocturne trout
#

Oh, you can't do this. You're trying to create a self-referential struct. Just use Line<'static>.

thick dagger
#

But it will be dropped after some work?

nocturne trout
thick dagger
#

can't i tie the lifetime with the struct? so, as long as struct is valid Line is valid too?

#

@carmine tinsel

nocturne trout
#

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.

thick dagger
#

it is getting over my head

nocturne trout
#

Do you understand the difference between &str and String?

thick dagger
#

yeah

#

&str is a view of String

carmine tinsel
#

what's progress_feedback_data.as_line(..)'s signature ?

thick dagger
carmine tinsel
#

ah, yeah

#

makes sense

thick dagger
#

it isn't making any sense for me ;-;

thick dagger
nocturne trout
#

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.

thick dagger
carmine tinsel
#

doesn't seem like there is a nice Line<'a> -> Line<'static> conv though

nocturne trout
carmine tinsel
#

that is technically close to the truth but also very very misleading comment

#

it could leave much longer if it is moved out

carmine tinsel
#

at all

#

you can't own a lifetime

thick dagger
#

huh? Is list a lifetime?

#

it isn't making any sense to me

nocturne trout
carmine tinsel
#

'hint is a lifetime

#

ah nvm my bad

#

yes, Base owns hint

thick dagger
carmine tinsel
#

but hint borrows from something

#

so Base borrows from that same thing

thick dagger
carmine tinsel
#

yes 'static outliives everything

nocturne trout
thick dagger
#

but with 'static, Hit outlives the Base, and must be valid as long as program runs

nocturne trout
carmine tinsel
#

that's all

carmine tinsel
#

'static means Base<'static> is valid forever, which is good

carmine tinsel
#

still, that only works if you can get a Line<'static>

nocturne trout
# thick dagger yeah kinda

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.

thick dagger
#

hopefully, i will learn this stuff as i go

nocturne trout
carmine tinsel
#

btw @thick dagger can i see the whole function's code ?

#

and what's the siignature of draw

nocturne trout
#

Can you show the definition of the feedback data?

thick dagger
#

i am not getting other parts

nocturne trout
carmine tinsel
# thick dagger *

doesn't look like everything.
for example

let mut base_app = Base::<&'static str>::default();
in the error message is missing

nocturne trout
# thick dagger yep

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.

thick dagger
#

@carmine tinsel

carmine tinsel
#

i really would like the whole function

#

even if it seems it is way too big

thick dagger
#

wait

carmine tinsel
#

in a text file is fine

thick dagger
#

ops

#

wait

carmine tinsel
#

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 ?

nocturne trout
carmine tinsel
#

thank you !

thick dagger
#

but i had to make it move after making that spawn() thingy

#

It was working perfectly fine before dealing with future ;-;

carmine tinsel
#
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

thick dagger
#

and if i clone and drop the guard, the lock will be freed for that downloader to use.

nocturne trout
#

Did you try cloning the line after creating it?

carmine tinsel
#

and as_line is a method on Progress ?

nocturne trout
#

Before assigning to hint?

carmine tinsel
#

as i said before, there is no simple Line<'a> -> Line<'static> utility

nocturne trout
#

.to_owned() then, maybe?

carmine tinsel
#

but all fields are public

thick dagger
carmine tinsel
#

so you could make one

carmine tinsel
carmine tinsel
thick dagger
carmine tinsel
#

ok

nocturne trout
carmine tinsel
#

ok, then there is no way to avoid Line<'static> without refacto

#

so let's make your converter

nocturne trout
carmine tinsel
#

if it can be borrowed it's nicer

thick dagger
#

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.

nocturne trout
thick dagger
nocturne trout
#

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?

thick dagger
#

just trying to copy the text into the variable right?

what text?

carmine tinsel
#
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()}
}
thick dagger
#

I am trying to make cargo style loader here so, it will be like <pre_text> [=====> ]

carmine tinsel
#

what if you use Span<'static> instead

thick dagger
#

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*

carmine tinsel
#

and Line<'static>

thick dagger
nocturne trout
#

So then that as_line method is giving us some Line<'1> and we just want to make it Line<'static>.

carmine tinsel
#

ah no that's doesn't work

carmine tinsel
#

oops i told discord to dictate my code message

#

that was weird

nocturne trout
#

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.

carmine tinsel
#

yeah that's cause of the move

#

you either remove it

thick dagger
#

fixed, I removed the move from the clouser.

carmine tinsel
#

or you do smth like

let mut base_app_mut = &mut base_app;

and use base_app_mut inside draw

nocturne trout
carmine tinsel
#

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

nocturne trout
carmine tinsel
#

well seems bar_width can be smaller than fill_space

#

you need to decide what to do when that happens

thick dagger
carmine tinsel
#

i don't know

#

it's your ui code, not mine

#

if you think that's the correct solution, then go for it

thick dagger
#

yeah it seems fine

carmine tinsel
#

then great

thick dagger
#

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

carmine tinsel
thick dagger
#
   tokio::spawn(async move -> anyhow::Result<()> {
}

why can't i do this?

#

@carmine tinsel

#

i can't pass closure either?

carmine tinsel
thick dagger
#

what am i suppose to do?

carmine tinsel
#

just remove the arrow and the type

thick dagger
#
        file.flush().await?;

I am keep getting type must be known at this pointrustcE0283 for all those ? operator

carmine tinsel
#
async move || anyhow::Result<()> { ... } ()