@@ -26,26 +26,22 @@ pub enum Either<T, U> {
2626 Right ( U )
2727}
2828
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.
2934#[ inline( always) ]
3035pub fn either < T , U , V > ( f_left : & fn ( & T ) -> V ,
3136 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-
4037 match * value {
41- Left ( ref l) => f_left ( l) ,
42- Right ( ref r) => f_right ( r)
38+ Left ( ref l) => f_left ( l) ,
39+ Right ( ref r) => f_right ( r)
4340 }
4441}
4542
43+ /// Extracts from a vector of either all the left values
4644pub fn lefts < T : Copy , U > ( eithers : & [ Either < T , U > ] ) -> ~[ T ] {
47- //! Extracts from a vector of either all the left values
48-
4945 do vec:: build_sized ( eithers. len ( ) ) |push| {
5046 for eithers. each |elt| {
5147 match * elt {
@@ -56,9 +52,8 @@ pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
5652 }
5753}
5854
55+ /// Extracts from a vector of either all the right values
5956pub fn rights < T , U : Copy > ( eithers : & [ Either < T , U > ] ) -> ~[ U ] {
60- //! Extracts from a vector of either all the right values
61-
6257 do vec:: build_sized ( eithers. len ( ) ) |push| {
6358 for eithers. each |elt| {
6459 match * elt {
@@ -69,80 +64,73 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
6964 }
7065}
7166
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 ] ) {
8172 let mut lefts: ~[ T ] = ~[ ] ;
8273 let mut rights: ~[ U ] = ~[ ] ;
8374 do vec:: consume ( eithers) |_i, elt| {
8475 match elt {
85- Left ( l) => lefts. push ( l) ,
86- Right ( r) => rights. push ( r)
76+ Left ( l) => lefts. push ( l) ,
77+ Right ( r) => rights. push ( r)
8778 }
8879 }
8980 return ( lefts, rights) ;
9081}
9182
83+ /// Flips between left and right of a given either
9284#[ inline( always) ]
9385pub fn flip < T , U > ( eith : Either < T , U > ) -> Either < U , T > {
94- //! Flips between left and right of a given either
95-
9686 match eith {
97- Right ( r) => Left ( r) ,
98- Left ( l) => Right ( l)
87+ Right ( r) => Left ( r) ,
88+ Left ( l) => Right ( l)
9989 }
10090}
10191
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
10296#[ 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 > {
11298 match eith {
113- Right ( r) => result:: Ok ( r) ,
114- Left ( l) => result:: Err ( l)
99+ Right ( r) => result:: Ok ( r) ,
100+ Left ( l) => result:: Err ( l)
115101 }
116102}
117103
104+ /// Checks whether the given value is a left
118105#[ inline( always) ]
119106pub fn is_left < T , U > ( eith : & Either < T , U > ) -> bool {
120- //! Checks whether the given value is a left
121-
122- match * eith { Left ( _) => true , _ => false }
107+ match * eith {
108+ Left ( _) => true ,
109+ _ => false
110+ }
123111}
124112
113+ /// Checks whether the given value is a right
125114#[ inline( always) ]
126115pub fn is_right < T , U > ( eith : & Either < T , U > ) -> bool {
127- //! Checks whether the given value is a right
128-
129- match * eith { Right ( _) => true , _ => false }
116+ match * eith {
117+ Right ( _) => true ,
118+ _ => false
119+ }
130120}
131121
122+ /// Retrieves the value in the left branch. Fails if the either is Right.
132123#[ inline( always) ]
133124pub 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-
136125 match eith {
137126 Left ( x) => x,
138127 Right ( _) => fail ! ( "either::unwrap_left Right" )
139128 }
140129}
141130
131+ /// Retrieves the value in the right branch. Fails if the either is Left.
142132#[ inline( always) ]
143133pub 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-
146134 match eith {
147135 Right ( x) => x,
148136 Left ( _) => fail ! ( "either::unwrap_right Left" )
0 commit comments