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
/// A helper trait for objects that must be consumed with the help of another value. /// /// If the method to consume a value is an associated function or requires additional arguments /// that cannot be determined at compile-time, this trait provides a simple way to implement /// Dispose for any container that has both the value to be disposed and any other values /// necessary, as demonstrated with [this implementation for pairs][(W, T)]. /// /// [(W, T)]: ./trait.Dispose.html#impl-Dispose-for-(W%2C%20T) pub trait DisposeWith<W> { /// Dispose self, using the provided value. fn dispose_with(self, with: W); } impl<W, F: FnOnce(W)> DisposeWith<W> for F { /// Run the closure with the given parameter, consuming both. fn dispose_with(self, with: W) { self(with); } } /// A helper trait for iterators with items implementing `DisposeWith`. /// /// This trait exists for a reason similar to [`DisposeIterator`] — namely, that implementing /// `DisposeWith` for `IntoIterator` causes conflicts with other implementations. More info can be /// found in the documentation for [`DisposeIterator`]. /// /// [`DisposeIterator`]: ./trait.DisposeIterator.html pub trait DisposeIteratorWith<W> { /// Dispose all items in the iterator using the provided value, consuming both. fn dispose_iter_with(self, with: W); } impl<W: Copy, I: IntoIterator> DisposeIteratorWith<W> for I where I::Item: DisposeWith<W> { /// Dispose all items in the iterator, using copies of the provided value. fn dispose_iter_with(self, with: W) { for el in self { el.dispose_with(with); } } } // TODO: add a more relaxed Clone impl whenever trait specialization is stabilized impl<W, T> DisposeWith<W> for Vec<T> where Vec<T>: DisposeIteratorWith<W> { fn dispose_with(self, with: W) { self.dispose_iter_with(with) } } impl<W, T> DisposeWith<W> for Box<[T]> where Vec<T>: DisposeIteratorWith<W> { fn dispose_with(self, with: W) { self.into_vec().dispose_iter_with(with) } } impl<'a, W, T> DisposeWith<W> for &'a [T] where &'a [T]: DisposeIteratorWith<W> { fn dispose_with(self, with: W) { self.dispose_iter_with(with) } }