StrataWM / strata

A cutting-edge, robust and sleek Wayland compositor with batteries included.
https://stratawm.github.io
GNU General Public License v3.0
177 stars 7 forks source link

trying to implement a global state #43

Closed anantnrg closed 1 year ago

anantnrg commented 1 year ago

Hey @calops, I've tried to implement what I understood but I pretty sure im doing something dumb here. Could you please check it out. Cheers :)

anantnrg commented 1 year ago

Hey @calops, I implemented what you said and obviously it fails to compile due to dyn BackendData (probably).

Also BackendData implements the Backend trait which is defined here:

pub trait Backend {
    fn seat_name(&self) -> String;
}

You could drop it entirely, and use Box<dyn Backend> in StrataState directly. Or maybe just use an enum?

Does this mean that we should make BackendData a field in StrataState, like this:

pub struct StrataState {
    ...
    BackendData: Box<dyn Backend>,
}

or does it mean something entirely different?

calops commented 1 year ago

What I meant is that instead of this in StrataState:

pub backend_data: BackendData,

You write this:

pub backend_data: Box<dyn Backend>,

Then you can just initialize it with Box::new(MyBackend::new()) where MyBackend implements the Backend trait. You don't need any generics do do any of this as it's dynamic polymorphism. Know that it involves a vtable, like you would get with pointers to objects in languages like C++ or Java.

Now, it's still possible to do all this with generics. You would drop the dyn entirely, but then you'd need to declare as many global states as there are backends and pass the one the user chooses as a parameter to every function that needs to use the state. It makes for a bit of boilerplate in the code but isn't too awful to use, but I suggest to keep the easy dyn Backend solution for now, and switch to generics if performance is ever an issue.

anantnrg commented 1 year ago

Thanks for the clarification. Its currently 12AM here, so I'll check this in the morning.

anantnrg commented 1 year ago

Hey @calops, I'm trying to implement BackendData like you suggested in your previous comment. But I have some trouble getting it to work. Could you please check what I've done and tell me if its correct?

Cheers :)

calops commented 1 year ago

Hi! Don't apologize for this, I'm happy to help. I'll look into it tonight!

anantnrg commented 1 year ago

Hey @calops, I created a Discord server for this project. If you think its easier to communicate there, you can join with this link https://discord.gg/HSJxz2bg. Cheers :)