From 6fc5984325a717b10dfd27ebc33916a6f1393e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 20 Feb 2025 13:50:20 +0200 Subject: [PATCH] gstreamer: Implement `ByteSliceExt` generally over all `AsRef<[u8]>` That covers `&[u8]` and `&mut [u8]` as well as `[u8]`, `[u8; N]`, `Vec` and many other types. Part-of: --- gstreamer/src/slice.rs | 43 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/gstreamer/src/slice.rs b/gstreamer/src/slice.rs index 0730c9f0d..f968773e7 100644 --- a/gstreamer/src/slice.rs +++ b/gstreamer/src/slice.rs @@ -12,28 +12,14 @@ pub trait ByteSliceExt { fn dump_range(&self, range: impl RangeBounds) -> Dump; } -impl ByteSliceExt for &[u8] { +impl> ByteSliceExt for T { fn dump(&self) -> Dump { self.dump_range(..) } fn dump_range(&self, range: impl RangeBounds) -> Dump { Dump { - data: self, - start: range.start_bound().cloned(), - end: range.end_bound().cloned(), - } - } -} - -impl ByteSliceExt for &mut [u8] { - fn dump(&self) -> Dump { - self.dump_range(..) - } - - fn dump_range(&self, range: impl RangeBounds) -> Dump { - Dump { - data: self, + data: self.as_ref(), start: range.start_bound().cloned(), end: range.end_bound().cloned(), } @@ -159,3 +145,28 @@ impl fmt::Debug for Dump<'_> { self.fmt(f, true) } } + +#[cfg(test)] +mod tests { + use super::ByteSliceExt; + + #[test] + fn dump_u8_slice() { + let mut b: [u8; 4] = [1u8, 2, 3, 4]; + let _ = b.dump(); + let b1 = b.as_slice(); + let _ = b1.dump(); + let b1 = b.as_mut_slice(); + let _ = b1.dump(); + } + + #[test] + fn dump_u8_vec() { + let mut b = vec![1u8, 2, 3, 4]; + let _ = b.dump(); + let b1 = b.as_slice(); + let _ = b1.dump(); + let b1 = b.as_mut_slice(); + let _ = b1.dump(); + } +}