@@ -26,26 +26,22 @@ pub enum Either<T, U> {
26
26
Right ( U )
27
27
}
28
28
29
+ /// Applies a function based on the given either value
30
+ ///
31
+ /// If `value` is left(T) then `f_left` is applied to its contents, if
32
+ /// `value` is right(U) then `f_right` is applied to its contents, and the
33
+ /// result is returned.
29
34
#[ inline( always) ]
30
35
pub fn either < T , U , V > ( f_left : & fn ( & T ) -> V ,
31
36
f_right : & fn ( & U ) -> V , value : & Either < T , U > ) -> V {
32
- /*!
33
- * Applies a function based on the given either value
34
- *
35
- * If `value` is left(T) then `f_left` is applied to its contents, if
36
- * `value` is right(U) then `f_right` is applied to its contents, and the
37
- * result is returned.
38
- */
39
-
40
37
match * value {
41
38
Left ( ref l) => f_left ( l) ,
42
39
Right ( ref r) => f_right ( r)
43
40
}
44
41
}
45
42
43
+ /// Extracts from a vector of either all the left values
46
44
pub fn lefts < T : Copy , U > ( eithers : & [ Either < T , U > ] ) -> ~[ T ] {
47
- //! Extracts from a vector of either all the left values
48
-
49
45
do vec:: build_sized ( eithers. len ( ) ) |push| {
50
46
for eithers. each |elt| {
51
47
match * elt {
@@ -56,9 +52,8 @@ pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
56
52
}
57
53
}
58
54
55
+ /// Extracts from a vector of either all the right values
59
56
pub fn rights < T , U : Copy > ( eithers : & [ Either < T , U > ] ) -> ~[ U ] {
60
- //! Extracts from a vector of either all the right values
61
-
62
57
do vec:: build_sized ( eithers. len ( ) ) |push| {
63
58
for eithers. each |elt| {
64
59
match * elt {
@@ -69,15 +64,11 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
69
64
}
70
65
}
71
66
72
- pub fn partition < T , U > ( eithers : ~[ Either < T , U > ] )
73
- -> ( ~[ T ] , ~[ U ] ) {
74
- /*!
75
- * Extracts from a vector of either all the left values and right values
76
- *
77
- * Returns a structure containing a vector of left values and a vector of
78
- * right values.
79
- */
80
-
67
+ /// Extracts from a vector of either all the left values and right values
68
+ ///
69
+ /// Returns a structure containing a vector of left values and a vector of
70
+ /// right values.
71
+ pub fn partition < T , U > ( eithers : ~[ Either < T , U > ] ) -> ( ~[ T ] , ~[ U ] ) {
81
72
let mut lefts: ~[ T ] = ~[ ] ;
82
73
let mut rights: ~[ U ] = ~[ ] ;
83
74
do vec:: consume ( eithers) |_i, elt| {
@@ -89,60 +80,51 @@ pub fn partition<T, U>(eithers: ~[Either<T, U>])
89
80
return ( lefts, rights) ;
90
81
}
91
82
83
+ /// Flips between left and right of a given either
92
84
#[ inline( always) ]
93
85
pub fn flip < T , U > ( eith : Either < T , U > ) -> Either < U , T > {
94
- //! Flips between left and right of a given either
95
-
96
86
match eith {
97
87
Right ( r) => Left ( r) ,
98
88
Left ( l) => Right ( l)
99
89
}
100
90
}
101
91
92
+ /// Converts either::t to a result::t
93
+ ///
94
+ /// Converts an `either` type to a `result` type, making the "right" choice
95
+ /// an ok result, and the "left" choice a fail
102
96
#[ inline( always) ]
103
- pub fn to_result < T , U > ( eith : Either < T , U > )
104
- -> Result < U , T > {
105
- /*!
106
- * Converts either::t to a result::t
107
- *
108
- * Converts an `either` type to a `result` type, making the "right" choice
109
- * an ok result, and the "left" choice a fail
110
- */
111
-
97
+ pub fn to_result < T , U > ( eith : Either < T , U > ) -> Result < U , T > {
112
98
match eith {
113
99
Right ( r) => result:: Ok ( r) ,
114
100
Left ( l) => result:: Err ( l)
115
101
}
116
102
}
117
103
104
+ /// Checks whether the given value is a left
118
105
#[ inline( always) ]
119
106
pub fn is_left < T , U > ( eith : & Either < T , U > ) -> bool {
120
- //! Checks whether the given value is a left
121
-
122
107
match * eith { Left ( _) => true , _ => false }
123
108
}
124
109
110
+ /// Checks whether the given value is a right
125
111
#[ inline( always) ]
126
112
pub fn is_right < T , U > ( eith : & Either < T , U > ) -> bool {
127
- //! Checks whether the given value is a right
128
-
129
113
match * eith { Right ( _) => true , _ => false }
130
114
}
131
115
116
+ /// Retrieves the value in the left branch. Fails if the either is Right.
132
117
#[ inline( always) ]
133
118
pub fn unwrap_left < T , U > ( eith : Either < T , U > ) -> T {
134
- //! Retrieves the value in the left branch. Fails if the either is Right.
135
-
136
119
match eith {
137
120
Left ( x) => x,
138
121
Right ( _) => fail ! ( "either::unwrap_left Right" )
139
122
}
140
123
}
141
124
125
+ /// Retrieves the value in the right branch. Fails if the either is Left.
142
126
#[ inline( always) ]
143
127
pub fn unwrap_right < T , U > ( eith : Either < T , U > ) -> U {
144
- //! Retrieves the value in the right branch. Fails if the either is Left.
145
-
146
128
match eith {
147
129
Right ( x) => x,
148
130
Left ( _) => fail ! ( "either::unwrap_right Left" )
0 commit comments