extend-fn 1.0.0

Use arbitrary FnMut when something that must implement Extend is needed
Documentation
#![cfg_attr(not(test), no_std)]
//! [fn-mut]: `FnMut`
//! [extend]: `core::iter::Extend`
#![doc = include_str!("../README.md")]

/// Use an arbitrary [`FnMut`] type to implement [`Extend`][extend].
///
/// ```rust
/// use extend_fn::ExtendUsing; 
///
/// let numbers = [5, 2, 11usize];
///
/// let mut result = 1usize; 
/// let mut extender = ExtendUsing::new(|value| result *= value);
/// extender.extend(numbers.iter().copied());
///
/// assert_eq!(numbers.iter().product::<usize>(), result);
/// ```
///
/// For cases where you are writing into an actual container and want to
/// transform items before their addition, consider using the 
/// [`extend_map` crate][extend-map] instead.
///
/// [extend]: `core::iter::Extend`
/// [extend-map]: https://crates.io/crates/extend_map
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub struct ExtendUsing<F: ?Sized>(pub F);

impl<F> ExtendUsing<F> {
    #[inline]
    pub const fn new(f: F) -> Self {
        Self(f)
    }
}

impl<F> From<F> for ExtendUsing<F> {
    #[inline]
    fn from(value: F) -> Self {
        Self::new(value) 
    }
}

impl<Item, F: ?Sized + FnMut(Item)> Extend<Item> for ExtendUsing<F> {
    #[inline]
    fn extend<I: IntoIterator<Item = Item>>(&mut self, iter: I) {
        for item in iter {
            self.0(item)
        }
    }
}

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.