Trait tuplez::TupleLike

source ·
pub trait TupleLike {
    type AsRefOutput<'a>: TupleLike
       where Self: 'a;
    type AsMutOutput<'a>: TupleLike
       where Self: 'a;
    type PushFrontOutput<T>: TupleLike;
    type PushBackOutput<T>: TupleLike;
    type RevOutput: TupleLike;
    type JoinOutput<T>: TupleLike
       where T: TupleLike;
    type ToSomeOutput: TupleLike;
    type ToOkOutput<E>: TupleLike;

    const LEN: usize;

    // Required methods
    fn as_ref(&self) -> Self::AsRefOutput<'_>;
    fn as_mut(&mut self) -> Self::AsMutOutput<'_>;
    fn push<T>(self, value: T) -> Self::PushBackOutput<T>;
    fn push_front<T>(self, value: T) -> Self::PushFrontOutput<T>;
    fn push_back<T>(self, value: T) -> Self::PushBackOutput<T>;
    fn rev(self) -> Self::RevOutput;
    fn join<T>(self, value: T) -> Self::JoinOutput<T>
       where T: TupleLike;
    fn to_some(self) -> Self::ToSomeOutput;
    fn to_ok<E>(self) -> Self::ToOkOutput<E>;

    // Provided method
    fn len(&self) -> usize { ... }
}
Expand description

The TupleLike trait defines the basic methods of tuples.

Notice that an Unit contains no elements and cannot be popped, so the pop methods is not required methods of a tuple. See the Popable trait about pop methods.

In fact, all tuples implement the rotation methods. However, since right rotation requires additional detection of whether the tuple can be popped, and cannot be introduced into TupleLike in a elegant way, so an extra trait Rotatable is used for them.

Another thing is, due to the limitation that Rust cannot represent primitive tuple types containing any number of elements, we cannot make TupleLike trait contain a method that converts tuple to the primitive tuple type. Therefore, this method is provided by the trait ToPrimitive and is only implemented for tuples with no more than 32 elements.

Required Associated Types§

source

type AsRefOutput<'a>: TupleLike where Self: 'a

The type of tuple containing immutable references to all elements of the tuple.

source

type AsMutOutput<'a>: TupleLike where Self: 'a

The type of tuple containing mutable references to all elements of the tuple.

source

type PushFrontOutput<T>: TupleLike

The type of tuple generated by pushing an element to the front of the tuple.

source

type PushBackOutput<T>: TupleLike

The type of tuple generated by pushing an element to the back of the tuple.

source

type RevOutput: TupleLike

The type of tuple generated by reversing the tuple.

source

type JoinOutput<T>: TupleLike where T: TupleLike

The type of tuple generated by joining two tuples.

source

type ToSomeOutput: TupleLike

The type of tuple after wrapping all elements into Option.

source

type ToOkOutput<E>: TupleLike

The type of tuple after wrapping all elements into Result.

Required Associated Constants§

source

const LEN: usize

The number of elements in the tuple.

Required Methods§

source

fn as_ref(&self) -> Self::AsRefOutput<'_>

Generate a tuple containing immutable references to all elements of the tuple.

§Example
use tuplez::*;

let tup = tuple!([1, 2], "hello".to_string());
let tup_ref: tuple_t!(&[i32; 2], &String) = tup.as_ref();
assert_eq!(tup_ref, tuple!(&[1, 2], &String::from("hello")));
source

fn as_mut(&mut self) -> Self::AsMutOutput<'_>

Generate a tuple containing mutable reference to all elements of the tuple.

§Example
use tuplez::*;

let mut tup = tuple!(1, "hello");
let tup_mut = tup.as_mut();
*get!(tup_mut; 0) += 1;
*get!(tup_mut; 1) = "world";
assert_eq!(tup, tuple!(2, "world"));
source

fn push<T>(self, value: T) -> Self::PushBackOutput<T>

Push an element to the back of the tuple.

§Examples
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
let tup2 = tup.push(44);
assert_eq!(tup2, tuple!(1, "hello", 3.14, 44));
source

fn push_front<T>(self, value: T) -> Self::PushFrontOutput<T>

Push an element to the front of the tuple.

§Examples
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
let tup2 = tup.push_front(44);
assert_eq!(tup2, tuple!(44, 1, "hello", 3.14));
source

fn push_back<T>(self, value: T) -> Self::PushBackOutput<T>

Push an element to the back of the tuple. Same as push().

source

fn rev(self) -> Self::RevOutput

Reverse elements of the tuple.

§Examples
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
let tup2 = tup.rev();
assert_eq!(tup2, tuple!(3.14, "hello", 1));
source

fn join<T>(self, value: T) -> Self::JoinOutput<T>
where T: TupleLike,

Join two tuples.

§Examples
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
let tup2 = tuple!(44, "world");
let tup3 = tup.join(tup2);
assert_eq!(tup3, tuple!(1, "hello", 3.14, 44, "world"));
source

fn to_some(self) -> Self::ToSomeOutput

Convert a tuple!(a, b, c ...) to tuple!(Some(a), Some(b), Some(c) ...).

§Example
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
assert_eq!(tup.to_some(), tuple!(Some(1), Some("hello"), Some(3.14)));
source

fn to_ok<E>(self) -> Self::ToOkOutput<E>

Convert a tuple!(a, b, c ...) to tuple!(Ok(a), Ok(b), Ok(c) ...).

Note: You need to provide the error type.

§Example
use tuplez::*;

let tup = tuple!(1, "hello", 3.14);
assert_eq!(tup.to_ok::<()>(), tuple!(Ok(1), Ok("hello"), Ok(3.14)));

Provided Methods§

source

fn len(&self) -> usize

Returns the number of elements in the tuple. MUST always return LEN.

§Examples
use tuplez::*;
assert_eq!(tuple!(1, "hello", 3.14).len(), 3);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl TupleLike for Unit

§

type AsRefOutput<'a> = Unit

§

type AsMutOutput<'a> = Unit

§

type PushFrontOutput<T> = Tuple<T, Unit>

§

type PushBackOutput<T> = Tuple<T, Unit>

§

type RevOutput = Unit

§

type JoinOutput<T> = T where T: TupleLike

§

type ToSomeOutput = Unit

§

type ToOkOutput<E> = Unit

source§

const LEN: usize = 0usize

source§

impl<First, Other> TupleLike for Tuple<First, Other>
where Other: TupleLike,

§

type AsRefOutput<'a> = Tuple<&'a First, <Other as TupleLike>::AsRefOutput<'a>> where Self: 'a

§

type AsMutOutput<'a> = Tuple<&'a mut First, <Other as TupleLike>::AsMutOutput<'a>> where Self: 'a

§

type PushFrontOutput<T> = Tuple<T, Tuple<First, Other>>

§

type PushBackOutput<T> = Tuple<First, <Other as TupleLike>::PushBackOutput<T>>

§

type RevOutput = <<Other as TupleLike>::RevOutput as TupleLike>::PushBackOutput<First>

§

type JoinOutput<T> = Tuple<First, <Other as TupleLike>::JoinOutput<T>> where T: TupleLike

§

type ToSomeOutput = Tuple<Option<First>, <Other as TupleLike>::ToSomeOutput>

§

type ToOkOutput<E> = Tuple<Result<First, E>, <Other as TupleLike>::ToOkOutput<E>>

source§

const LEN: usize = _