Skip to content

Make either::{partition, flip, to_result} take their arguments by-value #4349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/libcore/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
}
}

// XXX bad copies. take arg by val
pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
pub fn partition<T, U>(eithers: ~[Either<T, U>])
-> (~[T], ~[U]) {
/*!
* Extracts from a vector of either all the left values and right values
Expand All @@ -81,27 +80,25 @@ pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])

let mut lefts: ~[T] = ~[];
let mut rights: ~[U] = ~[];
for vec::each(eithers) |elt| {
match *elt {
Left(copy l) => lefts.push(l),
Right(copy r) => rights.push(r)
do vec::consume(eithers) |_i, elt| {
match elt {
Left(l) => lefts.push(l),
Right(r) => rights.push(r)
}
}
return (move lefts, move rights);
}

// XXX bad copies
pub pure fn flip<T: Copy, U: Copy>(eith: &Either<T, U>) -> Either<U, T> {
pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
//! Flips between left and right of a given either

match *eith {
Right(copy r) => Left(r),
Left(copy l) => Right(l)
match eith {
Right(r) => Left(r),
Left(l) => Right(l)
}
}

// XXX bad copies
pub pure fn to_result<T: Copy, U: Copy>(eith: &Either<T, U>)
pub pure fn to_result<T, U>(eith: Either<T, U>)
-> Result<U, T> {
/*!
* Converts either::t to a result::t
Expand All @@ -110,9 +107,9 @@ pub pure fn to_result<T: Copy, U: Copy>(eith: &Either<T, U>)
* an ok result, and the "left" choice a fail
*/

match *eith {
Right(copy r) => result::Ok(r),
Left(copy l) => result::Err(l)
match eith {
Right(r) => result::Ok(r),
Left(l) => result::Err(l)
}
}

Expand All @@ -128,7 +125,6 @@ pub pure fn is_right<T, U>(eith: &Either<T, U>) -> bool {
match *eith { Right(_) => true, _ => false }
}

// tjc: fix the next two after a snapshot
pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
//! Retrieves the value in the left branch. Fails if the either is Right.

Expand Down