From 8fce20c2cbd151b1e9a7ddbebfed39bbcf1d5f7f Mon Sep 17 00:00:00 2001 From: Arjan Topolovec Date: Sat, 28 Jun 2014 20:20:49 +0200 Subject: [PATCH] Implementation of common traits for MaybeOwnedVector --- src/libgraphviz/maybe_owned_vec.rs | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/libgraphviz/maybe_owned_vec.rs b/src/libgraphviz/maybe_owned_vec.rs index a34791f470e..026db9d96f6 100644 --- a/src/libgraphviz/maybe_owned_vec.rs +++ b/src/libgraphviz/maybe_owned_vec.rs @@ -8,8 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::collections::Collection; +use std::default::Default; use std::fmt; use std::iter::FromIterator; +use std::path::BytesContainer; use std::slice; // Note 1: It is not clear whether the flexibility of providing both @@ -61,6 +64,32 @@ impl<'a,T> MaybeOwnedVector<'a,T> { } } +impl<'a, T: PartialEq> PartialEq for MaybeOwnedVector<'a, T> { + fn eq(&self, other: &MaybeOwnedVector) -> bool { + self.as_slice() == other.as_slice() + } +} + +impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {} + +impl<'a, T: PartialOrd> PartialOrd for MaybeOwnedVector<'a, T> { + fn lt(&self, other: &MaybeOwnedVector) -> bool { + self.as_slice().lt(&other.as_slice()) + } +} + +impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> { + fn cmp(&self, other: &MaybeOwnedVector) -> Ordering { + self.as_slice().cmp(&other.as_slice()) + } +} + +impl<'a, T: PartialEq, V: Vector> Equiv for MaybeOwnedVector<'a, T> { + fn equiv(&self, other: &V) -> bool { + self.as_slice() == other.as_slice() + } +} + // The `Vector` trait is provided in the prelude and is implemented on // both `&'a [T]` and `Vec`, so it makes sense to try to support it // seamlessly. The other vector related traits from the prelude do @@ -108,6 +137,34 @@ impl<'a,T:Clone> CloneableVector for MaybeOwnedVector<'a,T> { } } +impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> { + fn clone(&self) -> MaybeOwnedVector<'a, T> { + match *self { + Growable(ref v) => Growable(v.to_owned()), + Borrowed(v) => Borrowed(v) + } + } +} + + +impl<'a, T> Default for MaybeOwnedVector<'a, T> { + fn default() -> MaybeOwnedVector<'a, T> { + Growable(Vec::new()) + } +} + +impl<'a, T> Collection for MaybeOwnedVector<'a, T> { + fn len(&self) -> uint { + self.as_slice().len() + } +} + +impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> { + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_slice() + } +} + impl<'a,T:Clone> MaybeOwnedVector<'a,T> { /// Convert `self` into a growable `Vec`, not making a copy if possible. pub fn into_vec(self) -> Vec {