From ac6b01dd91d0167fa2c4bb87cf459402917a3c5d Mon Sep 17 00:00:00 2001 From: Niko Date: Wed, 30 Jul 2025 17:42:24 -0700 Subject: [PATCH] Restructure to minimize the number of unsafe calls in VAD code --- src/whisper_vad.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/whisper_vad.rs b/src/whisper_vad.rs index ec21e8c..9c965ca 100644 --- a/src/whisper_vad.rs +++ b/src/whisper_vad.rs @@ -250,9 +250,13 @@ impl WhisperVadSegments { self.segment_count } + pub fn index_in_bounds(&self, idx: c_int) -> bool { + idx < 0 || idx > self.segment_count + } + /// Return the start timestamp of this segment in centiseconds (10s of milliseconds). pub fn get_segment_start_timestamp(&self, idx: c_int) -> Option { - if idx < 0 || idx > self.segment_count { + if self.index_in_bounds(idx) { None } else { Some(unsafe { whisper_vad_segments_get_segment_t0(self.ptr, idx) }) @@ -261,7 +265,7 @@ impl WhisperVadSegments { /// Return the end timestamp of this segment in centiseconds (10s of milliseconds). pub fn get_segment_end_timestamp(&self, idx: c_int) -> Option { - if idx < 0 || idx > self.segment_count { + if self.index_in_bounds(idx) { None } else { Some(unsafe { whisper_vad_segments_get_segment_t1(self.ptr, idx) }) @@ -269,13 +273,10 @@ impl WhisperVadSegments { } pub fn get_segment(&self, idx: c_int) -> Option { - if idx < 0 || idx > self.segment_count { - None - } else { - let start = unsafe { whisper_vad_segments_get_segment_t0(self.ptr, self.iter_idx) }; - let end = unsafe { whisper_vad_segments_get_segment_t1(self.ptr, self.iter_idx) }; - Some(WhisperVadSegment { start, end }) - } + let start = self.get_segment_start_timestamp(idx)?; + let end = self.get_segment_end_timestamp(idx)?; + + Some(WhisperVadSegment { start, end }) } }