Trait tuplez::Search

source ·
pub trait Search<T, Result>: TupleLike {
    type TakeRemainder: TupleLike;

    // Required methods
    fn take(self) -> (T, Self::TakeRemainder);
    fn ref_of(&self) -> &T;
    fn mut_of(&mut self) -> &mut T;
}
Expand description

Search for an element of a specific type in a tuple.

There is currently a restriction: only one element in the tuple can be of the type being searched.

Required Associated Types§

source

type TakeRemainder: TupleLike

The type of the remainder of the tuple after taking out the searched element.

Required Methods§

source

fn take(self) -> (T, Self::TakeRemainder)

Take out the searched element, and get the remainder of tuple.

Add a type annotation to the searched element to let take() know which one you want.

If you want to take out the first or last element, regardless of their type, see Popable.

§Example
use tuplez::*;

let tup = tuple!(3.14, "hello", 5, [1, 2, 3]);
let (value, remainder): (i32, _) = tup.take();
assert_eq!(value, 5);
assert_eq!(remainder, tuple!(3.14, "hello", [1, 2, 3]));
source

fn ref_of(&self) -> &T

Get an immutable reference of the searched element.

Add a type annotation to the searched element to let ref_of() know which one you want.

If you want to get the element by its index, see get!;

§Example
use tuplez::*;

let tup = tuple!(3.14, "hello", 5, [1, 2, 3]);
let arr: &[i32; 3] = tup.ref_of();
assert_eq!(arr, &[1, 2, 3]);
source

fn mut_of(&mut self) -> &mut T

Get a mutable reference of the searched element.

Add a type annotation to the searched element to let mut_of() know which one you want.

If you want to get the element by its index, see get!;

§Example
use tuplez::*;

let mut tup = tuple!(3.14, "hello", 5, [1, 2, 3]);
let s: &mut &str = tup.mut_of();
*s = "world";
assert_eq!(tup, tuple!(3.14, "world", 5, [1, 2, 3]));

Object Safety§

This trait is not object safe.

Implementors§

source§

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

§

type TakeRemainder = Other

source§

impl<First, Other, T, Result> Search<T, Searching<Result>> for Tuple<First, Other>
where Other: TupleLike + Search<T, Result>,

§

type TakeRemainder = Tuple<First, <Other as Search<T, Result>>::TakeRemainder>