jiff/util/
borrow.rs
1#[derive(Clone, Debug)]
12pub(crate) enum DumbCow<'a, T> {
13 Owned(T),
14 Borrowed(&'a T),
15}
16
17impl<'a, T> DumbCow<'a, T> {
18 pub(crate) fn borrowed(&self) -> DumbCow<'_, T> {
19 match *self {
20 DumbCow::Owned(ref this) => DumbCow::Borrowed(this),
21 DumbCow::Borrowed(ref this) => DumbCow::Borrowed(this),
22 }
23 }
24}
25
26impl<'a, T> core::ops::Deref for DumbCow<'a, T> {
27 type Target = T;
28 fn deref(&self) -> &T {
29 match *self {
30 DumbCow::Owned(ref t) => t,
31 DumbCow::Borrowed(t) => t,
32 }
33 }
34}
35
36impl<'a, T: core::fmt::Display> core::fmt::Display for DumbCow<'a, T> {
37 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
38 core::fmt::Display::fmt(core::ops::Deref::deref(self), f)
39 }
40}
41
42#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
46pub(crate) enum StringCow<'a> {
47 #[cfg(feature = "alloc")]
48 Owned(alloc::string::String),
49 Borrowed(&'a str),
50}
51
52impl<'a> StringCow<'a> {
53 pub(crate) fn as_str<'s>(&'s self) -> &'s str {
59 match *self {
60 #[cfg(feature = "alloc")]
61 StringCow::Owned(ref s) => s,
62 StringCow::Borrowed(s) => s,
63 }
64 }
65
66 #[cfg(feature = "alloc")]
70 pub(crate) fn into_owned(self) -> StringCow<'static> {
71 use alloc::string::ToString;
72
73 match self {
74 StringCow::Owned(string) => StringCow::Owned(string),
75 StringCow::Borrowed(string) => {
76 StringCow::Owned(string.to_string())
77 }
78 }
79 }
80}
81
82impl<'a> core::ops::Deref for StringCow<'a> {
83 type Target = str;
84 fn deref(&self) -> &str {
85 self.as_str()
86 }
87}
88
89impl<'a> core::fmt::Display for StringCow<'a> {
90 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
91 core::fmt::Display::fmt(self.as_str(), f)
92 }
93}
94
95#[cfg(feature = "alloc")]
96impl From<alloc::string::String> for StringCow<'static> {
97 fn from(string: alloc::string::String) -> StringCow<'static> {
98 StringCow::Owned(string)
99 }
100}
101
102impl<'a> From<&'a str> for StringCow<'a> {
103 fn from(string: &'a str) -> StringCow<'a> {
104 StringCow::Borrowed(string)
105 }
106}