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
9090 }
9191
9292 impl Deref for Id {
93− type Target = [u8];
93+ type Target = [u8; 20];
9494
9595 fn deref(&self) -> &Self::Target {
96− &self.0[..]
96+ &self.0
9797 }
9898 }
9999
git-odb/src/pack/bundle.rs+1−1
@@ -48,7 +48,7 @@ impl Bundle { · 47 unmodified lines
4848 /// For the latter, pack streams are required.
4949 pub fn locate<'a>(
5050 &self,
51− id: &[u8],
51+ id: &[u8; 20],
5252 out: &'a mut Vec<u8>,
5353 cache: &mut impl pack::cache::DecodeEntry,
5454 ) -> Option<Result<Object<'a>, Error>> {
git-odb/src/pack/index/accesss.rs+5−3
@@ -57,7 +57,7 @@ impl index::File { · 56 unmodified lines
5757
5858 /// Returns 20 bytes sha1 at the given index in our list of (sorted) sha1 hashes.
5959 /// 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] {
6161 let index: usize = index
6262 .try_into()
6363 .expect("an architecture able to hold 32 bits of integer");
@@ -65,7 +65,9 @@ impl index::File { · 1 unmodified line
6565 index::Kind::V2 => V2_HEADER_SIZE + index * SHA1_SIZE,
6666 index::Kind::V1 => V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE) + N32_SIZE,
6767 };
68− &self.data[start..start + SHA1_SIZE]
68+ (&self.data[start..start + SHA1_SIZE])
69+ .try_into()
70+ .expect("20 bytes exactly")
6971 }
7072
7173 pub fn pack_offset_at_index(&self, index: u32) -> u64 {
@@ -98,7 +100,7 @@ impl index::File { · 26 unmodified lines
98100 }
99101
100102 /// 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> {
102104 let first_byte = id[0] as usize;
103105 let mut upper_bound = self.fan[first_byte];
104106 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
55 * [x] extract borrowed objects from a pack
66 * [ ] ~~support streamed objects (similar to how it's done with loose objects)~~ - no need, all slices support io::Read, and we don't
77 actually support streaming, so let's net unify 'interfaces' on a low level like this.
8−* **borrowed objects**
9− * [ ] write loose
8+* **owned objects**
9+ * [ ] encode
1010 * [ ] blob
11− * [ ] tag
11+ * [x] tag
1212 * [ ] tree
1313 * [ ] commit
14+ * [ ] write loose
1415 * **plumbing - explode pack**
1516 * [ ] single threaded
1617 * [ ] multi-threaded