##// END OF EJS Templates
rust: simply use TryInto to convert slice to array...
Yuya Nishihara -
r43067:53995325 default
parent child Browse files
Show More
@@ -7,13 +7,13 b''
7 7
8 8 use crate::{
9 9 dirstate::{parsers::PARENT_SIZE, EntryState},
10 pack_dirstate, parse_dirstate,
11 utils::copy_into_array,
12 CopyMap, DirsIterable, DirsMultiset, DirstateEntry, DirstateError,
13 DirstateMapError, DirstateParents, DirstateParseError, StateMap,
10 pack_dirstate, parse_dirstate, CopyMap, DirsIterable, DirsMultiset,
11 DirstateEntry, DirstateError, DirstateMapError, DirstateParents,
12 DirstateParseError, StateMap,
14 13 };
15 14 use core::borrow::Borrow;
16 15 use std::collections::{HashMap, HashSet};
16 use std::convert::TryInto;
17 17 use std::iter::FromIterator;
18 18 use std::ops::Deref;
19 19 use std::time::Duration;
@@ -260,10 +260,10 b' impl DirstateMap {'
260 260 let parents;
261 261 if file_contents.len() == PARENT_SIZE * 2 {
262 262 parents = DirstateParents {
263 p1: copy_into_array(&file_contents[..PARENT_SIZE]),
264 p2: copy_into_array(
265 &file_contents[PARENT_SIZE..PARENT_SIZE * 2],
266 ),
263 p1: file_contents[..PARENT_SIZE].try_into().unwrap(),
264 p2: file_contents[PARENT_SIZE..PARENT_SIZE * 2]
265 .try_into()
266 .unwrap(),
267 267 };
268 268 } else if file_contents.is_empty() {
269 269 parents = DirstateParents {
@@ -5,7 +5,6 b''
5 5
6 6 use crate::{
7 7 dirstate::{CopyMap, EntryState, StateMap},
8 utils::copy_into_array,
9 8 DirstateEntry, DirstatePackError, DirstateParents, DirstateParseError,
10 9 };
11 10 use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
@@ -31,8 +30,8 b' pub fn parse_dirstate('
31 30
32 31 let mut curr_pos = PARENT_SIZE * 2;
33 32 let parents = DirstateParents {
34 p1: copy_into_array(&contents[..PARENT_SIZE]),
35 p2: copy_into_array(&contents[PARENT_SIZE..curr_pos]),
33 p1: contents[..PARENT_SIZE].try_into().unwrap(),
34 p2: contents[PARENT_SIZE..curr_pos].try_into().unwrap(),
36 35 };
37 36
38 37 while curr_pos < contents.len() {
@@ -9,23 +9,6 b''
9 9
10 10 pub mod files;
11 11
12 use std::convert::AsMut;
13
14 /// Takes a slice and copies it into an array.
15 ///
16 /// # Panics
17 ///
18 /// Will panic if the slice and target array don't have the same length.
19 pub fn copy_into_array<A, T>(slice: &[T]) -> A
20 where
21 A: Sized + Default + AsMut<[T]>,
22 T: Copy,
23 {
24 let mut a = Default::default();
25 <A as AsMut<[T]>>::as_mut(&mut a).copy_from_slice(slice);
26 a
27 }
28
29 12 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
30 13 ///
31 14 /// # Examples
@@ -24,7 +24,7 b' use crate::{'
24 24 ref_sharing::PySharedState,
25 25 };
26 26 use hg::{
27 utils::copy_into_array, DirsIterable, DirsMultiset, DirstateEntry,
27 DirsIterable, DirsMultiset, DirstateEntry,
28 28 DirstateMap as RustDirstateMap, DirstateParents, DirstateParseError,
29 29 EntryState,
30 30 };
@@ -239,8 +239,9 b' py_class!(pub class DirstateMap |py| {'
239 239 }
240 240
241 241 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
242 let p1 = copy_into_array(p1.extract::<PyBytes>(py)?.data(py));
243 let p2 = copy_into_array(p2.extract::<PyBytes>(py)?.data(py));
242 // TODO: don't panic; raise Python exception instead.
243 let p1 = p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
244 let p2 = p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
244 245
245 246 self.inner(py)
246 247 .borrow_mut()
@@ -274,8 +275,9 b' py_class!(pub class DirstateMap |py| {'
274 275 ) -> PyResult<PyBytes> {
275 276 let now = Duration::new(now.extract(py)?, 0);
276 277 let parents = DirstateParents {
277 p1: copy_into_array(p1.extract::<PyBytes>(py)?.data(py)),
278 p2: copy_into_array(p2.extract::<PyBytes>(py)?.data(py)),
278 // TODO: don't panic; raise Python exception instead.
279 p1: p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
280 p2: p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
279 281 };
280 282
281 283 match self.borrow_mut(py)?.pack(parents, now) {
@@ -15,10 +15,11 b' use cpython::{'
15 15 PythonObject, ToPyObject,
16 16 };
17 17 use hg::{
18 pack_dirstate, parse_dirstate, utils::copy_into_array, DirstateEntry,
18 pack_dirstate, parse_dirstate, DirstateEntry,
19 19 DirstatePackError, DirstateParents, DirstateParseError, PARENT_SIZE,
20 20 };
21 21 use std::collections::HashMap;
22 use std::convert::TryInto;
22 23
23 24 use libc::c_char;
24 25
@@ -120,8 +121,8 b' fn pack_dirstate_wrapper('
120 121 &mut dirstate_map,
121 122 &copies?,
122 123 DirstateParents {
123 p1: copy_into_array(&p1),
124 p2: copy_into_array(&p2),
124 p1: p1.try_into().unwrap(),
125 p2: p2.try_into().unwrap(),
125 126 },
126 127 Duration::from_secs(now.as_object().extract::<u64>(py)?),
127 128 ) {
General Comments 0
You need to be logged in to leave comments. Login now