Skip to content

Commit 6a3695d

Browse files
committed
auto merge of #15241 : arjantop/rust/maybeownedvector-improvements, r=alexcrichton
2 parents 1e6b699 + 8fce20c commit 6a3695d

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/libgraphviz/maybe_owned_vec.rs

+57
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::collections::Collection;
12+
use std::default::Default;
1113
use std::fmt;
1214
use std::iter::FromIterator;
15+
use std::path::BytesContainer;
1316
use std::slice;
1417

1518
// Note 1: It is not clear whether the flexibility of providing both
@@ -61,6 +64,32 @@ impl<'a,T> MaybeOwnedVector<'a,T> {
6164
}
6265
}
6366

67+
impl<'a, T: PartialEq> PartialEq for MaybeOwnedVector<'a, T> {
68+
fn eq(&self, other: &MaybeOwnedVector<T>) -> bool {
69+
self.as_slice() == other.as_slice()
70+
}
71+
}
72+
73+
impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {}
74+
75+
impl<'a, T: PartialOrd> PartialOrd for MaybeOwnedVector<'a, T> {
76+
fn lt(&self, other: &MaybeOwnedVector<T>) -> bool {
77+
self.as_slice().lt(&other.as_slice())
78+
}
79+
}
80+
81+
impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
82+
fn cmp(&self, other: &MaybeOwnedVector<T>) -> Ordering {
83+
self.as_slice().cmp(&other.as_slice())
84+
}
85+
}
86+
87+
impl<'a, T: PartialEq, V: Vector<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
88+
fn equiv(&self, other: &V) -> bool {
89+
self.as_slice() == other.as_slice()
90+
}
91+
}
92+
6493
// The `Vector` trait is provided in the prelude and is implemented on
6594
// both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
6695
// seamlessly. The other vector related traits from the prelude do
@@ -108,6 +137,34 @@ impl<'a,T:Clone> CloneableVector<T> for MaybeOwnedVector<'a,T> {
108137
}
109138
}
110139

140+
impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
141+
fn clone(&self) -> MaybeOwnedVector<'a, T> {
142+
match *self {
143+
Growable(ref v) => Growable(v.to_owned()),
144+
Borrowed(v) => Borrowed(v)
145+
}
146+
}
147+
}
148+
149+
150+
impl<'a, T> Default for MaybeOwnedVector<'a, T> {
151+
fn default() -> MaybeOwnedVector<'a, T> {
152+
Growable(Vec::new())
153+
}
154+
}
155+
156+
impl<'a, T> Collection for MaybeOwnedVector<'a, T> {
157+
fn len(&self) -> uint {
158+
self.as_slice().len()
159+
}
160+
}
161+
162+
impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
163+
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
164+
self.as_slice()
165+
}
166+
}
167+
111168
impl<'a,T:Clone> MaybeOwnedVector<'a,T> {
112169
/// Convert `self` into a growable `Vec`, not making a copy if possible.
113170
pub fn into_vec(self) -> Vec<T> {

0 commit comments

Comments
 (0)