Thanks to visit codestin.com
Credit goes to docs.rs

uapi/ustr/
bytes.rs

1use crate::{Bstr, Ustr};
2use std::{
3    borrow::Cow,
4    ffi::{CStr, OsStr},
5    ops::Deref,
6    os::unix::ffi::OsStrExt,
7    path::Path,
8};
9
10/// Trait for objects which can be turned into bytes
11///
12/// This is mostly an internal API.
13pub trait Bytes {
14    fn bytes(&self) -> &[u8];
15}
16
17impl Bytes for Bstr {
18    fn bytes(&self) -> &[u8] {
19        self.as_bytes()
20    }
21}
22
23impl<'a> Bytes for Cow<'a, Ustr> {
24    fn bytes(&self) -> &[u8] {
25        self.deref().as_bytes()
26    }
27}
28
29impl Bytes for [u8] {
30    fn bytes(&self) -> &[u8] {
31        self
32    }
33}
34
35impl Bytes for str {
36    fn bytes(&self) -> &[u8] {
37        self.as_bytes()
38    }
39}
40
41impl Bytes for CStr {
42    fn bytes(&self) -> &[u8] {
43        self.to_bytes()
44    }
45}
46
47impl Bytes for OsStr {
48    fn bytes(&self) -> &[u8] {
49        OsStrExt::as_bytes(self)
50    }
51}
52
53impl Bytes for Path {
54    fn bytes(&self) -> &[u8] {
55        OsStrExt::as_bytes(self.as_os_str())
56    }
57}