rune_alloc/iter/
try_extend.rs

1use crate::error::Error;
2
3/// Extend a collection with the contents of an iterator.
4///
5/// Iterators produce a series of values, and collections can also be thought
6/// of as a series of values. The `Extend` trait bridges this gap, allowing you
7/// to extend a collection by including the contents of that iterator. When
8/// extending a collection with an already existing key, that entry is updated
9/// or, in the case of collections that permit multiple entries with equal
10/// keys, that entry is inserted.
11///
12/// # Examples
13///
14/// Basic usage:
15///
16/// ```
17/// // You can extend a String with some chars:
18/// let mut message = String::from("The first three letters are: ");
19///
20/// message.extend(&['a', 'b', 'c']);
21///
22/// assert_eq!("abc", &message[29..32]);
23/// ```
24///
25/// Implementing `Extend`:
26///
27/// ```
28/// // A sample collection, that's just a wrapper over Vec<T>
29/// #[derive(Debug)]
30/// struct MyCollection(Vec<i32>);
31///
32/// // Let's give it some methods so we can create one and add things
33/// // to it.
34/// impl MyCollection {
35///     fn new() -> MyCollection {
36///         MyCollection(Vec::new())
37///     }
38///
39///     fn add(&mut self, elem: i32) {
40///         self.0.push(elem);
41///     }
42/// }
43///
44/// // since MyCollection has a list of i32s, we implement Extend for i32
45/// impl Extend<i32> for MyCollection {
46///
47///     // This is a bit simpler with the concrete type signature: we can call
48///     // extend on anything which can be turned into an Iterator which gives
49///     // us i32s. Because we need i32s to put into MyCollection.
50///     fn extend<T: IntoIterator<Item=i32>>(&mut self, iter: T) {
51///
52///         // The implementation is very straightforward: loop through the
53///         // iterator, and add() each element to ourselves.
54///         for elem in iter {
55///             self.add(elem);
56///         }
57///     }
58/// }
59///
60/// let mut c = MyCollection::new();
61///
62/// c.add(5);
63/// c.add(6);
64/// c.add(7);
65///
66/// // let's extend our collection with three more numbers
67/// c.extend(vec![1, 2, 3]);
68///
69/// // we've added these elements onto the end
70/// assert_eq!("MyCollection([5, 6, 7, 1, 2, 3])", format!("{c:?}"));
71/// ```
72pub trait TryExtend<T> {
73    /// Extends a collection with the contents of an iterator.
74    ///
75    /// As this is the only required method for this trait, the [trait-level]
76    /// docs contain more details.
77    ///
78    /// [trait-level]: TryExtend
79    ///
80    /// # Examples
81    ///
82    /// Basic usage:
83    ///
84    /// ```
85    /// use rune::alloc::String;
86    /// use rune::alloc::prelude::*;
87    ///
88    /// // You can extend a String with some chars:
89    /// let mut message = String::try_from("abc")?;
90    ///
91    /// message.try_extend(['d', 'e', 'f'].into_iter())?;
92    ///
93    /// assert_eq!("abcdef", &message);
94    /// # Ok::<_, rune::alloc::Error>(())
95    /// ```
96    fn try_extend<I>(&mut self, iter: I) -> Result<(), Error>
97    where
98        I: IntoIterator<Item = T>;
99}