1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

pub trait SwapTupleExt {
    type Result;
    /// Swap the two tuple elements
    fn swap(self) -> Self::Result;
}

impl <T, U> SwapTupleExt for (T, U) {
    type Result = (U, T);
    fn swap(self) -> Self::Result {
        (self.1, self.0)
    }
}

impl <'a, T, U> SwapTupleExt for &'a (T, U) {
    type Result = (&'a U, &'a T);
    fn swap(self) -> Self::Result {
        (&self.1, &self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::SwapTupleExt;

    #[test]
    fn swap_tuple_value() {
        let tuple: (&str, i32) = ("foo", 42);
        let swapped: (i32, &str) = tuple.swap();
        assert_eq!(swapped, (42, "foo"));
    }

    #[test]
    fn swap_tuple_ref() {
        let tuple: &(&str, i32) = &("foo", 42);
        let swapped: (&i32, &&str) = tuple.swap();
        assert_eq!(swapped, (&42, &"foo"));
    }
}