]> git.lizzy.rs Git - rust.git/blob - src/libgraphviz/maybe_owned_vec.rs
doc: remove incomplete sentence
[rust.git] / src / libgraphviz / maybe_owned_vec.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![deprecated = "use std::vec::CowVec"]
12
13 pub use self::MaybeOwnedVector::*;
14
15 use std::cmp::{Equiv, Ordering};
16 use std::default::Default;
17 use std::fmt;
18 use std::iter::FromIterator;
19 use std::path::BytesContainer;
20 use std::slice;
21
22 // Note 1: It is not clear whether the flexibility of providing both
23 // the `Growable` and `FixedLen` variants is sufficiently useful.
24 // Consider restricting to just a two variant enum.
25
26 // Note 2: Once Dynamically Sized Types (DST) lands, it might be
27 // reasonable to replace this with something like `enum MaybeOwned<'a,
28 // Sized? U>{ Owned(Box<U>), Borrowed(&'a U) }`; and then `U` could be
29 // instantiated with `[T]` or `str`, etc.  Of course, that would imply
30 // removing the `Growable` variant, which relates to note 1 above.
31 // Alternatively, we might add `MaybeOwned` for the general case but
32 // keep some form of `MaybeOwnedVector` to avoid unnecessary copying
33 // of the contents of `Vec<T>`, since we anticipate that to be a
34 // frequent way to dynamically construct a vector.
35
36 /// MaybeOwnedVector<'a,T> abstracts over `Vec<T>`, `&'a [T]`.
37 ///
38 /// Some clients will have a pre-allocated vector ready to hand off in
39 /// a slice; others will want to create the set on the fly and hand
40 /// off ownership, via `Growable`.
41 pub enum MaybeOwnedVector<'a,T:'a> {
42     Growable(Vec<T>),
43     Borrowed(&'a [T]),
44 }
45
46 /// Trait for moving into a `MaybeOwnedVector`
47 pub trait IntoMaybeOwnedVector<'a,T> {
48     /// Moves self into a `MaybeOwnedVector`
49     fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T>;
50 }
51
52 #[allow(deprecated)]
53 impl<'a,T:'a> IntoMaybeOwnedVector<'a,T> for Vec<T> {
54     #[allow(deprecated)]
55     #[inline]
56     fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Growable(self) }
57 }
58
59 #[allow(deprecated)]
60 impl<'a,T> IntoMaybeOwnedVector<'a,T> for &'a [T] {
61     #[allow(deprecated)]
62     #[inline]
63     fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Borrowed(self) }
64 }
65
66 impl<'a,T> MaybeOwnedVector<'a,T> {
67     pub fn iter(&'a self) -> slice::Iter<'a,T> {
68         match self {
69             &Growable(ref v) => v.as_slice().iter(),
70             &Borrowed(ref v) => v.iter(),
71         }
72     }
73
74     pub fn len(&self) -> uint { self.as_slice().len() }
75
76     #[allow(deprecated)]
77     pub fn is_empty(&self) -> bool { self.len() == 0 }
78 }
79
80 impl<'a, T: PartialEq> PartialEq for MaybeOwnedVector<'a, T> {
81     fn eq(&self, other: &MaybeOwnedVector<T>) -> bool {
82         self.as_slice() == other.as_slice()
83     }
84 }
85
86 impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {}
87
88 impl<'a, T: PartialOrd> PartialOrd for MaybeOwnedVector<'a, T> {
89     fn partial_cmp(&self, other: &MaybeOwnedVector<T>) -> Option<Ordering> {
90         self.as_slice().partial_cmp(other.as_slice())
91     }
92 }
93
94 impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
95     fn cmp(&self, other: &MaybeOwnedVector<T>) -> Ordering {
96         self.as_slice().cmp(other.as_slice())
97     }
98 }
99
100 #[allow(deprecated)]
101 impl<'a, T: PartialEq> Equiv<[T]> for MaybeOwnedVector<'a, T> {
102     fn equiv(&self, other: &[T]) -> bool {
103         self.as_slice() == other
104     }
105 }
106
107 // The `Vector` trait is provided in the prelude and is implemented on
108 // both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
109 // seamlessly.  The other vector related traits from the prelude do
110 // not appear to be implemented on both `&'a [T]` and `Vec<T>`.  (It
111 // is possible that this is an oversight in some cases.)
112 //
113 // In any case, with `Vector` in place, the client can just use
114 // `as_slice` if they prefer that over `match`.
115
116 impl<'b,T> AsSlice<T> for MaybeOwnedVector<'b,T> {
117     fn as_slice<'a>(&'a self) -> &'a [T] {
118         match self {
119             &Growable(ref v) => v.as_slice(),
120             &Borrowed(ref v) => v.as_slice(),
121         }
122     }
123 }
124
125 impl<'a,T> FromIterator<T> for MaybeOwnedVector<'a,T> {
126     #[allow(deprecated)]
127     fn from_iter<I:Iterator<Item=T>>(iterator: I) -> MaybeOwnedVector<'a,T> {
128         // If we are building from scratch, might as well build the
129         // most flexible variant.
130         Growable(iterator.collect())
131     }
132 }
133
134 impl<'a,T:fmt::Show> fmt::Show for MaybeOwnedVector<'a,T> {
135     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136         self.as_slice().fmt(f)
137     }
138 }
139
140 impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
141     #[allow(deprecated)]
142     fn clone(&self) -> MaybeOwnedVector<'a, T> {
143         match *self {
144             Growable(ref v) => Growable(v.clone()),
145             Borrowed(v) => Borrowed(v)
146         }
147     }
148 }
149
150 impl<'a, T> Default for MaybeOwnedVector<'a, T> {
151     #[allow(deprecated)]
152     fn default() -> MaybeOwnedVector<'a, T> {
153         Growable(Vec::new())
154     }
155 }
156
157 impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
158     fn container_as_bytes(&self) -> &[u8] {
159         self.as_slice()
160     }
161 }
162
163 impl<'a,T:Clone> MaybeOwnedVector<'a,T> {
164     /// Convert `self` into a growable `Vec`, not making a copy if possible.
165     pub fn into_vec(self) -> Vec<T> {
166         match self {
167             Growable(v) => v,
168             Borrowed(v) => v.to_vec(),
169         }
170     }
171 }