1#![stable(feature = "rust1", since = "1.0.0")]
4
5#[stable(feature = "rust1", since = "1.0.0")]
6pub use core::borrow::{Borrow, BorrowMut};
7use core::cmp::Ordering;
8use core::hash::{Hash, Hasher};
9#[cfg(not(no_global_oom_handling))]
10use core::ops::{Add, AddAssign};
11use core::ops::{Deref, DerefPure};
12
13use Cow::*;
14
15use crate::fmt;
16#[cfg(not(no_global_oom_handling))]
17use crate::string::String;
18
19#[stable(feature = "rust1", since = "1.0.0")]
20impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
21where
22 B: ToOwned,
23{
24 fn borrow(&self) -> &B {
25 &**self
26 }
27}
28
29#[rustc_diagnostic_item = "ToOwned"]
36#[stable(feature = "rust1", since = "1.0.0")]
37pub trait ToOwned {
38 #[stable(feature = "rust1", since = "1.0.0")]
40 type Owned: Borrow<Self>;
41
42 #[stable(feature = "rust1", since = "1.0.0")]
56 #[must_use = "cloning is often expensive and is not expected to have side effects"]
57 #[rustc_diagnostic_item = "to_owned_method"]
58 fn to_owned(&self) -> Self::Owned;
59
60 #[stable(feature = "toowned_clone_into", since = "1.63.0")]
76 fn clone_into(&self, target: &mut Self::Owned) {
77 *target = self.to_owned();
78 }
79}
80
81#[stable(feature = "rust1", since = "1.0.0")]
82impl<T> ToOwned for T
83where
84 T: Clone,
85{
86 type Owned = T;
87 fn to_owned(&self) -> T {
88 self.clone()
89 }
90
91 fn clone_into(&self, target: &mut T) {
92 target.clone_from(self);
93 }
94}
95
96#[stable(feature = "rust1", since = "1.0.0")]
178#[rustc_diagnostic_item = "Cow"]
179pub enum Cow<'a, B: ?Sized + 'a>
180where
181 B: ToOwned,
182{
183 #[stable(feature = "rust1", since = "1.0.0")]
185 Borrowed(#[stable(feature = "rust1", since = "1.0.0")] &'a B),
186
187 #[stable(feature = "rust1", since = "1.0.0")]
189 Owned(#[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned>::Owned),
190}
191
192#[stable(feature = "rust1", since = "1.0.0")]
193impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
194 fn clone(&self) -> Self {
195 match *self {
196 Borrowed(b) => Borrowed(b),
197 Owned(ref o) => {
198 let b: &B = o.borrow();
199 Owned(b.to_owned())
200 }
201 }
202 }
203
204 fn clone_from(&mut self, source: &Self) {
205 match (self, source) {
206 (&mut Owned(ref mut dest), &Owned(ref o)) => o.borrow().clone_into(dest),
207 (t, s) => *t = s.clone(),
208 }
209 }
210}
211
212impl<B: ?Sized + ToOwned> Cow<'_, B> {
213 #[unstable(feature = "cow_is_borrowed", issue = "65143")]
228 pub const fn is_borrowed(&self) -> bool {
229 match *self {
230 Borrowed(_) => true,
231 Owned(_) => false,
232 }
233 }
234
235 #[unstable(feature = "cow_is_borrowed", issue = "65143")]
250 pub const fn is_owned(&self) -> bool {
251 !self.is_borrowed()
252 }
253
254 #[stable(feature = "rust1", since = "1.0.0")]
272 pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
273 match *self {
274 Borrowed(borrowed) => {
275 *self = Owned(borrowed.to_owned());
276 match *self {
277 Borrowed(..) => unreachable!(),
278 Owned(ref mut owned) => owned,
279 }
280 }
281 Owned(ref mut owned) => owned,
282 }
283 }
284
285 #[stable(feature = "rust1", since = "1.0.0")]
320 pub fn into_owned(self) -> <B as ToOwned>::Owned {
321 match self {
322 Borrowed(borrowed) => borrowed.to_owned(),
323 Owned(owned) => owned,
324 }
325 }
326}
327
328#[stable(feature = "rust1", since = "1.0.0")]
329impl<B: ?Sized + ToOwned> Deref for Cow<'_, B>
330where
331 B::Owned: Borrow<B>,
332{
333 type Target = B;
334
335 fn deref(&self) -> &B {
336 match *self {
337 Borrowed(borrowed) => borrowed,
338 Owned(ref owned) => owned.borrow(),
339 }
340 }
341}
342
343#[unstable(feature = "deref_pure_trait", issue = "87121")]
348unsafe impl<T: Clone> DerefPure for Cow<'_, T> {}
349#[cfg(not(no_global_oom_handling))]
350#[unstable(feature = "deref_pure_trait", issue = "87121")]
351unsafe impl DerefPure for Cow<'_, str> {}
352#[cfg(not(no_global_oom_handling))]
353#[unstable(feature = "deref_pure_trait", issue = "87121")]
354unsafe impl<T: Clone> DerefPure for Cow<'_, [T]> {}
355
356#[stable(feature = "rust1", since = "1.0.0")]
357impl<B: ?Sized> Eq for Cow<'_, B> where B: Eq + ToOwned {}
358
359#[stable(feature = "rust1", since = "1.0.0")]
360impl<B: ?Sized> Ord for Cow<'_, B>
361where
362 B: Ord + ToOwned,
363{
364 #[inline]
365 fn cmp(&self, other: &Self) -> Ordering {
366 Ord::cmp(&**self, &**other)
367 }
368}
369
370#[stable(feature = "rust1", since = "1.0.0")]
371impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B>
372where
373 B: PartialEq<C> + ToOwned,
374 C: ToOwned,
375{
376 #[inline]
377 fn eq(&self, other: &Cow<'b, C>) -> bool {
378 PartialEq::eq(&**self, &**other)
379 }
380}
381
382#[stable(feature = "rust1", since = "1.0.0")]
383impl<'a, B: ?Sized> PartialOrd for Cow<'a, B>
384where
385 B: PartialOrd + ToOwned,
386{
387 #[inline]
388 fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> {
389 PartialOrd::partial_cmp(&**self, &**other)
390 }
391}
392
393#[stable(feature = "rust1", since = "1.0.0")]
394impl<B: ?Sized> fmt::Debug for Cow<'_, B>
395where
396 B: fmt::Debug + ToOwned<Owned: fmt::Debug>,
397{
398 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
399 match *self {
400 Borrowed(ref b) => fmt::Debug::fmt(b, f),
401 Owned(ref o) => fmt::Debug::fmt(o, f),
402 }
403 }
404}
405
406#[stable(feature = "rust1", since = "1.0.0")]
407impl<B: ?Sized> fmt::Display for Cow<'_, B>
408where
409 B: fmt::Display + ToOwned<Owned: fmt::Display>,
410{
411 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
412 match *self {
413 Borrowed(ref b) => fmt::Display::fmt(b, f),
414 Owned(ref o) => fmt::Display::fmt(o, f),
415 }
416 }
417}
418
419#[stable(feature = "default", since = "1.11.0")]
420impl<B: ?Sized> Default for Cow<'_, B>
421where
422 B: ToOwned<Owned: Default>,
423{
424 fn default() -> Self {
426 Owned(<B as ToOwned>::Owned::default())
427 }
428}
429
430#[stable(feature = "rust1", since = "1.0.0")]
431impl<B: ?Sized> Hash for Cow<'_, B>
432where
433 B: Hash + ToOwned,
434{
435 #[inline]
436 fn hash<H: Hasher>(&self, state: &mut H) {
437 Hash::hash(&**self, state)
438 }
439}
440
441#[stable(feature = "rust1", since = "1.0.0")]
442impl<T: ?Sized + ToOwned> AsRef<T> for Cow<'_, T> {
443 fn as_ref(&self) -> &T {
444 self
445 }
446}
447
448#[cfg(not(no_global_oom_handling))]
449#[stable(feature = "cow_add", since = "1.14.0")]
450impl<'a> Add<&'a str> for Cow<'a, str> {
451 type Output = Cow<'a, str>;
452
453 #[inline]
454 fn add(mut self, rhs: &'a str) -> Self::Output {
455 self += rhs;
456 self
457 }
458}
459
460#[cfg(not(no_global_oom_handling))]
461#[stable(feature = "cow_add", since = "1.14.0")]
462impl<'a> Add<Cow<'a, str>> for Cow<'a, str> {
463 type Output = Cow<'a, str>;
464
465 #[inline]
466 fn add(mut self, rhs: Cow<'a, str>) -> Self::Output {
467 self += rhs;
468 self
469 }
470}
471
472#[cfg(not(no_global_oom_handling))]
473#[stable(feature = "cow_add", since = "1.14.0")]
474impl<'a> AddAssign<&'a str> for Cow<'a, str> {
475 fn add_assign(&mut self, rhs: &'a str) {
476 if self.is_empty() {
477 *self = Cow::Borrowed(rhs)
478 } else if !rhs.is_empty() {
479 if let Cow::Borrowed(lhs) = *self {
480 let mut s = String::with_capacity(lhs.len() + rhs.len());
481 s.push_str(lhs);
482 *self = Cow::Owned(s);
483 }
484 self.to_mut().push_str(rhs);
485 }
486 }
487}
488
489#[cfg(not(no_global_oom_handling))]
490#[stable(feature = "cow_add", since = "1.14.0")]
491impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> {
492 fn add_assign(&mut self, rhs: Cow<'a, str>) {
493 if self.is_empty() {
494 *self = rhs
495 } else if !rhs.is_empty() {
496 if let Cow::Borrowed(lhs) = *self {
497 let mut s = String::with_capacity(lhs.len() + rhs.len());
498 s.push_str(lhs);
499 *self = Cow::Owned(s);
500 }
501 self.to_mut().push_str(&rhs);
502 }
503 }
504}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4