]> git.lizzy.rs Git - rust.git/blob - src/libgraphviz/maybe_owned_vec.rs
std: Rename Show/String to Debug/Display
[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::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 // U: ?Sized>{ 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 // The `Vector` trait is provided in the prelude and is implemented on
101 // both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
102 // seamlessly.  The other vector related traits from the prelude do
103 // not appear to be implemented on both `&'a [T]` and `Vec<T>`.  (It
104 // is possible that this is an oversight in some cases.)
105 //
106 // In any case, with `Vector` in place, the client can just use
107 // `as_slice` if they prefer that over `match`.
108
109 impl<'b,T> AsSlice<T> for MaybeOwnedVector<'b,T> {
110     fn as_slice<'a>(&'a self) -> &'a [T] {
111         match self {
112             &Growable(ref v) => v.as_slice(),
113             &Borrowed(ref v) => v.as_slice(),
114         }
115     }
116 }
117
118 impl<'a,T> FromIterator<T> for MaybeOwnedVector<'a,T> {
119     #[allow(deprecated)]
120     fn from_iter<I:Iterator<Item=T>>(iterator: I) -> MaybeOwnedVector<'a,T> {
121         // If we are building from scratch, might as well build the
122         // most flexible variant.
123         Growable(iterator.collect())
124     }
125 }
126
127 impl<'a,T:fmt::Debug> fmt::Debug for MaybeOwnedVector<'a,T> {
128     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129         self.as_slice().fmt(f)
130     }
131 }
132
133 impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
134     #[allow(deprecated)]
135     fn clone(&self) -> MaybeOwnedVector<'a, T> {
136         match *self {
137             Growable(ref v) => Growable(v.clone()),
138             Borrowed(v) => Borrowed(v)
139         }
140     }
141 }
142
143 impl<'a, T> Default for MaybeOwnedVector<'a, T> {
144     #[allow(deprecated)]
145     fn default() -> MaybeOwnedVector<'a, T> {
146         Growable(Vec::new())
147     }
148 }
149
150 impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
151     fn container_as_bytes(&self) -> &[u8] {
152         self.as_slice()
153     }
154 }
155
156 impl<'a,T:Clone> MaybeOwnedVector<'a,T> {
157     /// Convert `self` into a growable `Vec`, not making a copy if possible.
158     pub fn into_vec(self) -> Vec<T> {
159         match self {
160             Growable(v) => v,
161             Borrowed(v) => v.to_vec(),
162         }
163     }
164 }