88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- use std:: container :: Container ;
11+ use std:: fmt ;
1212use std:: iter:: FromIterator ;
1313use std:: slice;
1414
15- // Note: Once Dynamically Sized Types (DST) lands, this should be
16- // replaced with something like `enum Owned<'a, Sized? U>{ Owned(~U),
17- // Borrowed(&'a U) }`; and then `U` could be instantiated with `[T]`
18- // or `str`, etc.
15+ // Note 1: It is not clear whether the flexibility of providing both
16+ // the `Growable` and `FixedLen` variants is sufficiently useful.
17+ // Consider restricting to just a two variant enum.
1918
20- /// MaybeOwnedVector<'a,T> abstracts over `Vec<T>` and `&'a [T]`.
19+ // Note 2: Once Dynamically Sized Types (DST) lands, it might be
20+ // reasonable to replace this with something like `enum MaybeOwned<'a,
21+ // Sized? U>{ Owned(~U), Borrowed(&'a U) }`; and then `U` could be
22+ // instantiated with `[T]` or `str`, etc. Of course, that would imply
23+ // removing the `Growable` variant, which relates to note 1 above.
24+ // Alternatively, we might add `MaybeOwned` for the general case but
25+ // keep some form of `MaybeOwnedVector` to avoid unnecessary copying
26+ // of the contents of `Vec<T>`, since we anticipate that to be a
27+ // frequent way to dynamically construct a vector.
28+
29+ /// MaybeOwnedVector<'a,T> abstracts over `Vec<T>`, `~[T]`, `&'a [T]`.
2130///
2231/// Some clients will have a pre-allocated vector ready to hand off in
2332/// a slice; others will want to create the set on the fly and hand
24- /// off ownership.
25- #[ deriving( Eq ) ]
33+ /// off ownership, via either `Growable` or `FixedLen` depending on
34+ /// which kind of vector they have constucted. (The `FixedLen`
35+ /// variant is provided for interoperability with `std::slice` methods
36+ /// that return `~[T]`.)
2637pub enum MaybeOwnedVector < ' a , T > {
2738 Growable ( Vec < T > ) ,
39+ FixedLen ( ~[ T ] ) ,
2840 Borrowed ( & ' a [ T ] ) ,
2941}
3042
43+ /// Trait for moving into a `MaybeOwnedVector`
44+ pub trait IntoMaybeOwnedVector < ' a , T > {
45+ /// Moves self into a `MaybeOwnedVector`
46+ fn into_maybe_owned ( self ) -> MaybeOwnedVector < ' a , T > ;
47+ }
48+
49+ impl < ' a , T > IntoMaybeOwnedVector < ' a , T > for Vec < T > {
50+ #[ inline]
51+ fn into_maybe_owned ( self ) -> MaybeOwnedVector < ' a , T > { Growable ( self ) }
52+ }
53+
54+ impl < ' a , T > IntoMaybeOwnedVector < ' a , T > for ~[ T ] {
55+ #[ inline]
56+ fn into_maybe_owned ( self ) -> MaybeOwnedVector < ' a , T > { FixedLen ( self ) }
57+ }
58+
59+ impl < ' a , T > IntoMaybeOwnedVector < ' a , T > for & ' a [ T ] {
60+ #[ inline]
61+ fn into_maybe_owned ( self ) -> MaybeOwnedVector < ' a , T > { Borrowed ( self ) }
62+ }
63+
3164impl < ' a , T > MaybeOwnedVector < ' a , T > {
3265 pub fn iter ( & ' a self ) -> slice:: Items < ' a , T > {
3366 match self {
3467 & Growable ( ref v) => v. iter ( ) ,
68+ & FixedLen ( ref v) => v. iter ( ) ,
3569 & Borrowed ( ref v) => v. iter ( ) ,
3670 }
3771 }
3872}
3973
40- impl < ' a , T > Container for MaybeOwnedVector < ' a , T > {
41- fn len ( & self ) -> uint {
42- match self {
43- & Growable ( ref v) => v. len ( ) ,
44- & Borrowed ( ref v) => v. len ( ) ,
45- }
46- }
47- }
48-
4974// The `Vector` trait is provided in the prelude and is implemented on
5075// both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
5176// seamlessly. The other vector related traits from the prelude do
@@ -59,13 +84,49 @@ impl<'b,T> slice::Vector<T> for MaybeOwnedVector<'b,T> {
5984 fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
6085 match self {
6186 & Growable ( ref v) => v. as_slice ( ) ,
87+ & FixedLen ( ref v) => v. as_slice ( ) ,
6288 & Borrowed ( ref v) => v. as_slice ( ) ,
6389 }
6490 }
6591}
6692
6793impl < ' a , T > FromIterator < T > for MaybeOwnedVector < ' a , T > {
6894 fn from_iter < I : Iterator < T > > ( iterator : I ) -> MaybeOwnedVector < T > {
95+ // If we are building from scratch, might as well build the
96+ // most flexible variant.
6997 Growable ( FromIterator :: from_iter ( iterator) )
7098 }
7199}
100+
101+ impl < ' a , T : fmt:: Show > fmt:: Show for MaybeOwnedVector < ' a , T > {
102+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
103+ self . as_slice ( ) . fmt ( f)
104+ }
105+ }
106+
107+ impl < ' a , T : Clone > CloneableVector < T > for MaybeOwnedVector < ' a , T > {
108+ /// Returns a copy of `self`.
109+ fn to_owned ( & self ) -> ~[ T ] {
110+ self . as_slice ( ) . to_owned ( )
111+ }
112+
113+ /// Convert `self` into an owned slice, not making a copy if possible.
114+ fn into_owned ( self ) -> ~[ T ] {
115+ match self {
116+ Growable ( v) => v. as_slice ( ) . to_owned ( ) ,
117+ FixedLen ( v) => v,
118+ Borrowed ( v) => v. to_owned ( ) ,
119+ }
120+ }
121+ }
122+
123+ impl < ' a , T : Clone > MaybeOwnedVector < ' a , T > {
124+ /// Convert `self` into a growable `Vec`, not making a copy if possible.
125+ pub fn into_vec ( self ) -> Vec < T > {
126+ match self {
127+ Growable ( v) => v,
128+ FixedLen ( v) => Vec :: from_slice ( v. as_slice ( ) ) ,
129+ Borrowed ( v) => Vec :: from_slice ( v) ,
130+ }
131+ }
132+ }
0 commit comments