Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M3: Ignore empty time ranges in overlap checks

Lucenx950152171e626
diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs
index 2cb8004a8..253702ff1 100644
--- a/crates/time/src/lib.rs
+++ b/crates/time/src/lib.rs
@@ -121,6 +121,7 @@ impl SubAssign for Tick {
}
/// A half-open time range `[start, start + duration)`.
+/// Zero or negative durations are empty and never overlap another range.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TimeRange {
pub start: Tick,
@@ -141,7 +142,7 @@ impl TimeRange {
t >= self.start && t < self.end()
}
pub fn overlaps(&self, o: &TimeRange) -> bool {
- self.start < o.end() && o.start < self.end()
+ !self.is_empty() && !o.is_empty() && self.start < o.end() && o.start < self.end()
}
pub fn intersect(&self, o: &TimeRange) -> Option<TimeRange> {
let s = self.start.max(o.start);
@@ -514,6 +515,28 @@ mod tests {
use super::*;
use proptest::prelude::*;
+ #[test]
+ fn empty_time_ranges_never_overlap() {
+ let clip = TimeRange::new(Tick(0), Tick(10));
+ for duration in [0, -1, -5] {
+ let empty = TimeRange::new(Tick(5), Tick(duration));
+ assert!(empty.is_empty());
+ assert_eq!(empty.intersect(&clip), None);
+ assert!(!empty.overlaps(&clip), "duration {duration}");
+ assert!(!clip.overlaps(&empty), "duration {duration}");
+ }
+ }
+
+ #[test]
+ fn time_range_overlap_respects_half_open_boundaries() {
+ let clip = TimeRange::new(Tick(0), Tick(10));
+ for (start, duration, expected) in [(-5, 5, false), (10, 5, false), (-5, 6, true), (9, 5, true), (2, 3, true)] {
+ let other = TimeRange::new(Tick(start), Tick(duration));
+ assert_eq!(clip.overlaps(&other), expected);
+ assert_eq!(other.overlaps(&clip), expected);
+ }
+ }
+
#[test]
fn seconds_display_has_no_negative_zero() {
let f = |t: i64| format_time(Tick(t), FrameRate::default(), false, TimeDisplay::Seconds, 48000);