Skip to content

Commit 112f4ae

Browse files
committed
Rework gui_list_view_ex.
Working with CStr is not practical, and expecting the user to have to manipulate them is even less. Users are very likely to need to make a wrapper in order use this interface, so rework it to make it better to use. Expect a iterator of String for the text, this is more flexible than using a array and doesn't complicate the implementation as we will have to convert it to a list of CStr anyway.
1 parent ded9b79 commit 112f4ae

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

raylib/src/rgui/safe.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::core::text::WeakFont;
55
use crate::core::RaylibHandle;
66
use crate::ffi;
77

8-
use std::ffi::{CStr, CString};
8+
use std::ffi::{c_char, CStr, CString};
99

1010
/// Global gui modification functions
1111
impl RaylibHandle {
@@ -506,20 +506,24 @@ pub trait RaylibDrawGui {
506506
fn gui_list_view_ex(
507507
&mut self,
508508
bounds: impl Into<ffi::Rectangle>,
509-
text: &[&CStr],
509+
text: impl Iterator<Item = String>,
510510
focus: &mut i32,
511511
scroll_index: &mut i32,
512512
active: &mut i32,
513513
) -> i32 {
514-
let mut buffer = Vec::with_capacity(text.len());
515-
for t in text {
516-
buffer.push(t.as_ptr());
517-
}
514+
// We need to keep track of all CStr buffers.
515+
let buffer: Box<[Box<CStr>]> = text
516+
.map(|s| CString::new(s).unwrap().into_boxed_c_str())
517+
.collect();
518+
519+
let mut text_params: Box<[*const c_char]> =
520+
buffer.iter().map(|cstr| cstr.as_ptr()).collect();
521+
518522
unsafe {
519523
ffi::GuiListViewEx(
520524
bounds.into(),
521-
buffer.as_mut_ptr(),
522-
text.len() as i32,
525+
text_params.as_mut_ptr(),
526+
text_params.len() as i32,
523527
focus,
524528
scroll_index,
525529
active,

0 commit comments

Comments
 (0)