-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
..._get_text functions not available #162
Comments
What type should it return, |
I'm not very fluent in Rust (still can't figure out ownership) but this change made diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs
index 691ffb8..5235f86 100644
--- a/lvgl/src/widgets/label.rs
+++ b/lvgl/src/widgets/label.rs
@@ -1,5 +1,6 @@
use crate::widgets::Label;
use crate::{LabelLongMode, NativeObject};
+use cstr_core::CStr;
#[cfg(feature = "alloc")]
mod alloc_imp {
@@ -41,4 +42,10 @@ impl Label<'_> {
pub fn get_long_mode(&self) -> u8 {
unsafe { lvgl_sys::lv_label_get_long_mode(self.raw().as_ref()) }
}
+
+ pub fn get_text(&self) -> &'static str {
+ let char_ptr = unsafe { lvgl_sys::lv_label_get_text(self.raw().as_ref()) };
+ let c_str = unsafe { CStr::from_ptr(char_ptr) };
+ c_str.to_str().unwrap_or_default()
+ }
}
And I'm using it like this diff --git a/examples/demo.rs b/examples/demo.rs
index f69cf5b..dba4865 100644
--- a/examples/demo.rs
+++ b/examples/demo.rs
@@ -62,6 +62,8 @@ fn main() -> Result<(), LvError> {
bt.set_height(80);
bt.set_recolor(true);
bt.set_align(Align::TopLeft, 0, 0);
+ let mut test_label = Label::from("701");
+ println!("Label get_text from bindinig: {:?}", test_label.get_text());
let mut power: Label = "#fade2a 20%#".into();
power.set_recolor(true);
diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs
index 691ffb8..5235f86 100644
--- a/lvgl/src/widgets/label.rs
+++ b/lvgl/src/widgets/label.rs You can see the '701' being printed here: Do you think this is correct @AlixANNERAUD ? |
I will check that this weekend since I have to work on my semester final exams |
Good luck with the exams, I'll try to figure this out based on the commit history |
This is a memory safety issue, since String::from_utf8_lossy(cstr.to_bytes()).to_string() |
I see, so we need to somewhat determine which functions are unsafe (because of this or a similar bug) and document them or handle them differently in the codegen phase. Is it possible to use String in libs intended to be used with no-std? EDIT Just found this docs https://doc.rust-lang.org/core/ffi/c_str/struct.CStr.html |
Well, all function that return pointer are considered unsafe by default. We can then the type handling common types safely. |
The update on the generator I'm currently doing checks for the return type of the generated interfaces, I think it's possible to skip the functions that return pointers, and we can manually implement those. What do you think? |
Well that seems the most suitable solution to me. |
Closing in favor of #192 were the function that return pointers will be manually handled |
The
..._get_text
functions onLabel
andTextarea
don't seem to be available currently. I can get text by calling the C function in anunsafe
block like this:However, it would be nice if I could do
textarea.get_text()
.The text was updated successfully, but these errors were encountered: