Skip to content

Commit fb17a18

Browse files
authored
Add SettingsList for storing lists of settings (#738)
This is a new type that is used to store lists of settings in addition to `SettingsMap`. The difference is that for lists you don't use keys to look up the values, but instead you use indices, or more likely, iteration.
1 parent a94255c commit fb17a18

File tree

5 files changed

+634
-15
lines changed

5 files changed

+634
-15
lines changed

crates/livesplit-auto-splitting/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub struct ProcessId(u64);
4242
#[repr(transparent)]
4343
pub struct SettingsMap(NonZeroU64);
4444

45+
#[repr(transparent)]
46+
pub struct SettingsList(NonZeroU64);
47+
4548
#[repr(transparent)]
4649
pub struct SettingValue(NonZeroU64);
4750

@@ -295,13 +298,64 @@ extern "C" {
295298
key_ptr: *const u8,
296299
key_len: usize,
297300
) -> Option<SettingValue>;
301+
/// Gets the length of a settings map.
302+
pub fn settings_map_len(map: SettingsMap) -> u64;
303+
/// Gets the key of a setting value from the settings map based on the index
304+
/// by storing it into the buffer provided. Returns `false` if the buffer is
305+
/// too small. After this call, no matter whether it was successful or not,
306+
/// the `buf_len_ptr` will be set to the required buffer size. If `false` is
307+
/// returned and the `buf_len_ptr` got set to 0, the index is out of bounds.
308+
/// The key is guaranteed to be valid UTF-8 and is not nul-terminated.
309+
pub fn settings_map_get_key_by_index(
310+
map: SettingsMap,
311+
idx: u64,
312+
buf_ptr: *mut u8,
313+
buf_len_ptr: *mut usize,
314+
) -> bool;
315+
/// Gets a copy of the setting value from the settings map based on the
316+
/// index. Returns `None` if the index is out of bounds. Any changes to it
317+
/// are only perceived if it's stored back. You own the setting value and
318+
/// are responsible for freeing it.
319+
pub fn settings_map_get_value_by_index(map: SettingsMap, idx: u64) -> Option<SettingValue>;
320+
321+
/// Creates a new settings list. You own the settings list and are
322+
/// responsible for freeing it.
323+
pub fn settings_list_new() -> SettingsList;
324+
/// Frees a settings list.
325+
pub fn settings_list_free(list: SettingsList);
326+
/// Copies a settings list. No changes inside the copy affect the original
327+
/// settings list. You own the new settings list and are responsible for
328+
/// freeing it.
329+
pub fn settings_list_copy(list: SettingsList) -> SettingsList;
330+
/// Gets the length of a settings list.
331+
pub fn settings_list_len(list: SettingsList) -> u64;
332+
/// Gets a copy of the setting value from the settings list based on the
333+
/// index. Returns `None` if the index is out of bounds. Any changes to it
334+
/// are only perceived if it's stored back. You own the setting value and
335+
/// are responsible for freeing it.
336+
pub fn settings_list_get(list: SettingsList, idx: u64) -> Option<SettingValue>;
337+
/// Pushes a copy of the setting value to the end of the settings list. You
338+
/// still retain ownership of the setting value, which means you still need
339+
/// to free it.
340+
pub fn settings_list_push(list: SettingsList, value: SettingValue);
341+
/// Inserts a copy of the setting value into the settings list at the index
342+
/// given. If the index is out of bounds, the setting value is pushed to the
343+
/// end of the settings list. You still retain ownership of the setting
344+
/// value, which means you still need to free it.
345+
pub fn settings_list_insert(list: SettingsList, idx: u64, value: SettingValue);
298346

299347
/// Creates a new setting value from a settings map. The value is a copy of
300348
/// the settings map. Any changes to the original settings map afterwards
301349
/// are not going to be perceived by the setting value. You own the setting
302350
/// value and are responsible for freeing it. You also retain ownership of
303351
/// the settings map, which means you still need to free it.
304352
pub fn setting_value_new_map(value: SettingsMap) -> SettingValue;
353+
/// Creates a new setting value from a settings list. The value is a copy of
354+
/// the settings list. Any changes to the original settings list afterwards
355+
/// are not going to be perceived by the setting value. You own the setting
356+
/// value and are responsible for freeing it. You also retain ownership of
357+
/// the settings list, which means you still need to free it.
358+
pub fn setting_value_new_list(value: SettingsList) -> SettingValue;
305359
/// Creates a new boolean setting value. You own the setting value and are
306360
/// responsible for freeing it.
307361
pub fn setting_value_new_bool(value: bool) -> SettingValue;
@@ -317,13 +371,24 @@ extern "C" {
317371
pub fn setting_value_new_string(value_ptr: *const u8, value_len: usize) -> SettingValue;
318372
/// Frees a setting value.
319373
pub fn setting_value_free(value: SettingValue);
374+
/// Copies a setting value. No changes inside the copy affect the original
375+
/// setting value. You own the new setting value and are responsible for
376+
/// freeing it.
377+
pub fn setting_value_copy(value: SettingValue) -> SettingValue;
320378
/// Gets the value of a setting value as a settings map by storing it into
321379
/// the pointer provided. Returns `false` if the setting value is not a
322380
/// settings map. No value is stored into the pointer in that case. No
323381
/// matter what happens, you still retain ownership of the setting value,
324382
/// which means you still need to free it. You own the settings map and are
325383
/// responsible for freeing it.
326384
pub fn setting_value_get_map(value: SettingValue, value_ptr: *mut SettingsMap) -> bool;
385+
/// Gets the value of a setting value as a settings list by storing it into
386+
/// the pointer provided. Returns `false` if the setting value is not a
387+
/// settings list. No value is stored into the pointer in that case. No
388+
/// matter what happens, you still retain ownership of the setting value,
389+
/// which means you still need to free it. You own the settings list and are
390+
/// responsible for freeing it.
391+
pub fn setting_value_get_list(value: SettingValue, value_ptr: *mut SettingsList) -> bool;
327392
/// Gets the value of a boolean setting value by storing it into the pointer
328393
/// provided. Returns `false` if the setting value is not a boolean. No
329394
/// value is stored into the pointer in that case. No matter what happens,

crates/livesplit-auto-splitting/src/lib.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
//! pub struct SettingsMap(NonZeroU64);
4444
//!
4545
//! #[repr(transparent)]
46+
//! pub struct SettingsList(NonZeroU64);
47+
//!
48+
//! #[repr(transparent)]
4649
//! pub struct SettingValue(NonZeroU64);
4750
//!
4851
//! #[repr(transparent)]
@@ -295,13 +298,64 @@
295298
//! key_ptr: *const u8,
296299
//! key_len: usize,
297300
//! ) -> Option<SettingValue>;
301+
//! /// Gets the length of a settings map.
302+
//! pub fn settings_map_len(map: SettingsMap) -> u64;
303+
//! /// Gets the key of a setting value from the settings map based on the index
304+
//! /// by storing it into the buffer provided. Returns `false` if the buffer is
305+
//! /// too small. After this call, no matter whether it was successful or not,
306+
//! /// the `buf_len_ptr` will be set to the required buffer size. If `false` is
307+
//! /// returned and the `buf_len_ptr` got set to 0, the index is out of bounds.
308+
//! /// The key is guaranteed to be valid UTF-8 and is not nul-terminated.
309+
//! pub fn settings_map_get_key_by_index(
310+
//! map: SettingsMap,
311+
//! idx: u64,
312+
//! buf_ptr: *mut u8,
313+
//! buf_len_ptr: *mut usize,
314+
//! ) -> bool;
315+
//! /// Gets a copy of the setting value from the settings map based on the
316+
//! /// index. Returns `None` if the index is out of bounds. Any changes to it
317+
//! /// are only perceived if it's stored back. You own the setting value and
318+
//! /// are responsible for freeing it.
319+
//! pub fn settings_map_get_value_by_index(map: SettingsMap, idx: u64) -> Option<SettingValue>;
320+
//!
321+
//! /// Creates a new settings list. You own the settings list and are
322+
//! /// responsible for freeing it.
323+
//! pub fn settings_list_new() -> SettingsList;
324+
//! /// Frees a settings list.
325+
//! pub fn settings_list_free(list: SettingsList);
326+
//! /// Copies a settings list. No changes inside the copy affect the original
327+
//! /// settings list. You own the new settings list and are responsible for
328+
//! /// freeing it.
329+
//! pub fn settings_list_copy(list: SettingsList) -> SettingsList;
330+
//! /// Gets the length of a settings list.
331+
//! pub fn settings_list_len(list: SettingsList) -> u64;
332+
//! /// Gets a copy of the setting value from the settings list based on the
333+
//! /// index. Returns `None` if the index is out of bounds. Any changes to it
334+
//! /// are only perceived if it's stored back. You own the setting value and
335+
//! /// are responsible for freeing it.
336+
//! pub fn settings_list_get(list: SettingsList, idx: u64) -> Option<SettingValue>;
337+
//! /// Pushes a copy of the setting value to the end of the settings list. You
338+
//! /// still retain ownership of the setting value, which means you still need
339+
//! /// to free it.
340+
//! pub fn settings_list_push(list: SettingsList, value: SettingValue);
341+
//! /// Inserts a copy of the setting value into the settings list at the index
342+
//! /// given. If the index is out of bounds, the setting value is pushed to the
343+
//! /// end of the settings list. You still retain ownership of the setting
344+
//! /// value, which means you still need to free it.
345+
//! pub fn settings_list_insert(list: SettingsList, idx: u64, value: SettingValue);
298346
//!
299347
//! /// Creates a new setting value from a settings map. The value is a copy of
300348
//! /// the settings map. Any changes to the original settings map afterwards
301349
//! /// are not going to be perceived by the setting value. You own the setting
302350
//! /// value and are responsible for freeing it. You also retain ownership of
303351
//! /// the settings map, which means you still need to free it.
304352
//! pub fn setting_value_new_map(value: SettingsMap) -> SettingValue;
353+
//! /// Creates a new setting value from a settings list. The value is a copy of
354+
//! /// the settings list. Any changes to the original settings list afterwards
355+
//! /// are not going to be perceived by the setting value. You own the setting
356+
//! /// value and are responsible for freeing it. You also retain ownership of
357+
//! /// the settings list, which means you still need to free it.
358+
//! pub fn setting_value_new_list(value: SettingsList) -> SettingValue;
305359
//! /// Creates a new boolean setting value. You own the setting value and are
306360
//! /// responsible for freeing it.
307361
//! pub fn setting_value_new_bool(value: bool) -> SettingValue;
@@ -317,13 +371,24 @@
317371
//! pub fn setting_value_new_string(value_ptr: *const u8, value_len: usize) -> SettingValue;
318372
//! /// Frees a setting value.
319373
//! pub fn setting_value_free(value: SettingValue);
374+
//! /// Copies a setting value. No changes inside the copy affect the original
375+
//! /// setting value. You own the new setting value and are responsible for
376+
//! /// freeing it.
377+
//! pub fn setting_value_copy(value: SettingValue) -> SettingValue;
320378
//! /// Gets the value of a setting value as a settings map by storing it into
321379
//! /// the pointer provided. Returns `false` if the setting value is not a
322380
//! /// settings map. No value is stored into the pointer in that case. No
323381
//! /// matter what happens, you still retain ownership of the setting value,
324382
//! /// which means you still need to free it. You own the settings map and are
325383
//! /// responsible for freeing it.
326384
//! pub fn setting_value_get_map(value: SettingValue, value_ptr: *mut SettingsMap) -> bool;
385+
//! /// Gets the value of a setting value as a settings list by storing it into
386+
//! /// the pointer provided. Returns `false` if the setting value is not a
387+
//! /// settings list. No value is stored into the pointer in that case. No
388+
//! /// matter what happens, you still retain ownership of the setting value,
389+
//! /// which means you still need to free it. You own the settings list and are
390+
//! /// responsible for freeing it.
391+
//! pub fn setting_value_get_list(value: SettingValue, value_ptr: *mut SettingsList) -> bool;
327392
//! /// Gets the value of a boolean setting value by storing it into the pointer
328393
//! /// provided. Returns `false` if the setting value is not a boolean. No
329394
//! /// value is stored into the pointer in that case. No matter what happens,
@@ -392,6 +457,6 @@ mod timer;
392457

393458
pub use process::Process;
394459
pub use runtime::{Config, CreationError, InterruptHandle, Runtime, RuntimeGuard};
395-
pub use settings::{SettingValue, SettingsMap, UserSetting, UserSettingKind};
460+
pub use settings::{SettingValue, SettingsList, SettingsMap, UserSetting, UserSettingKind};
396461
pub use time;
397462
pub use timer::{Timer, TimerState};

0 commit comments

Comments
 (0)