| @@ -1,20 +1,22 @@ |
| 1 | use std::{cell::Ref, ops::DerefMut}; | 1 | use std::{borrow::Borrow, cell::Ref, ops::DerefMut}; |
| 2 | | 2 | |
| 3 | pub use git_object::Kind; | 3 | pub use git_object::Kind; |
| 4 | | 4 | |
| 5 | use crate::objs::immutable; | | |
| 6 | use crate::odb::Find; | | |
| 7 | use crate::{ | 5 | use crate::{ |
| 8 | hash::{oid, ObjectId}, | 6 | hash::{oid, ObjectId}, |
| 9 | object, odb, | 7 | object, |
| 10 | odb::FindExt, | 8 | objs::immutable, |
| 11 | Access, ObjectRef, Oid, | 9 | odb, |
| | 10 | odb::{Find, FindExt}, |
| | 11 | Access, Object, ObjectRef, Oid, |
| 12 | }; | 12 | }; |
| 13 | use std::borrow::Borrow; | | |
| 14 | | 13 | |
| 15 | mod impls { | 14 | mod oid_impls { |
| 16 | use super::Oid; | 15 | use super::Oid; |
| 17 | use crate::hash::{oid, ObjectId}; | 16 | use crate::{ |
| | 17 | hash::{oid, ObjectId}, |
| | 18 | Object, ObjectRef, |
| | 19 | }; |
| 18 | | 20 | |
| 19 | impl<'repo, A, B> PartialEq<Oid<'repo, A>> for Oid<'repo, B> { | 21 | impl<'repo, A, B> PartialEq<Oid<'repo, A>> for Oid<'repo, B> { |
| 20 | fn eq(&self, other: &Oid<'repo, A>) -> bool { | 22 | fn eq(&self, other: &Oid<'repo, A>) -> bool { |
| @@ -28,6 +30,24 @@ mod impls { · 7 unmodified lines |
| 28 | } | 30 | } |
| 29 | } | 31 | } |
| 30 | | 32 | |
| | 33 | impl<'repo, A> PartialEq<oid> for Oid<'repo, A> { |
| | 34 | fn eq(&self, other: &oid) -> bool { |
| | 35 | self.id == other |
| | 36 | } |
| | 37 | } |
| | 38 | |
| | 39 | impl<'repo, A, B> PartialEq<ObjectRef<'repo, A>> for Oid<'repo, B> { |
| | 40 | fn eq(&self, other: &ObjectRef<'repo, A>) -> bool { |
| | 41 | self.id == other.id |
| | 42 | } |
| | 43 | } |
| | 44 | |
| | 45 | impl<'repo, A> PartialEq<Object> for Oid<'repo, A> { |
| | 46 | fn eq(&self, other: &Object) -> bool { |
| | 47 | self.id == other.id |
| | 48 | } |
| | 49 | } |
| | 50 | |
| 31 | impl<'repo, A> std::fmt::Debug for Oid<'repo, A> { | 51 | impl<'repo, A> std::fmt::Debug for Oid<'repo, A> { |
| 32 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 52 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 33 | self.id.fmt(f) | 53 | self.id.fmt(f) |
| @@ -47,6 +67,21 @@ mod impls { · 13 unmodified lines |
| 47 | } | 67 | } |
| 48 | } | 68 | } |
| 49 | | 69 | |
| | 70 | impl Object { |
| | 71 | pub fn attach<A>(self, access: &A) -> ObjectRef<'_, A> |
| | 72 | where |
| | 73 | A: Access + Sized, |
| | 74 | { |
| | 75 | *access.cache().buf.borrow_mut() = self.data; |
| | 76 | ObjectRef { |
| | 77 | id: self.id, |
| | 78 | kind: self.kind, |
| | 79 | data: Ref::map(access.cache().buf.borrow(), |v| v.as_slice()), |
| | 80 | access, |
| | 81 | } |
| | 82 | } |
| | 83 | } |
| | 84 | |
| 50 | impl<'repo, A> ObjectRef<'repo, A> | 85 | impl<'repo, A> ObjectRef<'repo, A> |
| 51 | where | 86 | where |
| 52 | A: Access + Sized, | 87 | A: Access + Sized, |
| @@ -72,6 +107,50 @@ pub mod find { · 19 unmodified lines |
| 72 | } | 107 | } |
| 73 | } | 108 | } |
| 74 | | 109 | |
| | 110 | mod object_ref_impls { |
| | 111 | use crate::{Object, ObjectRef}; |
| | 112 | |
| | 113 | impl<'repo, A> From<ObjectRef<'repo, A>> for Object { |
| | 114 | fn from(r: ObjectRef<'repo, A>) -> Self { |
| | 115 | r.into_owned() |
| | 116 | } |
| | 117 | } |
| | 118 | |
| | 119 | impl<'repo, A> AsRef<[u8]> for ObjectRef<'repo, A> { |
| | 120 | fn as_ref(&self) -> &[u8] { |
| | 121 | &self.data |
| | 122 | } |
| | 123 | } |
| | 124 | |
| | 125 | impl AsRef<[u8]> for Object { |
| | 126 | fn as_ref(&self) -> &[u8] { |
| | 127 | &self.data |
| | 128 | } |
| | 129 | } |
| | 130 | } |
| | 131 | |
| | 132 | impl<'repo, A> ObjectRef<'repo, A> { |
| | 133 | pub fn to_owned(&self) -> Object { |
| | 134 | Object { |
| | 135 | id: self.id, |
| | 136 | kind: self.kind, |
| | 137 | data: self.data.to_owned(), |
| | 138 | } |
| | 139 | } |
| | 140 | |
| | 141 | pub fn into_owned(self) -> Object { |
| | 142 | Object { |
| | 143 | id: self.id, |
| | 144 | kind: self.kind, |
| | 145 | data: self.data.to_owned(), |
| | 146 | } |
| | 147 | } |
| | 148 | |
| | 149 | pub fn detach(self) -> Object { |
| | 150 | self.into() |
| | 151 | } |
| | 152 | } |
| | 153 | |
| 75 | impl<'repo, A> ObjectRef<'repo, A> | 154 | impl<'repo, A> ObjectRef<'repo, A> |
| 76 | where | 155 | where |
| 77 | A: Access + Sized, | 156 | A: Access + Sized, |
| @@ -86,6 +165,11 @@ where · 8 unmodified lines |
| 86 | } | 165 | } |
| 87 | | 166 | |
| 88 | pub mod peel_to_kind { | 167 | pub mod peel_to_kind { |
| | 168 | use crate::{ |
| | 169 | object::{peel_to_kind, Kind}, |
| | 170 | objs::immutable, |
| | 171 | odb, Access, ObjectRef, |
| | 172 | }; |
| 89 | | 173 | |
| 90 | impl<'repo, A> ObjectRef<'repo, A> | 174 | impl<'repo, A> ObjectRef<'repo, A> |
| 91 | where | 175 | where |
| @@ -140,9 +224,6 @@ pub mod peel_to_kind { · 48 unmodified lines |
| 140 | } | 224 | } |
| 141 | } | 225 | } |
| 142 | } | 226 | } |
| 143 | use crate::object::{peel_to_kind, Kind}; | | |
| 144 | use crate::objs::immutable; | | |
| 145 | use crate::{odb, Access, ObjectRef}; | | |
| 146 | pub use error::Error; | 227 | pub use error::Error; |
| 147 | } | 228 | } |
| 148 | | 229 | |