Skip to content

Commit d360a98

Browse files
committed
fix: slice of slice
1 parent 9bf08b0 commit d360a98

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "byteview"
33
description = "Thin, immutable zero-copy slice type"
44
license = "MIT OR Apache-2.0"
5-
version = "0.6.0"
5+
version = "0.6.1"
66
edition = "2021"
77
rust-version = "1.74"
88
readme = "README.md"

fuzz/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ libfuzzer-sys = "0.4"
1414
path = ".."
1515

1616
[[bin]]
17-
name = "fuzz_target_1"
18-
path = "fuzz_targets/fuzz_target_1.rs"
17+
name = "byteview"
18+
path = "fuzz_targets/byteview.rs"
1919
test = false
2020
doc = false
2121
bench = false

src/byteview.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ impl ByteView {
275275
Self::with_size_zeroed(slice_len)
276276
}
277277

278-
/// Fuses some byte slices into a single byteview.
278+
/// Fuses two byte slices into a single byteview.
279279
#[must_use]
280-
pub fn fused(slices: &[&[u8]]) -> Self {
281-
let len: usize = slices.iter().map(|x| x.len()).sum();
280+
pub fn fused(left: &[u8], right: &[u8]) -> Self {
281+
let len: usize = left.len() + right.len();
282282
let mut builder = Self::with_size_unchecked(len);
283283

284284
// NOTE:
@@ -290,9 +290,8 @@ impl ByteView {
290290
let mut mutator = builder.get_mut().expect("we are the owner");
291291
let mut mutator = &mut mutator[..];
292292

293-
for slice in slices {
294-
mutator.write_all(slice).expect("should write");
295-
}
293+
mutator.write_all(left).expect("should write");
294+
mutator.write_all(right).expect("should write");
296295
}
297296

298297
builder
@@ -593,7 +592,7 @@ impl ByteView {
593592
len,
594593
prefix: [0; PREFIX_SIZE],
595594
heap: unsafe { self.trailer.long.heap },
596-
offset: begin as u32,
595+
offset: unsafe { self.trailer.long.offset } + begin as u32,
597596
original_len: unsafe { self.trailer.long.original_len },
598597
}),
599598
},
@@ -850,30 +849,41 @@ mod tests {
850849
);
851850
}
852851

852+
#[test]
853+
fn sliced_clone() {
854+
let s = ByteView::from([
855+
1, 255, 255, 255, 251, 255, 255, 255, 255, 255, 1, 21, 255, 255, 255, 255, 5, 255, 255,
856+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 4, 3, 255,
857+
255, 0, 0, 255, 0, 0, 0, 254, 2, 0, 0, 0, 5, 2, 42, 0, 0, 0, 1, 0, 0, 0, 44, 0, 0, 0,
858+
2, 0, 0, 0,
859+
]);
860+
let slice = s.slice(12..(12 + 21));
861+
862+
#[allow(clippy::redundant_clone)]
863+
let cloned = slice.clone();
864+
865+
assert_eq!(slice.prefix(), cloned.prefix());
866+
assert_eq!(slice, cloned);
867+
}
868+
853869
#[test]
854870
fn fuse_empty() {
855-
let bytes = ByteView::fused(&[]);
871+
let bytes = ByteView::fused(&[], &[]);
856872
assert_eq!(&*bytes, []);
857873
}
858874

859875
#[test]
860876
fn fuse_one() {
861-
let bytes = ByteView::fused(&[b"abc"]);
877+
let bytes = ByteView::fused(b"abc", &[]);
862878
assert_eq!(&*bytes, b"abc");
863879
}
864880

865881
#[test]
866882
fn fuse_two() {
867-
let bytes = ByteView::fused(&[b"abc", b"def"]);
883+
let bytes = ByteView::fused(b"abc", b"def");
868884
assert_eq!(&*bytes, b"abcdef");
869885
}
870886

871-
#[test]
872-
fn fuse_four() {
873-
let bytes = ByteView::fused(&[b"abc", b"def", b"xyz", b"123"]);
874-
assert_eq!(&*bytes, b"abcdefxyz123");
875-
}
876-
877887
#[test]
878888
fn empty_slice() {
879889
let bytes = ByteView::with_size_zeroed(0);

0 commit comments

Comments
 (0)