Skip to content

Commit 67a3e4b

Browse files
authoredOct 6, 2023
Merge pull request #340 from epage/custom
fix(stream): Correctly count offsets for non-byte slices
2 parents 552e417 + 68977e5 commit 67a3e4b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎src/stream/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ impl<'a, T> Offset for &'a [T] {
13001300
fst <= snd,
13011301
"`Offset::offset_to` only accepts slices of `self`"
13021302
);
1303-
snd as usize - fst as usize
1303+
(snd as usize - fst as usize) / crate::lib::std::mem::size_of::<T>()
13041304
}
13051305
}
13061306

‎src/stream/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,18 @@ fn test_partial_complete() {
114114
i.restore_partial(incomplete_state);
115115
assert!(i.is_partial(), "incomplete stream state should be restored");
116116
}
117+
118+
#[test]
119+
fn test_custom_slice() {
120+
type Token = usize;
121+
type TokenSlice<'i> = &'i [Token];
122+
123+
let mut tokens: TokenSlice<'_> = &[1, 2, 3, 4];
124+
125+
let input = &mut tokens;
126+
let start = input.checkpoint();
127+
let _ = input.next_token();
128+
let _ = input.next_token();
129+
let offset = input.offset_from(&start);
130+
assert_eq!(offset, 2);
131+
}

0 commit comments

Comments
 (0)