#![no_std]
#![doc = include_str!("../README.md")]
#[doc = include_str!("../README.md")]
pub trait TupleExt: Sized {
type Left;
type Right;
type Pack<T>;
fn right_as<D>(self, to: &mut D) -> Self::Pack<Self::Left>
where Self::Right: Into<D>;
fn left_as<D>(self, to: &mut D) -> Self::Pack<Self::Right>
where Self::Left: Into<D>;
}
impl<L, R> TupleExt for (L, R) {
type Left = L;
type Right = R;
type Pack<T> = T;
fn right_as<D>(self, to: &mut D) -> Self::Pack<Self::Left>
where Self::Right: Into<D>,
{
*to = self.1.into();
self.0
}
fn left_as<D>(self, to: &mut D) -> Self::Pack<Self::Right>
where Self::Left: Into<D>,
{
*to = self.0.into();
self.1
}
}
impl<L> TupleExt for [L; 2] {
type Left = L;
type Right = L;
type Pack<T> = T;
fn right_as<D>(self, to: &mut D) -> Self::Pack<Self::Left>
where Self::Right: Into<D>,
{
let [left, right] = self;
*to = right.into();
left
}
fn left_as<D>(self, to: &mut D) -> Self::Pack<Self::Right>
where Self::Left: Into<D>,
{
let [left, right] = self;
*to = left.into();
right
}
}
impl<L, R> TupleExt for Option<(L, R)> {
type Left = L;
type Right = R;
type Pack<T> = Option<T>;
fn right_as<D>(self, to: &mut D) -> Self::Pack<Self::Left>
where Self::Right: Into<D>,
{
self.map(|x| x.right_as(to))
}
fn left_as<D>(self, to: &mut D) -> Self::Pack<Self::Right>
where Self::Left: Into<D>,
{
self.map(|x| x.left_as(to))
}
}
impl<L, R, E> TupleExt for Result<(L, R), E> {
type Left = L;
type Right = R;
type Pack<T> = Result<T, E>;
fn right_as<D>(self, to: &mut D) -> Self::Pack<Self::Left>
where Self::Right: Into<D>,
{
self.map(|x| x.right_as(to))
}
fn left_as<D>(self, to: &mut D) -> Self::Pack<Self::Right>
where Self::Left: Into<D>,
{
self.map(|x| x.left_as(to))
}
}