Skip to content

Commit 88f2e6c

Browse files
committed
feat: Add detach() and detached() too all object types.
That way, the detachment API is symmetric. It's required to overcome the `Drop` implementation of each of these types which prevents moving data out of the object (easily).
1 parent db0b851 commit 88f2e6c

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

gix/src/object/blob.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::{Blob, ObjectDetached};
2+
13
///
24
#[cfg(feature = "blob-diff")]
35
pub mod diff {
@@ -147,3 +149,20 @@ pub mod diff {
147149
}
148150
}
149151
}
152+
153+
/// Remove Lifetime
154+
impl Blob<'_> {
155+
/// Create an owned instance of this object, copying our data in the process.
156+
pub fn detached(&self) -> ObjectDetached {
157+
ObjectDetached {
158+
id: self.id,
159+
kind: gix_object::Kind::Blob,
160+
data: self.data.clone(),
161+
}
162+
}
163+
164+
/// Sever the connection to the `Repository` and turn this instance into a standalone object.
165+
pub fn detach(self) -> ObjectDetached {
166+
self.into()
167+
}
168+
}

gix/src/object/commit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod error {
2020

2121
pub use error::Error;
2222

23+
/// Remove Lifetime
2324
impl<'repo> Commit<'repo> {
2425
/// Create an owned instance of this object, copying our data in the process.
2526
pub fn detached(&self) -> ObjectDetached {

gix/src/object/impls.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ impl<'repo> From<Object<'repo>> for ObjectDetached {
77
ObjectDetached {
88
id: v.id,
99
kind: v.kind,
10-
data: std::mem::take(&mut v.data),
10+
data: steal_from_freelist(&mut v.data),
1111
}
1212
}
1313
}
@@ -17,7 +17,7 @@ impl<'repo> From<Commit<'repo>> for ObjectDetached {
1717
ObjectDetached {
1818
id: v.id,
1919
kind: gix_object::Kind::Commit,
20-
data: std::mem::take(&mut v.data),
20+
data: steal_from_freelist(&mut v.data),
2121
}
2222
}
2323
}
@@ -27,7 +27,27 @@ impl<'repo> From<Tag<'repo>> for ObjectDetached {
2727
ObjectDetached {
2828
id: v.id,
2929
kind: gix_object::Kind::Tag,
30-
data: std::mem::take(&mut v.data),
30+
data: steal_from_freelist(&mut v.data),
31+
}
32+
}
33+
}
34+
35+
impl<'repo> From<Blob<'repo>> for ObjectDetached {
36+
fn from(mut v: Blob<'repo>) -> Self {
37+
ObjectDetached {
38+
id: v.id,
39+
kind: gix_object::Kind::Blob,
40+
data: steal_from_freelist(&mut v.data),
41+
}
42+
}
43+
}
44+
45+
impl<'repo> From<Tree<'repo>> for ObjectDetached {
46+
fn from(mut v: Tree<'repo>) -> Self {
47+
ObjectDetached {
48+
id: v.id,
49+
kind: gix_object::Kind::Tree,
50+
data: steal_from_freelist(&mut v.data),
3151
}
3252
}
3353
}

gix/src/object/tag.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ext::ObjectIdExt, Tag};
1+
use crate::{ext::ObjectIdExt, ObjectDetached, Tag};
22

33
impl<'repo> Tag<'repo> {
44
/// Decode the entire tag object and return it for accessing all tag information.
@@ -24,3 +24,20 @@ impl<'repo> Tag<'repo> {
2424
gix_object::TagRefIter::from_bytes(&self.data).tagger()
2525
}
2626
}
27+
28+
/// Remove Lifetime
29+
impl Tag<'_> {
30+
/// Create an owned instance of this object, copying our data in the process.
31+
pub fn detached(&self) -> ObjectDetached {
32+
ObjectDetached {
33+
id: self.id,
34+
kind: gix_object::Kind::Tag,
35+
data: self.data.clone(),
36+
}
37+
}
38+
39+
/// Sever the connection to the `Repository` and turn this instance into a standalone object.
40+
pub fn detach(self) -> ObjectDetached {
41+
self.into()
42+
}
43+
}

gix/src/object/tree/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use gix_object::tree::EntryMode;
44
use gix_object::{bstr::BStr, TreeRefIter};
55
use gix_odb::FindExt;
66

7-
use crate::{object::find, Id, Tree};
7+
use crate::{object::find, Id, ObjectDetached, Tree};
88

99
/// Initialization
1010
impl<'repo> Tree<'repo> {
@@ -250,3 +250,20 @@ mod _impls {
250250
}
251251
}
252252
}
253+
254+
/// Remove Lifetime
255+
impl Tree<'_> {
256+
/// Create an owned instance of this object, copying our data in the process.
257+
pub fn detached(&self) -> ObjectDetached {
258+
ObjectDetached {
259+
id: self.id,
260+
kind: gix_object::Kind::Tree,
261+
data: self.data.clone(),
262+
}
263+
}
264+
265+
/// Sever the connection to the `Repository` and turn this instance into a standalone object.
266+
pub fn detach(self) -> ObjectDetached {
267+
self.into()
268+
}
269+
}

0 commit comments

Comments
 (0)