use std::borrow::Cow;
use std::iter::FusedIterator;
pub trait IntoOwned {
type Owned;
fn into_owned(self) -> Self::Owned;
}
impl<T: ToOwned + ?Sized> IntoOwned for &T {
type Owned = T::Owned;
fn into_owned(self) -> Self::Owned {
self.to_owned()
}
}
macro_rules! impl_into_owned_for_tuple {
($($T:ident),+) => {
impl<$($T: IntoOwned),+> IntoOwned for ($($T,)+) {
type Owned = ($($T::Owned,)+);
fn into_owned(self) -> Self::Owned {
#[allow(non_snake_case)]
let ($($T,)+) = self;
($($T.into_owned(),)+)
}
}
};
}
impl_into_owned_for_tuple!(A);
impl_into_owned_for_tuple!(A, B);
impl_into_owned_for_tuple!(A, B, C);
impl_into_owned_for_tuple!(A, B, C, D);
impl_into_owned_for_tuple!(A, B, C, D, E);
impl_into_owned_for_tuple!(A, B, C, D, E, F);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G, H);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G, H, I);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G, H, I, J);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G, H, I, J, K);
impl_into_owned_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
impl<'a, T: ToOwned + ?Sized> IntoOwned for Cow<'a, T> {
type Owned = T::Owned;
fn into_owned(self) -> T::Owned {
Cow::into_owned(self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct MapIntoOwned<I>(pub I);
impl<I: Iterator> Iterator for MapIntoOwned<I>
where
I::Item: IntoOwned,
{
type Item = <I::Item as IntoOwned>::Owned;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(IntoOwned::into_owned)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
fn last(self) -> Option<Self::Item> {
self.0.last().map(IntoOwned::into_owned)
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth(n).map(IntoOwned::into_owned)
}
}
impl<I: DoubleEndedIterator> DoubleEndedIterator for MapIntoOwned<I>
where
I::Item: IntoOwned,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(IntoOwned::into_owned)
}
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth_back(n).map(IntoOwned::into_owned)
}
}
impl<I: ExactSizeIterator> ExactSizeIterator for MapIntoOwned<I>
where
I::Item: IntoOwned,
{
fn len(&self) -> usize {
self.0.len()
}
}
impl<I: FusedIterator> FusedIterator for MapIntoOwned<I> where I::Item: IntoOwned {}
#[derive(Debug, Clone, Copy)]
pub struct MapOkIntoOwned<I>(pub I);
impl<T: IntoOwned, E, I: Iterator<Item = Result<T, E>>> Iterator for MapOkIntoOwned<I> {
type Item = Result<T::Owned, E>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|r| r.map(IntoOwned::into_owned))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
fn last(self) -> Option<Self::Item> {
self.0.last().map(|r| r.map(IntoOwned::into_owned))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth(n).map(|r| r.map(IntoOwned::into_owned))
}
}
impl<T: IntoOwned, E, I: DoubleEndedIterator<Item = Result<T, E>>> DoubleEndedIterator
for MapOkIntoOwned<I>
{
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|r| r.map(IntoOwned::into_owned))
}
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth_back(n).map(|r| r.map(IntoOwned::into_owned))
}
}
impl<T: IntoOwned, E, I: ExactSizeIterator<Item = Result<T, E>>> ExactSizeIterator
for MapOkIntoOwned<I>
{
fn len(&self) -> usize {
self.0.len()
}
}
impl<T: IntoOwned, E, I: FusedIterator<Item = Result<T, E>>> FusedIterator for MapOkIntoOwned<I> {}