Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/revlog/path_encode.rs @ 50161:5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Thu, 16 Feb 2023 18:45:23 +0000 |
parents | 5ce53ff6133a |
children | 6baea276a985 |
comparison
equal
deleted
inserted
replaced
50160:5ce53ff6133a | 50161:5d7ba99da3b8 |
---|---|
45 bitset[(c as usize) >> 5] & (1 << (c & 31)) != 0 | 45 bitset[(c as usize) >> 5] & (1 << (c & 31)) != 0 |
46 } | 46 } |
47 | 47 |
48 const MAXENCODE: usize = 4096 * 4; | 48 const MAXENCODE: usize = 4096 * 4; |
49 | 49 |
50 struct DestArr { | 50 struct DestArr<const N: usize> { |
51 buf: [u8; MAXENCODE], | 51 buf: [u8; N], |
52 pub len: usize, | 52 pub len: usize, |
53 } | 53 } |
54 | 54 |
55 impl DestArr { | 55 impl<const N: usize> DestArr<N> { |
56 pub fn create() -> Self { | 56 pub fn create() -> Self { |
57 DestArr { | 57 DestArr { |
58 buf: [0; MAXENCODE], | 58 buf: [0; N], |
59 len: 0, | 59 len: 0, |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 pub fn contents(&self) -> &[u8] { | 63 pub fn contents(&self) -> &[u8] { |
64 &self.buf[..self.len] | 64 &self.buf[..self.len] |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 impl Sink for DestArr { | 68 impl<const N: usize> Sink for DestArr<N> { |
69 fn write_byte(&mut self, c: u8) { | 69 fn write_byte(&mut self, c: u8) { |
70 self.buf[self.len] = c; | 70 self.buf[self.len] = c; |
71 self.len += 1; | 71 self.len += 1; |
72 } | 72 } |
73 | 73 |
598 dest_vec[..destlen].to_vec() | 598 dest_vec[..destlen].to_vec() |
599 } | 599 } |
600 } | 600 } |
601 | 601 |
602 fn hash_encode(src: &[u8]) -> Vec<u8> { | 602 fn hash_encode(src: &[u8]) -> Vec<u8> { |
603 let mut dired = DestArr::create(); | 603 let mut dired: DestArr<MAXENCODE> = DestArr::create(); |
604 let mut lowered = DestArr::create(); | 604 let mut lowered: DestArr<MAXENCODE> = DestArr::create(); |
605 let mut auxed = DestArr::create(); | 605 let mut auxed: DestArr<MAXENCODE> = DestArr::create(); |
606 let baselen = (src.len() - 5) * 3; | 606 let baselen = (src.len() - 5) * 3; |
607 if baselen >= MAXENCODE { | 607 if baselen >= MAXENCODE { |
608 panic!("path_encode::hash_encore: string too long: {}", baselen) | 608 panic!("path_encode::hash_encore: string too long: {}", baselen) |
609 }; | 609 }; |
610 encode_dir(&mut dired, src); | 610 encode_dir(&mut dired, src); |