diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2014-11-21 17:10:42 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2014-11-25 11:22:23 -0500 |
| commit | 3293ab14e24d136d0482bb18afef577aebed251e (patch) | |
| tree | c21d2568d6eaf1cc1835034cf2557277c4e8d58b /src/libcore | |
| parent | 48ca6d1840818e4a8977d00ed62cf0e8e0e5d193 (diff) | |
| download | rust-3293ab14e24d136d0482bb18afef577aebed251e.tar.gz rust-3293ab14e24d136d0482bb18afef577aebed251e.zip | |
Deprecate MaybeOwned[Vector] in favor of Cow
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/borrow.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/libcore/borrow.rs b/src/libcore/borrow.rs index da0e23e1a5e..06fda8d6092 100644 --- a/src/libcore/borrow.rs +++ b/src/libcore/borrow.rs @@ -45,8 +45,11 @@ #![unstable = "recently added as part of collections reform"] use clone::Clone; +use cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; +use fmt; use kinds::Sized; use ops::Deref; +use option::Option; use self::Cow::*; /// A trait for borrowing data. @@ -81,6 +84,24 @@ impl<'a, Sized? T> BorrowFromMut<&'a mut T> for T { fn borrow_from_mut<'b>(owned: &'b mut &'a mut T) -> &'b mut T { &mut **owned } } +impl<'a, T, Sized? B> BorrowFrom<Cow<'a, T, B>> for B where B: ToOwned<T> { + fn borrow_from<'b>(owned: &'b Cow<'a, T, B>) -> &'b B { + &**owned + } +} + +/// Trait for moving into a `Cow` +pub trait IntoCow<'a, T, Sized? B> { + /// Moves `self` into `Cow` + fn into_cow(self) -> Cow<'a, T, B>; +} + +impl<'a, T, Sized? B> IntoCow<'a, T, B> for Cow<'a, T, B> where B: ToOwned<T> { + fn into_cow(self) -> Cow<'a, T, B> { + self + } +} + /// A generalization of Clone to borrowed data. pub trait ToOwned<Owned> for Sized?: BorrowFrom<Owned> { /// Create owned data from borrowed data, usually by copying. @@ -139,6 +160,22 @@ impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> { Owned(owned) => owned } } + + /// Returns true if this `Cow` wraps a borrowed value + pub fn is_borrowed(&self) -> bool { + match *self { + Borrowed(_) => true, + _ => false, + } + } + + /// Returns true if this `Cow` wraps an owned value + pub fn is_owned(&self) -> bool { + match *self { + Owned(_) => true, + _ => false, + } + } } impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> { @@ -149,3 +186,35 @@ impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> { } } } + +impl<'a, T, Sized? B> Eq for Cow<'a, T, B> where B: Eq + ToOwned<T> {} + +impl<'a, T, Sized? B> Ord for Cow<'a, T, B> where B: Ord + ToOwned<T> { + #[inline] + fn cmp(&self, other: &Cow<'a, T, B>) -> Ordering { + Ord::cmp(&**self, &**other) + } +} + +impl<'a, T, Sized? B> PartialEq for Cow<'a, T, B> where B: PartialEq + ToOwned<T> { + #[inline] + fn eq(&self, other: &Cow<'a, T, B>) -> bool { + PartialEq::eq(&**self, &**other) + } +} + +impl<'a, T, Sized? B> PartialOrd for Cow<'a, T, B> where B: PartialOrd + ToOwned<T> { + #[inline] + fn partial_cmp(&self, other: &Cow<'a, T, B>) -> Option<Ordering> { + PartialOrd::partial_cmp(&**self, &**other) + } +} + +impl<'a, T, Sized? B> fmt::Show for Cow<'a, T, B> where B: fmt::Show + ToOwned<T>, T: fmt::Show { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Borrowed(ref b) => fmt::Show::fmt(b, f), + Owned(ref o) => fmt::Show::fmt(o, f), + } + } +} |
