mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2025-02-23 14:26:23 +00:00
gstreamer: Implement ByteSliceExt
generally over all AsRef<[u8]>
That covers `&[u8]` and `&mut [u8]` as well as `[u8]`, `[u8; N]`, `Vec<u8>` and many other types. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1651>
This commit is contained in:
parent
8eb8ab921b
commit
6fc5984325
1 changed files with 27 additions and 16 deletions
|
@ -12,28 +12,14 @@ pub trait ByteSliceExt {
|
|||
fn dump_range(&self, range: impl RangeBounds<usize>) -> Dump;
|
||||
}
|
||||
|
||||
impl ByteSliceExt for &[u8] {
|
||||
impl<T: AsRef<[u8]>> ByteSliceExt for T {
|
||||
fn dump(&self) -> Dump {
|
||||
self.dump_range(..)
|
||||
}
|
||||
|
||||
fn dump_range(&self, range: impl RangeBounds<usize>) -> 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<usize>) -> 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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue