Implement WriteTo trait for gix::Blob
Unified · Split
| @@ -150,6 +150,23 @@ impl std::fmt::Debug for Object<'_> { · 149 unmodified lines | |||
| 150 | } | 150 | } |
| 151 | } | 151 | } |
| 152 | 152 | ||
| 153 | /// Note that the `data` written here might not correspond to the `id` of the `Blob` anymore if it was modified. | ||
| 154 | /// Also, this is merely for convenience when writing empty blobs to the ODB. For writing any blob, use | ||
| 155 | /// [`Repository::write_blob()`](crate::Repository::write_blob()). | ||
| 156 | impl gix_object::WriteTo for Blob<'_> { | ||
| 157 | fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> { | ||
| 158 | out.write_all(&self.data) | ||
| 159 | } | ||
| 160 | |||
| 161 | fn kind(&self) -> gix_object::Kind { | ||
| 162 | gix_object::Kind::Blob | ||
| 163 | } | ||
| 164 | |||
| 165 | fn size(&self) -> u64 { | ||
| 166 | self.data.len() as u64 | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 153 | /// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be | 170 | /// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be |
| 154 | /// returned to the free list. | 171 | /// returned to the free list. |
| 155 | fn steal_from_freelist(data: &mut Vec<u8>) -> Vec<u8> { | 172 | fn steal_from_freelist(data: &mut Vec<u8>) -> Vec<u8> { |
| @@ -262,7 +262,7 @@ mod write_object { · 261 unmodified lines | |||
| 262 | let oid = repo.write_object(gix::objs::TreeRef::empty())?; | 262 | let oid = repo.write_object(gix::objs::TreeRef::empty())?; |
| 263 | assert_eq!( | 263 | assert_eq!( |
| 264 | oid, | 264 | oid, |
| 265 | gix::hash::ObjectId::empty_tree(repo.object_hash()), | 265 | repo.object_hash().empty_tree(), |
| 266 | "it produces a well-known empty tree id" | 266 | "it produces a well-known empty tree id" |
| 267 | ); | 267 | ); |
| 268 | Ok(()) | 268 | Ok(()) |
| @@ -277,7 +277,7 @@ mod write_object { · 8 unmodified lines | |||
| 277 | time: Default::default(), | 277 | time: Default::default(), |
| 278 | }; | 278 | }; |
| 279 | let commit = gix::objs::Commit { | 279 | let commit = gix::objs::Commit { |
| 280 | tree: gix::hash::ObjectId::empty_tree(repo.object_hash()), | 280 | tree: repo.object_hash().empty_tree(), |
| 281 | author: actor.clone(), | 281 | author: actor.clone(), |
| 282 | committer: actor, | 282 | committer: actor, |
| 283 | parents: Default::default(), | 283 | parents: Default::default(), |
| @@ -292,6 +292,21 @@ mod write_object { · 8 unmodified lines | |||
| 292 | ); | 292 | ); |
| 293 | Ok(()) | 293 | Ok(()) |
| 294 | } | 294 | } |
| 295 | |||
| 296 | #[test] | ||
| 297 | fn blob_write_to_implementation() -> crate::Result { | ||
| 298 | let repo = empty_bare_in_memory_repo()?; | ||
| 299 | let blob = repo.empty_blob(); | ||
| 300 | |||
| 301 | // Create a blob directly to test our WriteTo implementation | ||
| 302 | let actual_id = repo.write_object(&blob)?; | ||
| 303 | let actual_blob = repo.find_object(actual_id)?.into_blob(); | ||
| 304 | assert_eq!(actual_id, repo.object_hash().empty_blob()); | ||
| 305 | |||
| 306 | assert_eq!(actual_blob.data, blob.data); | ||
| 307 | |||
| 308 | Ok(()) | ||
| 309 | } | ||
| 295 | } | 310 | } |
| 296 | 311 | ||
| 297 | mod write_blob { | 312 | mod write_blob { |