diff --git a/src/libcore/core.rs b/src/libcore/core.rs index a14b67b40f13..603f19362ee4 100644 --- a/src/libcore/core.rs +++ b/src/libcore/core.rs @@ -11,7 +11,7 @@ pub use GenericPath = path::GenericPath; pub use WindowsPath = path::WindowsPath; pub use PosixPath = path::PosixPath; -pub use tuple::{TupleOps, ExtendedTupleOps}; +pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps}; pub use str::{StrSlice, UniqueStr}; pub use vec::{ConstVector, CopyableVector, ImmutableVector}; pub use vec::{ImmutableEqVector, ImmutableCopyableVector}; diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index 246ce16c8131..899b16eb1320 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -6,13 +6,13 @@ use cmp::{Eq, Ord}; -pub trait TupleOps { +pub trait CopyableTuple { pure fn first() -> T; pure fn second() -> U; pure fn swap() -> (U, T); } -impl (T, U): TupleOps { +impl (T, U): CopyableTuple { /// Return the first element of self pure fn first() -> T { @@ -34,6 +34,24 @@ impl (T, U): TupleOps { } +pub trait ImmutableTuple { + pure fn first_ref(&self) -> &self/T; + pure fn second_ref(&self) -> &self/U; +} + +impl (T, U): ImmutableTuple { + pure fn first_ref(&self) -> &self/T { + match *self { + (ref t, _) => t, + } + } + pure fn second_ref(&self) -> &self/U { + match *self { + (_, ref u) => u, + } + } +} + pub trait ExtendedTupleOps { fn zip(&self) -> ~[(A, B)]; fn map(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C]; @@ -145,6 +163,13 @@ impl (A, B, C) : Ord { pure fn gt(other: &(A, B, C)) -> bool { (*other).lt(&self) } } +#[test] +fn test_tuple_ref() { + let (~"foo", ~"bar"); + assert x.first_ref() == &~"foo"; + assert x.second_ref() == &~"bar"; +} + #[test] #[allow(non_implicitly_copyable_typarams)] fn test_tuple() {