diff --git a/src/buf/cursor.rs b/src/buf/cursor.rs index 26fab5bce..a554abb6d 100644 --- a/src/buf/cursor.rs +++ b/src/buf/cursor.rs @@ -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()), + } + } +} diff --git a/tests/test_cursor.rs b/tests/test_cursor.rs index 6e28768ea..1a2c7d704 100644 --- a/tests/test_cursor.rs +++ b/tests/test_cursor.rs @@ -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();