8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use std:: container :: Container ;
11
+ use std:: fmt ;
12
12
use std:: iter:: FromIterator ;
13
13
use std:: slice;
14
14
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.
19
18
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]`.
21
30
///
22
31
/// Some clients will have a pre-allocated vector ready to hand off in
23
32
/// 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]`.)
26
37
pub enum MaybeOwnedVector < ' a , T > {
27
38
Growable ( Vec < T > ) ,
39
+ FixedLen ( ~[ T ] ) ,
28
40
Borrowed ( & ' a [ T ] ) ,
29
41
}
30
42
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
+
31
64
impl < ' a , T > MaybeOwnedVector < ' a , T > {
32
65
pub fn iter ( & ' a self ) -> slice:: Items < ' a , T > {
33
66
match self {
34
67
& Growable ( ref v) => v. iter ( ) ,
68
+ & FixedLen ( ref v) => v. iter ( ) ,
35
69
& Borrowed ( ref v) => v. iter ( ) ,
36
70
}
37
71
}
38
72
}
39
73
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
-
49
74
// The `Vector` trait is provided in the prelude and is implemented on
50
75
// both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
51
76
// seamlessly. The other vector related traits from the prelude do
@@ -59,13 +84,49 @@ impl<'b,T> slice::Vector<T> for MaybeOwnedVector<'b,T> {
59
84
fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
60
85
match self {
61
86
& Growable ( ref v) => v. as_slice ( ) ,
87
+ & FixedLen ( ref v) => v. as_slice ( ) ,
62
88
& Borrowed ( ref v) => v. as_slice ( ) ,
63
89
}
64
90
}
65
91
}
66
92
67
93
impl < ' a , T > FromIterator < T > for MaybeOwnedVector < ' a , T > {
68
94
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.
69
97
Growable ( FromIterator :: from_iter ( iterator) )
70
98
}
71
99
}
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