Skip to content

Commit 13b18d1

Browse files
authored
Merge pull request #73 from LebedevRI/develop
Coord arith: allow subtracting negative offsets
2 parents 53db38e + fd725b6 commit 13b18d1

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/std/coord_arith/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl core::ops::Sub<RowOffset> for RowIndex {
2828
#[inline]
2929
fn sub(self, rhs: RowOffset) -> Self::Output {
3030
let lhs = *self;
31-
let rhs = (*rhs).try_into().ok()?;
32-
Some(Self::new(lhs.checked_sub(rhs)?))
31+
let rhs = *rhs;
32+
Some(Self::new(lhs.checked_sub_signed(rhs)?))
3333
}
3434
}
3535

@@ -59,8 +59,8 @@ impl core::ops::Sub<ColOffset> for ColIndex {
5959
#[inline]
6060
fn sub(self, rhs: ColOffset) -> Self::Output {
6161
let lhs = *self;
62-
let rhs = (*rhs).try_into().ok()?;
63-
Some(Self::new(lhs.checked_sub(rhs)?))
62+
let rhs = *rhs;
63+
Some(Self::new(lhs.checked_sub_signed(rhs)?))
6464
}
6565
}
6666

src/std/coord_arith/tests.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,32 @@ where
169169
let thr: usize = isize::MAX.try_into().unwrap();
170170
let tests = [
171171
//
172-
((0, -2_isize), None),
173-
((0, -1), None),
172+
((0, isize::MIN), Some(thr + 1)),
173+
((0, isize::MIN + 1), Some(thr)),
174+
((0, (isize::MIN + 2)), Some(thr - 1)),
175+
((0, -2_isize), Some(2)),
176+
((0, -1), Some(1)),
174177
((0, 0), Some(0_usize)),
175178
((0, 1), None),
176179
((0, 2), None),
177180
//
178-
((1, -2), None),
179-
((1, -1), None),
181+
((1, isize::MIN), Some(thr + 2)),
182+
((1, isize::MIN + 1), Some(thr + 1)),
183+
((1, (isize::MIN + 2)), Some(thr)),
184+
((1, (isize::MIN + 3)), Some(thr - 1)),
185+
((1, -2), Some(3)),
186+
((1, -1), Some(2)),
180187
((1, 0), Some(1)),
181188
((1, 1), Some(0)),
182189
((1, 2), None),
183190
((1, 3), None),
184191
//
185-
((thr - 1, -2), None),
186-
((thr - 1, -1), None),
192+
((thr - 1, isize::MIN), Some(2 * thr)),
193+
((thr - 1, isize::MIN + 1), Some(2 * thr - 1)),
194+
((thr - 1, (isize::MIN + 2)), Some(2 * thr - 2)),
195+
((thr - 1, (isize::MIN + 3)), Some(2 * thr - 3)),
196+
((thr - 1, -2), Some(thr + 1)),
197+
((thr - 1, -1), Some(thr)),
187198
((thr - 1, 0), Some(thr - 1)),
188199
((thr - 1, 1), Some(thr - 2)),
189200
((thr - 1, 2), Some(thr - 3)),
@@ -192,8 +203,12 @@ where
192203
((thr - 1, isize::MAX - 1), Some(0)),
193204
((thr - 1, isize::MAX), None),
194205
//
195-
((thr, -2), None),
196-
((thr, -1), None),
206+
((thr, isize::MIN), Some(2 * thr + 1)),
207+
((thr, isize::MIN + 1), Some(2 * thr)),
208+
((thr, (isize::MIN + 2)), Some(2 * thr - 1)),
209+
((thr, (isize::MIN + 3)), Some(2 * thr - 2)),
210+
((thr, -2), Some(thr + 2)),
211+
((thr, -1), Some(thr + 1)),
197212
((thr, 0), Some(thr)),
198213
((thr, 1), Some(thr - 1)),
199214
((thr, 2), Some(thr - 2)),
@@ -202,8 +217,12 @@ where
202217
((thr, isize::MAX - 1), Some(1)),
203218
((thr, isize::MAX), Some(0)),
204219
//
205-
((thr + 1, -2), None),
206-
((thr + 1, -1), None),
220+
((thr + 1, isize::MIN), None),
221+
((thr + 1, isize::MIN + 1), Some(2 * thr + 1)),
222+
((thr + 1, (isize::MIN + 2)), Some(2 * thr)),
223+
((thr + 1, (isize::MIN + 3)), Some(2 * thr - 1)),
224+
((thr + 1, -2), Some(thr + 3)),
225+
((thr + 1, -1), Some(thr + 2)),
207226
((thr + 1, 0), Some(thr + 1)),
208227
((thr + 1, 1), Some(thr)),
209228
((thr + 1, 2), Some(thr - 1)),
@@ -220,7 +239,11 @@ where
220239
#[test]
221240
fn rowindex_offset_test() {
222241
enumerate_offset_tests(|(a, b), c| {
223-
assert_eq!(RowIndex::new(a) - RowOffset::new(b), c.map(RowIndex::new));
242+
assert_eq!(
243+
RowIndex::new(a) - RowOffset::new(b),
244+
c.map(RowIndex::new),
245+
"{a} - {b}",
246+
);
224247
});
225248
}
226249

0 commit comments

Comments
 (0)