]>
git.proxmox.com Git - rustc.git/blob - vendor/kstring/src/string_cow.rs
1 use std
::{borrow::Cow, fmt}
;
3 use crate::KStringBase
;
5 use crate::KStringRefInner
;
7 type StdString
= std
::string
::String
;
8 type BoxedStr
= Box
<str>;
10 /// A reference to a UTF-8 encoded, immutable string.
11 pub type KStringCow
<'s
> = KStringCowBase
<'s
, crate::backend
::DefaultStr
>;
13 /// A reference to a UTF-8 encoded, immutable string.
16 pub struct KStringCowBase
<'s
, B
= crate::backend
::DefaultStr
> {
17 pub(crate) inner
: KStringCowInner
<'s
, B
>,
21 pub(crate) enum KStringCowInner
<'s
, B
> {
23 Owned(KStringBase
<B
>),
26 impl<'s
, B
> KStringCowBase
<'s
, B
> {
27 /// Create a new empty `KStringCowBase`.
30 pub const fn new() -> Self {
34 /// Create a reference to a `'static` data.
37 pub const fn from_static(other
: &'
static str) -> Self {
39 inner
: KStringCowInner
::Owned(KStringBase
::from_static(other
)),
44 impl<'s
, B
: crate::backend
::HeapStr
> KStringCowBase
<'s
, B
> {
45 /// Create an owned `KStringCowBase`.
48 pub fn from_boxed(other
: BoxedStr
) -> Self {
50 inner
: KStringCowInner
::Owned(KStringBase
::from_boxed(other
)),
54 /// Create an owned `KStringCowBase`.
57 pub fn from_string(other
: StdString
) -> Self {
59 inner
: KStringCowInner
::Owned(KStringBase
::from_string(other
)),
63 /// Create a reference to a borrowed data.
66 pub fn from_ref(other
: &'s
str) -> Self {
68 inner
: KStringCowInner
::Borrowed(other
),
72 /// Get a reference to the `KStringBase`.
75 pub fn as_ref(&self) -> KStringRef
<'_
> {
79 /// Clone the data into an owned-type.
82 pub fn into_owned(self) -> KStringBase
<B
> {
83 self.inner
.into_owned()
86 /// Extracts a string slice containing the entire `KStringCowBase`.
89 pub fn as_str(&self) -> &str {
93 /// Convert to a mutable string type, cloning the data if necessary.
96 pub fn into_string(self) -> StdString
{
97 String
::from(self.into_boxed_str())
100 /// Convert to a mutable string type, cloning the data if necessary.
103 pub fn into_boxed_str(self) -> BoxedStr
{
104 self.inner
.into_boxed_str()
107 /// Convert to a Cow str
110 pub fn into_cow_str(self) -> Cow
<'s
, str> {
111 self.inner
.into_cow_str()
115 impl<'s
, B
: crate::backend
::HeapStr
> KStringCowInner
<'s
, B
> {
117 fn as_ref(&self) -> KStringRef
<'_
> {
119 Self::Borrowed(s
) => KStringRef
::from_ref(s
),
120 Self::Owned(s
) => s
.as_ref(),
125 fn into_owned(self) -> KStringBase
<B
> {
127 Self::Borrowed(s
) => KStringBase
::from_ref(s
),
133 fn as_str(&self) -> &str {
135 Self::Borrowed(s
) => s
,
136 Self::Owned(s
) => s
.as_str(),
141 fn into_boxed_str(self) -> BoxedStr
{
143 Self::Borrowed(s
) => BoxedStr
::from(s
),
144 Self::Owned(s
) => s
.into_boxed_str(),
148 /// Convert to a Cow str
150 fn into_cow_str(self) -> Cow
<'s
, str> {
152 Self::Borrowed(s
) => Cow
::Borrowed(s
),
153 Self::Owned(s
) => s
.into_cow_str(),
158 impl<'s
, B
: crate::backend
::HeapStr
> std
::ops
::Deref
for KStringCowBase
<'s
, B
> {
162 fn deref(&self) -> &str {
167 impl<'s
, B
: crate::backend
::HeapStr
> Eq
for KStringCowBase
<'s
, B
> {}
169 impl<'s
, B
: crate::backend
::HeapStr
> PartialEq
<KStringCowBase
<'s
, B
>> for KStringCowBase
<'s
, B
> {
171 fn eq(&self, other
: &KStringCowBase
<'s
, B
>) -> bool
{
172 PartialEq
::eq(self.as_str(), other
.as_str())
176 impl<'s
, B
: crate::backend
::HeapStr
> PartialEq
<str> for KStringCowBase
<'s
, B
> {
178 fn eq(&self, other
: &str) -> bool
{
179 PartialEq
::eq(self.as_str(), other
)
183 impl<'s
, B
: crate::backend
::HeapStr
> PartialEq
<&'s
str> for KStringCowBase
<'s
, B
> {
185 fn eq(&self, other
: &&str) -> bool
{
186 PartialEq
::eq(self.as_str(), *other
)
190 impl<'s
, B
: crate::backend
::HeapStr
> PartialEq
<String
> for KStringCowBase
<'s
, B
> {
192 fn eq(&self, other
: &StdString
) -> bool
{
193 PartialEq
::eq(self.as_str(), other
.as_str())
197 impl<'s
, B
: crate::backend
::HeapStr
> Ord
for KStringCowBase
<'s
, B
> {
199 fn cmp(&self, other
: &Self) -> std
::cmp
::Ordering
{
200 self.as_str().cmp(other
.as_str())
204 impl<'s
, B
: crate::backend
::HeapStr
> PartialOrd
for KStringCowBase
<'s
, B
> {
206 fn partial_cmp(&self, other
: &Self) -> Option
<std
::cmp
::Ordering
> {
207 self.as_str().partial_cmp(other
.as_str())
211 impl<'s
, B
: crate::backend
::HeapStr
> std
::hash
::Hash
for KStringCowBase
<'s
, B
> {
213 fn hash
<H
: std
::hash
::Hasher
>(&self, state
: &mut H
) {
214 self.as_str().hash(state
);
218 impl<'s
, B
: crate::backend
::HeapStr
> fmt
::Debug
for KStringCowBase
<'s
, B
> {
220 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
225 impl<'s
, B
: crate::backend
::HeapStr
> fmt
::Display
for KStringCowBase
<'s
, B
> {
227 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
228 fmt
::Display
::fmt(self.as_str(), f
)
232 impl<'s
, B
: crate::backend
::HeapStr
> AsRef
<str> for KStringCowBase
<'s
, B
> {
234 fn as_ref(&self) -> &str {
239 impl<'s
, B
: crate::backend
::HeapStr
> AsRef
<[u8]> for KStringCowBase
<'s
, B
> {
241 fn as_ref(&self) -> &[u8] {
246 impl<'s
, B
: crate::backend
::HeapStr
> AsRef
<std
::ffi
::OsStr
> for KStringCowBase
<'s
, B
> {
248 fn as_ref(&self) -> &std
::ffi
::OsStr
{
253 impl<'s
, B
: crate::backend
::HeapStr
> AsRef
<std
::path
::Path
> for KStringCowBase
<'s
, B
> {
255 fn as_ref(&self) -> &std
::path
::Path
{
256 std
::path
::Path
::new(self)
260 impl<'s
, B
: crate::backend
::HeapStr
> std
::borrow
::Borrow
<str> for KStringCowBase
<'s
, B
> {
262 fn borrow(&self) -> &str {
267 impl<'s
, B
> Default
for KStringCowBase
<'s
, B
> {
269 fn default() -> Self {
274 impl<'s
, B
: crate::backend
::HeapStr
> From
<KStringBase
<B
>> for KStringCowBase
<'s
, B
> {
276 fn from(other
: KStringBase
<B
>) -> Self {
277 let inner
= KStringCowInner
::Owned(other
);
282 impl<'s
, B
: crate::backend
::HeapStr
> From
<&'s KStringBase
<B
>> for KStringCowBase
<'s
, B
> {
284 fn from(other
: &'s KStringBase
<B
>) -> Self {
285 let other
= other
.as_ref();
290 impl<'s
, B
: crate::backend
::HeapStr
> From
<KStringRef
<'s
>> for KStringCowBase
<'s
, B
> {
292 fn from(other
: KStringRef
<'s
>) -> Self {
294 KStringRefInner
::Borrowed(s
) => Self::from_ref(s
),
295 KStringRefInner
::Singleton(s
) => Self::from_static(s
),
300 impl<'s
, B
: crate::backend
::HeapStr
> From
<&'s KStringRef
<'s
>> for KStringCowBase
<'s
, B
> {
302 fn from(other
: &'s KStringRef
<'s
>) -> Self {
304 KStringRefInner
::Borrowed(s
) => Self::from_ref(s
),
305 KStringRefInner
::Singleton(s
) => Self::from_static(s
),
310 impl<'s
, B
: crate::backend
::HeapStr
> From
<StdString
> for KStringCowBase
<'s
, B
> {
312 fn from(other
: StdString
) -> Self {
313 Self::from_string(other
)
317 impl<'s
, B
: crate::backend
::HeapStr
> From
<&'s StdString
> for KStringCowBase
<'s
, B
> {
319 fn from(other
: &'s StdString
) -> Self {
320 Self::from_ref(other
.as_str())
324 impl<'s
, B
: crate::backend
::HeapStr
> From
<BoxedStr
> for KStringCowBase
<'s
, B
> {
326 fn from(other
: BoxedStr
) -> Self {
327 // Since the memory is already allocated, don't bother moving it into a FixedString
328 Self::from_boxed(other
)
332 impl<'s
, B
: crate::backend
::HeapStr
> From
<&'s BoxedStr
> for KStringCowBase
<'s
, B
> {
334 fn from(other
: &'s BoxedStr
) -> Self {
335 Self::from_ref(other
)
339 impl<'s
, B
: crate::backend
::HeapStr
> From
<&'s
str> for KStringCowBase
<'s
, B
> {
341 fn from(other
: &'s
str) -> Self {
342 Self::from_ref(other
)
346 impl<B
: crate::backend
::HeapStr
> std
::str::FromStr
for KStringCowBase
<'_
, B
> {
347 type Err
= std
::convert
::Infallible
;
349 fn from_str(s
: &str) -> Result
<Self, Self::Err
> {
350 Ok(Self::from_string(s
.into()))
354 #[cfg(feature = "serde")]
355 impl<'s
, B
: crate::backend
::HeapStr
> serde
::Serialize
for KStringCowBase
<'s
, B
> {
357 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
359 S
: serde
::Serializer
,
361 serializer
.serialize_str(self.as_str())
365 #[cfg(feature = "serde")]
366 impl<'de
, 's
, B
: crate::backend
::HeapStr
> serde
::Deserialize
<'de
> for KStringCowBase
<'s
, B
> {
367 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
369 D
: serde
::Deserializer
<'de
>,
371 KStringBase
::deserialize(deserializer
).map(|s
| s
.into())
381 println
!("KStringCow: {}", std
::mem
::size_of
::<KStringCow
<'
static>>());