Coverage Report

Created: 2025-12-31 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/memchr-2.5.0/src/cow.rs
Line
Count
Source
1
use core::ops;
2
3
/// A specialized copy-on-write byte string.
4
///
5
/// The purpose of this type is to permit usage of a "borrowed or owned
6
/// byte string" in a way that keeps std/no-std compatibility. That is, in
7
/// no-std mode, this type devolves into a simple &[u8] with no owned variant
8
/// available. We can't just use a plain Cow because Cow is not in core.
9
#[derive(Clone, Debug)]
10
pub struct CowBytes<'a>(Imp<'a>);
11
12
// N.B. We don't use std::borrow::Cow here since we can get away with a
13
// Box<[u8]> for our use case, which is 1/3 smaller than the Vec<u8> that
14
// a Cow<[u8]> would use.
15
#[cfg(feature = "std")]
16
#[derive(Clone, Debug)]
17
enum Imp<'a> {
18
    Borrowed(&'a [u8]),
19
    Owned(Box<[u8]>),
20
}
21
22
#[cfg(not(feature = "std"))]
23
#[derive(Clone, Debug)]
24
struct Imp<'a>(&'a [u8]);
25
26
impl<'a> ops::Deref for CowBytes<'a> {
27
    type Target = [u8];
28
29
    #[inline(always)]
30
0
    fn deref(&self) -> &[u8] {
31
0
        self.as_slice()
32
0
    }
33
}
34
35
impl<'a> CowBytes<'a> {
36
    /// Create a new borrowed CowBytes.
37
    #[inline(always)]
38
0
    pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &'a B) -> CowBytes<'a> {
39
0
        CowBytes(Imp::new(bytes.as_ref()))
40
0
    }
41
42
    /// Create a new owned CowBytes.
43
    #[cfg(feature = "std")]
44
    #[inline(always)]
45
0
    pub fn new_owned(bytes: Box<[u8]>) -> CowBytes<'static> {
46
0
        CowBytes(Imp::Owned(bytes))
47
0
    }
48
49
    /// Return a borrowed byte string, regardless of whether this is an owned
50
    /// or borrowed byte string internally.
51
    #[inline(always)]
52
0
    pub fn as_slice(&self) -> &[u8] {
53
0
        self.0.as_slice()
54
0
    }
55
56
    /// Return an owned version of this copy-on-write byte string.
57
    ///
58
    /// If this is already an owned byte string internally, then this is a
59
    /// no-op. Otherwise, the internal byte string is copied.
60
    #[cfg(feature = "std")]
61
    #[inline(always)]
62
0
    pub fn into_owned(self) -> CowBytes<'static> {
63
0
        match self.0 {
64
0
            Imp::Borrowed(b) => CowBytes::new_owned(Box::from(b)),
65
0
            Imp::Owned(b) => CowBytes::new_owned(b),
66
        }
67
0
    }
68
}
69
70
impl<'a> Imp<'a> {
71
    #[cfg(feature = "std")]
72
    #[inline(always)]
73
0
    pub fn new(bytes: &'a [u8]) -> Imp<'a> {
74
0
        Imp::Borrowed(bytes)
75
0
    }
76
77
    #[cfg(not(feature = "std"))]
78
    #[inline(always)]
79
    pub fn new(bytes: &'a [u8]) -> Imp<'a> {
80
        Imp(bytes)
81
    }
82
83
    #[cfg(feature = "std")]
84
    #[inline(always)]
85
0
    pub fn as_slice(&self) -> &[u8] {
86
0
        match self {
87
0
            Imp::Owned(ref x) => x,
88
0
            Imp::Borrowed(x) => x,
89
        }
90
0
    }
91
92
    #[cfg(not(feature = "std"))]
93
    #[inline(always)]
94
    pub fn as_slice(&self) -> &[u8] {
95
        self.0
96
    }
97
}