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.
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.
11 #![deprecated = "use std::vec::CowVec"]
13 pub use self::MaybeOwnedVector::*;
15 use std::cmp::Ordering;
16 use std::default::Default;
18 use std::iter::FromIterator;
19 use std::path::BytesContainer;
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.
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.
36 /// MaybeOwnedVector<'a,T> abstracts over `Vec<T>`, `&'a [T]`.
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> {
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>;
53 impl<'a,T:'a> IntoMaybeOwnedVector<'a,T> for Vec<T> {
56 fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Growable(self) }
60 impl<'a,T> IntoMaybeOwnedVector<'a,T> for &'a [T] {
63 fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Borrowed(self) }
66 impl<'a,T> MaybeOwnedVector<'a,T> {
67 pub fn iter(&'a self) -> slice::Iter<'a,T> {
69 &Growable(ref v) => v.as_slice().iter(),
70 &Borrowed(ref v) => v.iter(),
74 pub fn len(&self) -> uint { self.as_slice().len() }
77 pub fn is_empty(&self) -> bool { self.len() == 0 }
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()
86 impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {}
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())
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())
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.)
106 // In any case, with `Vector` in place, the client can just use
107 // `as_slice` if they prefer that over `match`.
109 impl<'b,T> AsSlice<T> for MaybeOwnedVector<'b,T> {
110 fn as_slice<'a>(&'a self) -> &'a [T] {
112 &Growable(ref v) => v.as_slice(),
113 &Borrowed(ref v) => v.as_slice(),
118 impl<'a,T> FromIterator<T> for MaybeOwnedVector<'a,T> {
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())
127 impl<'a,T:fmt::Show> fmt::Show for MaybeOwnedVector<'a,T> {
128 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129 self.as_slice().fmt(f)
133 impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
135 fn clone(&self) -> MaybeOwnedVector<'a, T> {
137 Growable(ref v) => Growable(v.clone()),
138 Borrowed(v) => Borrowed(v)
143 impl<'a, T> Default for MaybeOwnedVector<'a, T> {
145 fn default() -> MaybeOwnedVector<'a, T> {
150 impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
151 fn container_as_bytes(&self) -> &[u8] {
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> {
161 Borrowed(v) => v.to_vec(),