Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support copy_within on empty ranges #237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/slice/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,12 @@ where
where R: RangeExt<usize> {
let len = self.len();
let src = src.normalize(0, len);
// prevent the asserts below from failing for empty ranges
if src.start == src.end {
assert!(src.start <= len, "range start index {} is out of bounds for bit-slice of length {}", src.start, len);
assert!(dest <= len, "index {} is out of bounds for bit-slice of length {}", dest, len);
return;
}
self.assert_in_bounds(src.start, 0 .. len);
self.assert_in_bounds(src.end, 0 ..= len);
self.assert_in_bounds(dest, 0 .. len);
Expand Down
25 changes: 25 additions & 0 deletions src/slice/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ fn copying() {
}
}

// regression tests for #236
#[test]
fn copy_within_supports_empty_ranges() {
let empty = bits![mut u8, Lsb0; 0; 0];
empty.copy_within(0..0, 0);
assert_eq!(empty, bits![]);

let non_empty = bits![mut u8, Lsb0; 0; 3];
non_empty.copy_within(2..2, 0);
assert_eq!(non_empty, bits![0, 0, 0]);
}
#[test]
#[should_panic]
fn copy_within_panics_if_empty_range_exceeds_bounds() {
let empty_slice = bits![mut u8, Lsb0; 0; 0];
empty_slice.copy_within(2..2, 0);
}
#[test]
#[should_panic]
fn copy_within_panics_if_dest_for_empty_range_exceeds_bounds() {
let empty_slice = bits![mut u8, Lsb0; 0; 0];
empty_slice.copy_within(0..0, 1);
}


#[test]
fn writing() {
let bits = bits![mut 0; 2];
Expand Down