diff rust/hg-core/src/utils.rs @ 42748:7cae6bc29ff9

rust-parsers: switch to parse/pack_dirstate to mutate-on-loop Both `parse_dirstate` and `pack_dirstate` can operate directly on the data they're passed, which prevents the creation of intermediate data structures, simplifies the function signatures and reduces boilerplate. They are exposed directly to the Python for now, but a later patch will make use of them inside `hg-core`. Differential Revision: https://phab.mercurial-scm.org/D6628
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 09 Jul 2019 11:49:49 +0200
parents 2f760da140ee
children 4b3b27d567d5
line wrap: on
line diff
--- a/rust/hg-core/src/utils.rs	Wed Jul 10 10:16:28 2019 +0200
+++ b/rust/hg-core/src/utils.rs	Tue Jul 09 11:49:49 2019 +0200
@@ -1,5 +1,22 @@
 pub mod files;
 
+use std::convert::AsMut;
+
+/// Takes a slice and copies it into an array.
+///
+/// # Panics
+///
+/// Will panic if the slice and target array don't have the same length.
+pub fn copy_into_array<A, T>(slice: &[T]) -> A
+where
+    A: Sized + Default + AsMut<[T]>,
+    T: Copy,
+{
+    let mut a = Default::default();
+    <A as AsMut<[T]>>::as_mut(&mut a).copy_from_slice(slice);
+    a
+}
+
 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
 ///
 /// # Examples