##// 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 use crate::{
8 use crate::{
9 dirstate::{parsers::PARENT_SIZE, EntryState},
9 dirstate::{parsers::PARENT_SIZE, EntryState},
10 pack_dirstate, parse_dirstate,
10 pack_dirstate, parse_dirstate, CopyMap, DirsIterable, DirsMultiset,
11 utils::copy_into_array,
11 DirstateEntry, DirstateError, DirstateMapError, DirstateParents,
12 CopyMap, DirsIterable, DirsMultiset, DirstateEntry, DirstateError,
12 DirstateParseError, StateMap,
13 DirstateMapError, DirstateParents, DirstateParseError, StateMap,
14 };
13 };
15 use core::borrow::Borrow;
14 use core::borrow::Borrow;
16 use std::collections::{HashMap, HashSet};
15 use std::collections::{HashMap, HashSet};
16 use std::convert::TryInto;
17 use std::iter::FromIterator;
17 use std::iter::FromIterator;
18 use std::ops::Deref;
18 use std::ops::Deref;
19 use std::time::Duration;
19 use std::time::Duration;
@@ -260,10 +260,10 b' impl DirstateMap {'
260 let parents;
260 let parents;
261 if file_contents.len() == PARENT_SIZE * 2 {
261 if file_contents.len() == PARENT_SIZE * 2 {
262 parents = DirstateParents {
262 parents = DirstateParents {
263 p1: copy_into_array(&file_contents[..PARENT_SIZE]),
263 p1: file_contents[..PARENT_SIZE].try_into().unwrap(),
264 p2: copy_into_array(
264 p2: file_contents[PARENT_SIZE..PARENT_SIZE * 2]
265 &file_contents[PARENT_SIZE..PARENT_SIZE * 2],
265 .try_into()
266 ),
266 .unwrap(),
267 };
267 };
268 } else if file_contents.is_empty() {
268 } else if file_contents.is_empty() {
269 parents = DirstateParents {
269 parents = DirstateParents {
@@ -5,7 +5,6 b''
5
5
6 use crate::{
6 use crate::{
7 dirstate::{CopyMap, EntryState, StateMap},
7 dirstate::{CopyMap, EntryState, StateMap},
8 utils::copy_into_array,
9 DirstateEntry, DirstatePackError, DirstateParents, DirstateParseError,
8 DirstateEntry, DirstatePackError, DirstateParents, DirstateParseError,
10 };
9 };
11 use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
10 use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
@@ -31,8 +30,8 b' pub fn parse_dirstate('
31
30
32 let mut curr_pos = PARENT_SIZE * 2;
31 let mut curr_pos = PARENT_SIZE * 2;
33 let parents = DirstateParents {
32 let parents = DirstateParents {
34 p1: copy_into_array(&contents[..PARENT_SIZE]),
33 p1: contents[..PARENT_SIZE].try_into().unwrap(),
35 p2: copy_into_array(&contents[PARENT_SIZE..curr_pos]),
34 p2: contents[PARENT_SIZE..curr_pos].try_into().unwrap(),
36 };
35 };
37
36
38 while curr_pos < contents.len() {
37 while curr_pos < contents.len() {
@@ -9,23 +9,6 b''
9
9
10 pub mod files;
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 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
12 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
30 ///
13 ///
31 /// # Examples
14 /// # Examples
@@ -24,7 +24,7 b' use crate::{'
24 ref_sharing::PySharedState,
24 ref_sharing::PySharedState,
25 };
25 };
26 use hg::{
26 use hg::{
27 utils::copy_into_array, DirsIterable, DirsMultiset, DirstateEntry,
27 DirsIterable, DirsMultiset, DirstateEntry,
28 DirstateMap as RustDirstateMap, DirstateParents, DirstateParseError,
28 DirstateMap as RustDirstateMap, DirstateParents, DirstateParseError,
29 EntryState,
29 EntryState,
30 };
30 };
@@ -239,8 +239,9 b' py_class!(pub class DirstateMap |py| {'
239 }
239 }
240
240
241 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
241 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
242 let p1 = copy_into_array(p1.extract::<PyBytes>(py)?.data(py));
242 // TODO: don't panic; raise Python exception instead.
243 let p2 = copy_into_array(p2.extract::<PyBytes>(py)?.data(py));
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 self.inner(py)
246 self.inner(py)
246 .borrow_mut()
247 .borrow_mut()
@@ -274,8 +275,9 b' py_class!(pub class DirstateMap |py| {'
274 ) -> PyResult<PyBytes> {
275 ) -> PyResult<PyBytes> {
275 let now = Duration::new(now.extract(py)?, 0);
276 let now = Duration::new(now.extract(py)?, 0);
276 let parents = DirstateParents {
277 let parents = DirstateParents {
277 p1: copy_into_array(p1.extract::<PyBytes>(py)?.data(py)),
278 // TODO: don't panic; raise Python exception instead.
278 p2: copy_into_array(p2.extract::<PyBytes>(py)?.data(py)),
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 match self.borrow_mut(py)?.pack(parents, now) {
283 match self.borrow_mut(py)?.pack(parents, now) {
@@ -15,10 +15,11 b' use cpython::{'
15 PythonObject, ToPyObject,
15 PythonObject, ToPyObject,
16 };
16 };
17 use hg::{
17 use hg::{
18 pack_dirstate, parse_dirstate, utils::copy_into_array, DirstateEntry,
18 pack_dirstate, parse_dirstate, DirstateEntry,
19 DirstatePackError, DirstateParents, DirstateParseError, PARENT_SIZE,
19 DirstatePackError, DirstateParents, DirstateParseError, PARENT_SIZE,
20 };
20 };
21 use std::collections::HashMap;
21 use std::collections::HashMap;
22 use std::convert::TryInto;
22
23
23 use libc::c_char;
24 use libc::c_char;
24
25
@@ -120,8 +121,8 b' fn pack_dirstate_wrapper('
120 &mut dirstate_map,
121 &mut dirstate_map,
121 &copies?,
122 &copies?,
122 DirstateParents {
123 DirstateParents {
123 p1: copy_into_array(&p1),
124 p1: p1.try_into().unwrap(),
124 p2: copy_into_array(&p2),
125 p2: p2.try_into().unwrap(),
125 },
126 },
126 Duration::from_secs(now.as_object().extract::<u64>(py)?),
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