Skip to content

Commit

Permalink
Add Clone impl to BufCursor
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanplecki committed Jan 26, 2024
1 parent 7336b06 commit 31142d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/buf/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,15 @@ impl<'b, B: SeekBuf + ?Sized> DoubleEndedIterator for BufCursor<'b, B> {
impl<'b, B: SeekBuf + ?Sized> FusedIterator for BufCursor<'b, B> {}

impl<'b, B: SeekBuf + ?Sized> ExactSizeIterator for BufCursor<'b, B> {}

impl<'b, B: SeekBuf + ?Sized> Clone for BufCursor<'b, B> {
fn clone(&self) -> Self {
Self {
buf: self.buf,
front_chunk_offset: Cell::new(self.front_chunk_offset.get()),
back_chunk_offset: Cell::new(self.back_chunk_offset.get()),
front_chunk: Cell::new(self.front_chunk.get()),
back_chunk: Cell::new(self.back_chunk.get()),
}
}
}
18 changes: 18 additions & 0 deletions tests/test_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ fn test_iterator() {
assert_eq!(cursor.next_back(), None);
}

#[test]
fn test_clone() {
let buf = b"Hello World!".as_slice();

let mut cursor = buf.cursor();

assert_eq!(cursor.next(), Some(&b'H'));
assert_eq!(cursor.next_back(), Some(&b'!'));

let mut cursor_clone = cursor.clone();

assert_eq!(cursor.next(), Some(&b'e'));
assert_eq!(cursor.next_back(), Some(&b'd'));

assert_eq!(cursor_clone.next(), Some(&b'e'));
assert_eq!(cursor_clone.next_back(), Some(&b'd'));
}

#[test]
fn test_seek() {
let buf = b"<<< TEXT >>>".as_slice();
Expand Down

0 comments on commit 31142d3

Please sign in to comment.