use alloc::{string::String, vec::Vec};
mod private {
use super::*;
pub trait Sealed<T> {}
impl<T> Sealed<T> for T {}
impl<T, A: Sealed<T>> Sealed<T> for (A, T) {}
impl<T> Sealed<T> for Option<T> {}
impl<T> Sealed<T> for Vec<T> {}
impl<T> Sealed<T> for Option<Vec<T>> {}
impl<T> Sealed<T> for Vec<Option<T>> {}
impl<T, A: Sealed<T>> Sealed<T> for Vec<(A, T)> {}
impl Sealed<char> for String {}
impl Sealed<char> for Option<String> {}
}
#[allow(clippy::len_without_is_empty)]
pub trait Chain<T>: private::Sealed<T> {
fn len(&self) -> usize;
fn append_to(self, v: &mut Vec<T>);
}
impl<T> Chain<T> for T {
fn len(&self) -> usize {
1
}
fn append_to(self, v: &mut Vec<T>) {
v.push(self);
}
}
impl<T, A: Chain<T>> Chain<T> for (A, T) {
fn len(&self) -> usize {
1
}
fn append_to(self, v: &mut Vec<T>) {
self.0.append_to(v);
v.push(self.1);
}
}
impl<T> Chain<T> for Option<T> {
fn len(&self) -> usize {
self.is_some() as usize
}
fn append_to(self, v: &mut Vec<T>) {
if let Some(x) = self {
v.push(x);
}
}
}
impl<T> Chain<T> for Vec<T> {
fn len(&self) -> usize {
self.as_slice().len()
}
fn append_to(mut self, v: &mut Vec<T>) {
v.append(&mut self);
}
}
impl Chain<char> for String {
fn len(&self) -> usize {
self.chars().count()
}
fn append_to(self, v: &mut Vec<char>) {
v.extend(self.chars());
}
}
impl<T> Chain<T> for Option<Vec<T>> {
fn len(&self) -> usize {
self.as_ref().map_or(0, Chain::<T>::len)
}
fn append_to(self, v: &mut Vec<T>) {
if let Some(x) = self {
x.append_to(v);
}
}
}
impl Chain<char> for Option<String> {
fn len(&self) -> usize {
self.as_ref().map_or(0, Chain::<char>::len)
}
fn append_to(self, v: &mut Vec<char>) {
if let Some(x) = self {
x.append_to(v);
}
}
}
impl<T> Chain<T> for Vec<Option<T>> {
fn len(&self) -> usize {
self.iter().map(Chain::<T>::len).sum()
}
fn append_to(self, v: &mut Vec<T>) {
self
.into_iter()
.for_each(|x| x.append_to(v));
}
}
impl<T, A: Chain<T>> Chain<T> for Vec<(A, T)> {
fn len(&self) -> usize {
self.iter().map(Chain::<T>::len).sum()
}
fn append_to(self, v: &mut Vec<T>) {
self
.into_iter()
.for_each(|x| x.append_to(v));
}
}