Skip to content

Commit ff1a381

Browse files
committed
Option index i64, Option bool i32
1 parent aec843f commit ff1a381

6 files changed

Lines changed: 43 additions & 23 deletions

File tree

crates/livesplit-auto-splitting/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ unsafe extern "C" {
105105
/// when the attempt is finished, but has not been reset.
106106
/// So you need to be careful when using this value for indexing.
107107
/// Same index does not imply same split on undo and then split.
108-
pub safe fn timer_current_split_index() -> i32;
108+
pub safe fn timer_current_split_index() -> i64;
109109
/// Whether the segment at `idx` was splitted this attempt.
110-
pub safe fn timer_segment_splitted(idx: i32) -> bool;
110+
/// Returns `1` if the segment was splitted, or `0` if skipped.
111+
/// If `idx` is greater than or equal to the current split index,
112+
/// `-1` is returned instead.
113+
pub safe fn timer_segment_splitted(idx: u64) -> i32;
111114

112115
/// Starts the timer.
113116
pub safe fn timer_start();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@
105105
//! /// when the attempt is finished, but has not been reset.
106106
//! /// So you need to be careful when using this value for indexing.
107107
//! /// Same index does not imply same split on undo and then split.
108-
//! pub safe fn timer_current_split_index() -> i32;
108+
//! pub safe fn timer_current_split_index() -> i64;
109109
//! /// Whether the segment at `idx` was splitted this attempt.
110-
//! pub safe fn timer_segment_splitted(idx: i32) -> bool;
110+
//! /// Returns `1` if the segment was splitted, or `0` if skipped.
111+
//! /// If `idx` is greater than or equal to the current split index,
112+
//! /// `-1` is returned instead.
113+
//! pub safe fn timer_segment_splitted(idx: u64) -> i32;
111114
//!
112115
//! /// Starts the timer.
113116
//! pub safe fn timer_start();

crates/livesplit-auto-splitting/src/runtime/api/timer.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ pub fn bind<T: Timer>(linker: &mut Linker<Context<T>>) -> Result<(), CreationErr
2020
.data()
2121
.timer
2222
.current_split_index()
23-
.map_or(-1, |i| i as i32)
23+
.map_or(-1, |i| i as i64)
2424
}
2525
})
2626
.map_err(|source| CreationError::LinkFunction {
2727
source,
2828
name: "timer_current_split_index",
2929
})?
3030
.func_wrap("env", "timer_segment_splitted", {
31-
|caller: Caller<'_, Context<T>>, index: i32| {
32-
let Ok(idx) = usize::try_from(index) else {
33-
return Ok(0u32);
34-
};
35-
Ok(caller.data().timer.segment_splitted(idx) as u32)
31+
|caller: Caller<'_, Context<T>>, index: u64| {
32+
Ok(caller
33+
.data()
34+
.timer
35+
.segment_splitted(index as usize)
36+
.map_or(-1, |b| b as i32))
3637
}
3738
})
3839
.map_err(|source| CreationError::LinkFunction {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ pub trait Timer: Send {
4343
/// Same index does not imply same split on undo and then split.
4444
fn current_split_index(&self) -> Option<usize>;
4545
/// Whether the segment at `idx` was splitted this attempt.
46-
fn segment_splitted(&self, idx: usize) -> bool;
46+
/// Returns `Some(true)` if the segment was splitted,
47+
/// or `Some(false)` if skipped.
48+
/// If `idx` is greater than or equal to the current split index,
49+
/// `None` is returned instead.
50+
fn segment_splitted(&self, idx: usize) -> Option<bool>;
4751
/// Starts the timer.
4852
fn start(&mut self);
4953
/// Splits the current segment.

crates/livesplit-auto-splitting/tests/sandboxing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl Timer for DummyTimer {
1717
fn current_split_index(&self) -> Option<usize> {
1818
None
1919
}
20-
fn segment_splitted(&self, _idx: usize) -> bool {
21-
false
20+
fn segment_splitted(&self, _idx: usize) -> Option<bool> {
21+
None
2222
}
2323
fn start(&mut self) {}
2424
fn split(&mut self) {}

src/auto_splitting/mod.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@
105105
//! /// when the attempt is finished, but has not been reset.
106106
//! /// So you need to be careful when using this value for indexing.
107107
//! /// Same index does not imply same split on undo and then split.
108-
//! pub safe fn timer_current_split_index() -> i32;
109-
//! /// Whether the segment given by `idx` was splitted this attempt.
110-
//! pub safe fn timer_segment_splitted(idx: i32) -> bool;
108+
//! pub safe fn timer_current_split_index() -> i64;
109+
//! /// Whether the segment at `idx` was splitted this attempt.
110+
//! /// Returns `1` if the segment was splitted, or `0` if skipped.
111+
//! /// If `idx` is greater than or equal to the current split index,
112+
//! /// `-1` is returned instead.
113+
//! pub safe fn timer_segment_splitted(idx: u64) -> i32;
111114
//!
112115
//! /// Starts the timer.
113116
//! pub safe fn timer_start();
@@ -800,13 +803,19 @@ impl<E: event::CommandSink + TimerQuery + Send> AutoSplitTimer for Timer<E> {
800803
self.0.get_timer().current_split_index()
801804
}
802805

803-
fn segment_splitted(&self, idx: usize) -> bool {
804-
self.0
805-
.get_timer()
806-
.run()
807-
.segments()
808-
.get(idx)
809-
.is_some_and(|segment| segment.split_time().real_time.is_some())
806+
fn segment_splitted(&self, idx: usize) -> Option<bool> {
807+
let t = self.0.get_timer();
808+
if !(idx < t.current_split_index()?) {
809+
return None;
810+
}
811+
Some(
812+
t.run()
813+
.segments()
814+
.get(idx)?
815+
.split_time()
816+
.real_time
817+
.is_some(),
818+
)
810819
}
811820

812821
fn start(&mut self) {

0 commit comments

Comments
 (0)