lvgl / lv_binding_rust

LVGL bindings for Rust. A powerful and easy-to-use embedded GUI with many widgets, advanced visual effects (opacity, antialiasing, animations) and low memory requirements (16K RAM, 64K Flash).
MIT License
687 stars 71 forks source link

pub struct DrawBuffer<const N: usize> #147

Closed fulup-bzh closed 1 year ago

fulup-bzh commented 1 year ago

Forcing this to a constant prevent from having simulator screen size store in a config object. Is there any special reason to enforce const here ?

pub struct DrawBuffer<const N: usize>
nia-e commented 1 year ago

Unfortunately I'm unsure if that can practically be removed; it would require having an allocator at runtime which we can only guarantee with the alloc feature (and duplicating code would be a massive pain). I've considered implementing a basic allocator based on LVGL's as a nonglobal fallback, but it would be a huge source of bugs I worry. I'll keep this open for the future

fulup-bzh commented 1 year ago

I'm not sure to understand, this is an integer and not a pointer it is copies once also the CONTS does not provide any functionnality

nia-e commented 1 year ago

The const N is used to determine the size of the allocation for the buffer; we store a Pin<Box<[MaybeUninit<lvgl_sys::lv_color_t>; N]>> inside of the actual DrawBuffer struct (note that this is not the same Box as in the rust stdlib; we use our own implementation, see mem.rs). Because the innermost value is a sized slice [T; N] we can't actually allocate it at runtime; we'd need a Vec<T> impl or similar, which is considerably more complex than just our boutique Box<T>. Arguably this is a Rust limitation, but short of doing some extremely unsafe things I don't see how to get around it short of said custom Vec implementation

fulup-bzh commented 1 year ago

I understand your point, it is a before everything a Rust limitation. Where you cannot force a specific element of a structure to be constance. If is was a string, we could use &'static str for integer it does not exist.

The point I do not get is that for me they is not difference in between the two following options

nia-e commented 1 year ago

In the latter case, you're getting the values at runtime (since the compiler doesn't know the value of config.h or of config.v) so the actual size of the final DrawBuffer isn't known by the compiler. You could work around this with a build script that sets some constants, or by using const fns to calculate the size (if the config-reading crate you use supports it).

fulup-bzh commented 1 year ago

I understand your point. I will try to move around by checking if a const fn thank you for your very reactive support.