From 00fa5d8aeb29f310e3186bf9c141fece7530a905 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Tue, 2 Sep 2025 10:47:35 -0400 Subject: [PATCH 1/2] Add timer current_split_index and timer segment_splitted Option index i64, Option bool i32 --- src/runtime/sys.rs | 12 ++++++++++++ src/runtime/timer.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/runtime/sys.rs b/src/runtime/sys.rs index 4fef9ff5..ab1d6838 100644 --- a/src/runtime/sys.rs +++ b/src/runtime/sys.rs @@ -79,6 +79,18 @@ extern "C" { pub fn timer_undo_split(); /// Resets the timer. pub fn timer_reset(); + /// Accesses the index of the split the attempt is currently on. + /// If there's no attempt in progress, `-1` is returned instead. + /// This returns an index that is equal to the amount of segments + /// when the attempt is finished, but has not been reset. + /// So you need to be careful when using this value for indexing. + /// Same index does not imply same split on undo and then split. + pub fn timer_current_split_index() -> i64; + /// Whether the segment at `idx` was splitted this attempt. + /// Returns `1` if the segment was splitted, or `0` if skipped. + /// If `idx` is greater than or equal to the current split index, + /// `-1` is returned instead. + pub fn timer_segment_splitted(idx: u64) -> i32; /// Sets a custom key value pair. This may be arbitrary information that the /// auto splitter wants to provide for visualization. The pointers need to /// point to valid UTF-8 encoded text with the respective given length. diff --git a/src/runtime/timer.rs b/src/runtime/timer.rs index b1eb218d..c6942785 100644 --- a/src/runtime/timer.rs +++ b/src/runtime/timer.rs @@ -112,6 +112,33 @@ pub fn state() -> TimerState { } } +/// Accesses the index of the split the attempt is currently on. +/// If there's no attempt in progress, `None` is returned instead. +/// This returns an index that is equal to the amount of segments +/// when the attempt is finished, but has not been reset. +/// So you need to be careful when using this value for indexing. +/// Same index does not imply same split on undo and then split. +pub fn current_split_index() -> Option { + let i = unsafe { sys::timer_current_split_index() }; + if i.is_negative() { + return None; + } + Some(i as u64) +} + +/// Whether the segment at `idx` was splitted this attempt. +/// Returns `Some(true)` if the segment was splitted, +/// or `Some(false)` if skipped. +/// If `idx` is greater than or equal to the current split index, +/// `None` is returned instead. +pub fn segment_splitted(idx: u64) -> Option { + match unsafe { sys::timer_segment_splitted(idx) } { + 1 => Some(true), + 0 => Some(false), + _ => None, + } +} + /// Sets the game time. #[inline] pub fn set_game_time(time: time::Duration) { From 9e81750e0fc2727e6c7e2c281bd5551447395513 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Sat, 8 Nov 2025 17:02:40 -0500 Subject: [PATCH 2/2] SAFETY comments --- src/runtime/timer.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/runtime/timer.rs b/src/runtime/timer.rs index c6942785..2b286081 100644 --- a/src/runtime/timer.rs +++ b/src/runtime/timer.rs @@ -119,6 +119,7 @@ pub fn state() -> TimerState { /// So you need to be careful when using this value for indexing. /// Same index does not imply same split on undo and then split. pub fn current_split_index() -> Option { + // SAFETY: It is always safe to call this function. let i = unsafe { sys::timer_current_split_index() }; if i.is_negative() { return None; @@ -132,6 +133,10 @@ pub fn current_split_index() -> Option { /// If `idx` is greater than or equal to the current split index, /// `None` is returned instead. pub fn segment_splitted(idx: u64) -> Option { + // SAFETY: It is always safe to call this function. + // Even when `idx` is out of bounds, + // timer_segment_splitted returns `-1`, + // and then segment_splitted returns `None`. match unsafe { sys::timer_segment_splitted(idx) } { 1 => Some(true), 0 => Some(false),