1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{Bstr, Ustr};
use std::{
    borrow::Cow,
    ffi::{CStr, OsStr},
    ops::Deref,
    os::unix::ffi::OsStrExt,
    path::Path,
};

/// Trait for objects which can be turned into bytes
///
/// This is mostly an internal API.
pub trait Bytes {
    fn bytes(&self) -> &[u8];
}

impl Bytes for Bstr {
    fn bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<'a> Bytes for Cow<'a, Ustr> {
    fn bytes(&self) -> &[u8] {
        self.deref().as_bytes()
    }
}

impl Bytes for [u8] {
    fn bytes(&self) -> &[u8] {
        self
    }
}

impl Bytes for str {
    fn bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl Bytes for CStr {
    fn bytes(&self) -> &[u8] {
        self.to_bytes()
    }
}

impl Bytes for OsStr {
    fn bytes(&self) -> &[u8] {
        OsStrExt::as_bytes(self)
    }
}

impl Bytes for Path {
    fn bytes(&self) -> &[u8] {
        OsStrExt::as_bytes(self.as_os_str())
    }
}