pub trait Subseq<Seq, I>: TupleLikewhere
Seq: TupleLike,{
// Required methods
fn subseq(self) -> Seq;
fn subseq_ref(&self) -> Seq::AsRefOutput<'_>;
fn subseq_mut(&mut self) -> Seq::AsMutOutput<'_>;
fn swap_subseq(&mut self, subseq: &mut Seq);
// Provided method
fn replace_subseq(&mut self, subseq: Seq) -> Seq { ... }
}Expand description
Search for subsequences of tuples.
The subsequence must have one and only one candidate in the supersequence.
Required Methods§
Sourcefn subseq(self) -> Seq
fn subseq(self) -> Seq
Take out a subsequence.
NOTE: The subsequence must have one and only one candidate in the supersequence.
Add a type annotation to the subsequence to let subseq() know.
Hint: The TupleLike trait provides the subseq() method as the wrapper
for this subseq() method.
§Example
use tuplez::{tuple, TupleLike, tuple_t};
let tup = tuple!(12, "hello", 24, 3.14, true);
let subseq: tuple_t!(&str, bool) = tup.subseq();
assert_eq!(subseq, tuple!("hello", true));
// Two candidates available: `(12, true)` or `(24, true)`.
// let subseq: tuple_t!(i32, bool) = tup.subseq();
// No candidates available.
// `(true, "hello")` cannot be a candidate, since its element order is
// different from the supersequence.
// let subseq: tuple_t!(bool, &str) = tup.subseq();
// Although `24` is also `i32`, but only `(12, "hello")` is a candidate.
let subseq: tuple_t!(i32, &str) = tup.subseq();
assert_eq!(subseq, tuple!(12, "hello"));
// It's OK to pick all `i32`s since there is only one candidate.
let subseq: tuple_t!(i32, i32) = tup.subseq();
assert_eq!(subseq, tuple!(12, 24));Sourcefn subseq_ref(&self) -> Seq::AsRefOutput<'_>
fn subseq_ref(&self) -> Seq::AsRefOutput<'_>
Similar to subseq(),
but all its elements are immutable references to the supersequence’s elements.
NOTE: The subsequence must have one and only one candidate in the supersequence.
Hint: The TupleLike trait provides the subseq_ref() method as
the wrapper for this subseq_ref() method.
Rust is almost impossible to infer generic types by the return type annotation, so you need to call it like:
use tuplez::{tuple, TupleLike, tuple_t};
let tup = tuple!(12, "hello", vec![1, 2, 3], 24, 3.14, true);
let subseq = tup.subseq_ref::<tuple_t!(&'static str, bool), _>();
assert_eq!(subseq, tuple!(&"hello", &true));Sourcefn subseq_mut(&mut self) -> Seq::AsMutOutput<'_>
fn subseq_mut(&mut self) -> Seq::AsMutOutput<'_>
Similar to subseq(),
but all its elements are mutable references to the supersequence’s elements.
NOTE: The subsequence must have one and only one candidate in the supersequence.
Hint: The TupleLike trait provides the subseq_mut() method as
the wrapper for this subseq_mut() method.
Rust is almost impossible to infer generic types by the return type annotation, so you need to call it like:
use tuplez::{get, tuple, TupleLike, tuple_t};
let mut tup = tuple!(12, "hello", vec![1, 2, 3], 24, 3.14, true);
let subseq = tup.subseq_mut::<tuple_t!(&'static str, bool), _>();
*get!(subseq; 0) = "world";
*get!(subseq; 1) = false;
assert_eq!(tup, tuple!(12, "world", vec![1, 2, 3], 24, 3.14, false));Sourcefn swap_subseq(&mut self, subseq: &mut Seq)
fn swap_subseq(&mut self, subseq: &mut Seq)
Swap elements with a subsequence.
See subseq() to see which inputs are subsequence.
NOTE: The subsequence must have one and only one candidate in the supersequence.
Hint: The TupleLike trait provides the swap_subseq() method as
the wrapper for this swap_subseq() method.
§Example
use tuplez::{tuple, TupleLike};
let mut tup = tuple!(1, Some("hello"), 2, Some(()), 3.14, 3);
let mut tup2 = tuple!(Some("world"), 9.8);
tup.swap_subseq(&mut tup2);
assert_eq!(tup, tuple!(1, Some("world"), 2, Some(()), 9.8, 3));
assert_eq!(tup2, tuple!(Some("hello"), 3.14));Provided Methods§
Sourcefn replace_subseq(&mut self, subseq: Seq) -> Seq
fn replace_subseq(&mut self, subseq: Seq) -> Seq
Replace elements with a subsequence.
See subseq() to see which inputs are subsequence.
NOTE: The subsequence must have one and only one candidate in the supersequence.
It returns a subsequence consisting of the replaced elements.
Hint: If you don’t want to consume the input tuple,
then what you are looking for might be swap_subseq().
Hint: The TupleLike trait provides the replace_subseq() method as
the wrapper for this replace_subseq() method.
§Example
use tuplez::{tuple, TupleLike};
let mut tup = tuple!(1, Some("hello"), 2, Some(()), 3.14, 3);
let replaced = tup.replace_subseq(tuple!(Some("world"), 9.8));
assert_eq!(tup, tuple!(1, Some("world"), 2, Some(()), 9.8, 3));
assert_eq!(replaced, tuple!(Some("hello"), 3.14));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.