diff options
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/core.rs | 2 | ||||
| -rw-r--r-- | src/libcore/tuple.rs | 29 |
2 files changed, 28 insertions, 3 deletions
diff --git a/src/libcore/core.rs b/src/libcore/core.rs index a14b67b40f1..603f19362ee 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 246ce16c813..899b16eb132 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -6,13 +6,13 @@ use cmp::{Eq, Ord}; -pub trait TupleOps<T,U> { +pub trait CopyableTuple<T, U> { pure fn first() -> T; pure fn second() -> U; pure fn swap() -> (U, T); } -impl<T: Copy, U: Copy> (T, U): TupleOps<T,U> { +impl<T: Copy, U: Copy> (T, U): CopyableTuple<T, U> { /// Return the first element of self pure fn first() -> T { @@ -34,6 +34,24 @@ impl<T: Copy, U: Copy> (T, U): TupleOps<T,U> { } +pub trait ImmutableTuple<T, U> { + pure fn first_ref(&self) -> &self/T; + pure fn second_ref(&self) -> &self/U; +} + +impl<T, U> (T, U): ImmutableTuple<T, U> { + 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<A,B> { fn zip(&self) -> ~[(A, B)]; fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C]; @@ -146,6 +164,13 @@ impl<A: Ord, B: Ord, C: Ord> (A, B, C) : Ord { } #[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() { assert (948, 4039.48).first() == 948; |
