History

Use statically known borrowed arrays for perfect type safety!

https://stackoverflow.com/questions/25428920/how-to-get-a-slice-as-an-array-in-rust

Sebastian Thiel committed 6 years ago

Commit 3ead0481 parent 766f3e44 files · +12 −9Browse files

Unified · Split

4 files
  1. git-object/src/types.rs +2 −2
  2. git-odb/src/pack/bundle.rs +1 −1
  3. git-odb/src/pack/index/accesss.rs +5 −3
  4. tasks.md +4 −3
git-object/src/types.rs+2−2
@@ -90,10 +90,10 @@ impl Id { · 89 unmodified lines
90}90}
91 91
92impl Deref for Id {92impl Deref for Id {
93 type Target = [u8];93 type Target = [u8; 20];
94 94
95 fn deref(&self) -> &Self::Target {95 fn deref(&self) -> &Self::Target {
96 &self.0[..]96 &self.0
97 }97 }
98}98}
99 99
git-odb/src/pack/bundle.rs+1−1
@@ -48,7 +48,7 @@ impl Bundle { · 47 unmodified lines
48 /// For the latter, pack streams are required.48 /// For the latter, pack streams are required.
49 pub fn locate<'a>(49 pub fn locate<'a>(
50 &self,50 &self,
51 id: &[u8],51 id: &[u8; 20],
52 out: &'a mut Vec<u8>,52 out: &'a mut Vec<u8>,
53 cache: &mut impl pack::cache::DecodeEntry,53 cache: &mut impl pack::cache::DecodeEntry,
54 ) -> Option<Result<Object<'a>, Error>> {54 ) -> Option<Result<Object<'a>, Error>> {
git-odb/src/pack/index/accesss.rs+5−3
@@ -57,7 +57,7 @@ impl index::File { · 56 unmodified lines
57 57
58 /// Returns 20 bytes sha1 at the given index in our list of (sorted) sha1 hashes.58 /// Returns 20 bytes sha1 at the given index in our list of (sorted) sha1 hashes.
59 /// The index ranges from 0 to self.num_objects()59 /// The index ranges from 0 to self.num_objects()
60 pub fn oid_at_index(&self, index: u32) -> &[u8] {60 pub fn oid_at_index(&self, index: u32) -> &[u8; 20] {
61 let index: usize = index61 let index: usize = index
62 .try_into()62 .try_into()
63 .expect("an architecture able to hold 32 bits of integer");63 .expect("an architecture able to hold 32 bits of integer");
@@ -65,7 +65,9 @@ impl index::File { · 1 unmodified line
65 index::Kind::V2 => V2_HEADER_SIZE + index * SHA1_SIZE,65 index::Kind::V2 => V2_HEADER_SIZE + index * SHA1_SIZE,
66 index::Kind::V1 => V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE) + N32_SIZE,66 index::Kind::V1 => V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE) + N32_SIZE,
67 };67 };
68 &self.data[start..start + SHA1_SIZE]68 (&self.data[start..start + SHA1_SIZE])
69 .try_into()
70 .expect("20 bytes exactly")
69 }71 }
70 72
71 pub fn pack_offset_at_index(&self, index: u32) -> u64 {73 pub fn pack_offset_at_index(&self, index: u32) -> u64 {
@@ -98,7 +100,7 @@ impl index::File { · 26 unmodified lines
98 }100 }
99 101
100 /// Returns the offset of the given object ID (20 bytes) for use with the `(oid|pack_offset|crc32)_at_index()`102 /// Returns the offset of the given object ID (20 bytes) for use with the `(oid|pack_offset|crc32)_at_index()`
101 pub fn lookup_index(&self, id: &[u8]) -> Option<u32> {103 pub fn lookup_index(&self, id: &[u8; 20]) -> Option<u32> {
102 let first_byte = id[0] as usize;104 let first_byte = id[0] as usize;
103 let mut upper_bound = self.fan[first_byte];105 let mut upper_bound = self.fan[first_byte];
104 let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };106 let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
tasks.md+4−3
@@ -5,12 +5,13 @@ · 4 unmodified lines
5 * [x] extract borrowed objects from a pack5 * [x] extract borrowed objects from a pack
6 * [ ] ~~support streamed objects (similar to how it's done with loose objects)~~ - no need, all slices support io::Read, and we don't6 * [ ] ~~support streamed objects (similar to how it's done with loose objects)~~ - no need, all slices support io::Read, and we don't
7 actually support streaming, so let's net unify 'interfaces' on a low level like this.7 actually support streaming, so let's net unify 'interfaces' on a low level like this.
8* **borrowed objects**8* **owned objects**
9 * [ ] write loose9 * [ ] encode
10 * [ ] blob10 * [ ] blob
11 * [ ] tag11 * [x] tag
12 * [ ] tree12 * [ ] tree
13 * [ ] commit13 * [ ] commit
14 * [ ] write loose
14* **plumbing - explode pack**15* **plumbing - explode pack**
15 * [ ] single threaded16 * [ ] single threaded
16 * [ ] multi-threaded17 * [ ] multi-threaded