]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_index/vec.rs
Merge branch 'master' of github.com:rust-lang/rust into add-tvSO-target
[rust.git] / src / librustc_index / vec.rs
index d14bafb44fd5b519c1ded8c80b32965ddd8bc97e..1dfe97238a3df8b7f6235f08b0383733d7e86992 100644 (file)
@@ -574,6 +574,14 @@ pub fn from_elem_n(elem: T, n: usize) -> Self
         IndexVec { raw: vec![elem; n], _marker: PhantomData }
     }
 
+    /// Create an `IndexVec` with `n` elements, where the value of each
+    /// element is the result of `func(i)`
+    #[inline]
+    pub fn from_fn_n(func: impl FnMut(I) -> T, n: usize) -> Self {
+        let indices = (0..n).map(I::new);
+        Self::from_raw(indices.map(func).collect())
+    }
+
     #[inline]
     pub fn push(&mut self, d: T) -> I {
         let idx = I::new(self.len());