pub trait ToArray<T>: TupleLike {
type Array: IntoIterator<Item = T>;
type Iter<'a>: Iterator<Item = &'a T>
where Self::AsRefOutput<'a>: ToArray<&'a T>,
Self: 'a,
T: 'a;
type IterMut<'a>: Iterator<Item = &'a mut T>
where Self::AsMutOutput<'a>: ToArray<&'a mut T>,
Self: 'a,
T: 'a;
// Required methods
fn to_array(self) -> Self::Array;
fn iter<'a>(&'a self) -> Self::Iter<'a>
where Self::AsRefOutput<'a>: ToArray<&'a T>,
Self: 'a,
T: 'a;
fn iter_mut<'a>(&'a mut self) -> Self::IterMut<'a>
where Self::AsMutOutput<'a>: ToArray<&'a mut T>,
Self: 'a,
T: 'a;
}
Expand description
Convert from tuples to primitive arrays, if all elements of the tuple are of the same type.
Because the generic constant expressions feature is still unstable yet,
we can only limit the maximum number of elements of the tuple that implement ToArray
to 32,
just like what we did with the primitive tuples.
However, if you are OK with using rustc released to nightly channel to compile codes with unstable features,
you can enable the any_array
feature to implement ToArray
on tuples with no limit on the number of elements.
tuplez = { features = [ "any_array" ] }
Always remember: unstable features are not guaranteed by Rust and may not be available someday in the future.
Why <T>
instead of type Item
? Well, this is because the Unit
s can be converted to any [T; 0]
.
Required Associated Types§
sourcetype Array: IntoIterator<Item = T>
type Array: IntoIterator<Item = T>
The primitive array type to generate.
Required Methods§
sourcefn to_array(self) -> Self::Array
fn to_array(self) -> Self::Array
Convert from the tuple to the primitive array.
§Example
use tuplez::{tuple, ToArray};
let tup = tuple!(1, 2, 3, 4, 5, 6);
assert_eq!(tup.to_array(), [1, 2, 3, 4, 5, 6]);
sourcefn iter<'a>(&'a self) -> Self::Iter<'a>
fn iter<'a>(&'a self) -> Self::Iter<'a>
Get immutable element iterator.
§Example
use tuplez::{tuple, ToArray};
let tup = tuple!(1, 2, 3, 4, 5, 6);
assert_eq!(tup.iter().sum::<i32>(), 21);