Skip to content

Commit ad1e4da

Browse files
TerryTaoYYghedo
authored andcommitted
octets: fix slice_last when offset is non-zero
1 parent 971ec56 commit ad1e4da

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

octets/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ impl<'a> Octets<'a> {
332332
return Err(BufferTooShortError);
333333
}
334334

335-
let cap = self.cap();
336-
Ok(&self.buf[cap - len..])
335+
let end = self.buf.len();
336+
Ok(&self.buf[end - len..end])
337337
}
338338

339339
/// Advances the buffer's offset.
@@ -747,8 +747,8 @@ impl<'a> OctetsMut<'a> {
747747
return Err(BufferTooShortError);
748748
}
749749

750-
let cap = self.cap();
751-
Ok(&mut self.buf[cap - len..])
750+
let end = self.buf.len();
751+
Ok(&mut self.buf[end - len..end])
752752
}
753753

754754
/// Advances the buffer's offset.
@@ -1488,6 +1488,13 @@ mod tests {
14881488
assert_eq!(b.slice_last(4), Ok(&exp[..]));
14891489
}
14901490

1491+
{
1492+
let mut b = Octets::with_slice(&d);
1493+
b.get_bytes(5).unwrap();
1494+
let exp = b"orld".to_vec();
1495+
assert_eq!(b.slice_last(4), Ok(&exp[..]));
1496+
}
1497+
14911498
{
14921499
let b = Octets::with_slice(&d);
14931500
let exp = b"d".to_vec();
@@ -1522,6 +1529,13 @@ mod tests {
15221529
assert_eq!(b.slice_last(4), Ok(&mut exp[..]));
15231530
}
15241531

1532+
{
1533+
let mut b = OctetsMut::with_slice(&mut d);
1534+
b.get_bytes(5).unwrap();
1535+
let mut exp = b"orld".to_vec();
1536+
assert_eq!(b.slice_last(4), Ok(&mut exp[..]));
1537+
}
1538+
15251539
{
15261540
let mut b = OctetsMut::with_slice(&mut d);
15271541
let mut exp = b"d".to_vec();

0 commit comments

Comments
 (0)