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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
//! Extension for typle types.
//!
//! All traits in this module are implemented with tuple no longer than 10.
//!
//! By default, the module only expose implementation for tuple no longer than 5.
//! You can use longer impls by enabling the `long-tuple-impl` feature.
//!
//! Note that the [`TupleConcat`] trait is only implemented for those types that
//! returns a tuple shorter than 10.
/// Zip a tuple with another single value.
///
/// For concating two tuples, see [`TupleConcat`] for more details.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleZip;
///
/// let x = (10u8, 'a');
/// let y = x.zip(-5i32);
///
/// assert_eq!(y, (10u8, 'a', -5i32));
/// ```
pub trait TupleZip<T> {
type Output;
/// Zip with another single value.
fn zip(self, val: T) -> Self::Output;
}
/// Insert a value into a tuple.
///
/// This is internally used behind the [`TupleInsert`] trait for convenience.
///
/// However, if something goes wrong with the trait solver,
/// this trait can also be used explicitly.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleInsertExact;
///
/// let x = (10u8, 'a');
/// let y = <(u8, char) as TupleInsertExact<1, _>>::insert(x, -5i32);
///
/// assert_eq!(y, (10u8, -5i32, 'a'));
/// ```
pub trait TupleInsertExact<const POS: usize, T> {
type Output;
fn insert(self, val: T) -> Self::Output;
}
/// Insert a value into a tuple.
///
/// Unlike `TupleInsertExact`, this trait moves the `const POS` to a method,
/// so it can be used with the help of type derivation
/// without explicitly declaring the underlying trait.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleInsert;
///
/// let x = (10u8, 'a');
/// let y = x.insert::<1>(-5i32);
///
/// assert_eq!(y, (10u8, -5i32, 'a'));
/// ```
pub trait TupleInsert<T> {
fn insert<const POS: usize>(self, val: T) -> Self::Output
where
Self: TupleInsertExact<POS, T> + Sized,
{
<Self as TupleInsertExact<POS, T>>::insert(self, val)
}
}
/// Remove a value from a tuple.
///
/// This is internally used behind the [`TupleRemove`] trait for convenience.
///
/// However, if something goes wrong with the trait solver,
/// this trait can also be used explicitly.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleRemoveExact;
///
/// let x = (10u8, 'a', -5i32);
/// let y = <(u8, char, i32) as TupleRemoveExact<1>>::remove(x);
///
/// assert_eq!(y, (10u8, -5i32));
/// ```
pub trait TupleRemoveExact<const POS: usize> {
type Output;
fn remove(self) -> Self::Output;
}
/// Remove a value from a tuple.
///
/// Unlike `TupleRemoveExact`, this trait moves the `const POS` to a method,
/// so it can be used with the help of type derivation
/// without explicitly declaring the underlying trait.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleRemove;
///
/// let x = (10u8, 'a', -5i32);
/// let y = x.remove::<1>();
///
/// assert_eq!(y, (10u8, -5i32));
/// ```
pub trait TupleRemove {
fn remove<const POS: usize>(self) -> Self::Output
where
Self: TupleRemoveExact<POS> + Sized,
{
<Self as TupleRemoveExact<POS>>::remove(self)
}
}
/// Concat two tuples.
///
/// This works like [`TupleZip`], but takes another tuple as input.
///
/// **Note:**
/// This trait only has implementations for operations that return tuples of
/// length 5 / 10 (`long-tuple-impl` feature) or less.
/// That means that `(A, B, C, D, E, F, G)` cannot be concated with `(H, I, J, K, L)`.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::tuple::TupleConcat;
///
/// let x = (10u8, 'a');
/// let y = (-5i32, "foo");
/// let z = x.concat(y);
///
/// assert_eq!(z, (10u8, 'a', -5i32, "foo"));
/// ```
pub trait TupleConcat<T> {
type Output;
fn concat(self, other: T) -> Self::Output;
}
mod __generated {
use paste::paste;
use super::{
TupleConcat, TupleInsert, TupleInsertExact, TupleRemove, TupleRemoveExact, TupleZip,
};
macro_rules! __impl_tuple_zip {
($($ph:ident),+ | $tar:ident) => {
paste! {
impl<$($ph),+, $tar> TupleZip<$tar> for ($($ph,)+) {
type Output = ($($ph),+, $tar);
fn zip(self, val: $tar) -> Self::Output {
let ($([< $ph:lower >],)+) = self;
($([< $ph:lower >]),+, val)
}
}
}
};
}
macro_rules! __impl_tuple_insert {
($($ph:ident),+ | $tar:ident) => {
impl<$($ph),+, $tar> TupleInsert<$tar> for ($($ph,)+) {}
};
}
macro_rules! __impl_tuple_insert_exact {
($($ph:ident),+ | $tar:ident at $index:expr => $($res:ident),+) => {
paste! {
impl<$($ph),+, $tar> TupleInsertExact<$index, $tar> for ($($ph,)+) {
type Output = ($($res,)+);
fn insert(self, val: $tar) -> Self::Output {
let [< $tar:lower >] = val;
let ($([< $ph:lower >],)+) = self;
($([< $res:lower >]),+)
}
}
}
};
}
macro_rules! __impl_tuple_remove {
($($ph:ident),+) => {
impl<$($ph),+> TupleRemove for ($($ph,)+) {}
};
}
macro_rules! __impl_tuple_remove_exact {
($($ph:ident),+ at $index:expr => $($res:ident),+) => {
paste! {
impl<$($ph),+> TupleRemoveExact<$index> for ($($ph,)+) {
type Output = ($($res,)+);
#[allow(unused_variables)]
fn remove(self) -> Self::Output {
let ($([< $ph:lower >],)+) = self;
($([< $res:lower >],)+)
}
}
}
};
}
macro_rules! __impl_tuple_concat {
($($ph:ident),+ with $($tar:ident),+) => {
paste! {
impl<$($ph),+, $($tar),+> TupleConcat<($($tar,)+)> for ($($ph,)+) {
type Output = ($($ph),+, $($tar),+);
fn concat(self, other: ($($tar,)+)) -> Self::Output {
let ($([< $ph:lower >],)+) = self;
let ($([< $tar:lower >],)+) = other;
($([< $ph:lower >]),+, $([< $tar:lower >]),+)
}
}
}
};
}
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/tuple_short_impl.rs"
));
#[cfg(feature = "long-tuple-impl")]
mod __long_tuple_impl {
use paste::paste;
use super::super::{
TupleConcat, TupleInsert, TupleInsertExact, TupleRemove, TupleRemoveExact, TupleZip,
};
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/tuple_long_impl.rs"
));
}
}