Skip to content

Commit

Permalink
feat: Add detach() and detached() too all object types.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Byron committed Oct 12, 2023
1 parent db0b851 commit 88f2e6c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
19 changes: 19 additions & 0 deletions gix/src/object/blob.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::{Blob, ObjectDetached};

///
#[cfg(feature = "blob-diff")]
pub mod diff {
Expand Down Expand Up @@ -147,3 +149,20 @@ pub mod diff {
}
}
}

/// Remove Lifetime
impl Blob<'_> {
/// Create an owned instance of this object, copying our data in the process.
pub fn detached(&self) -> ObjectDetached {
ObjectDetached {
id: self.id,
kind: gix_object::Kind::Blob,
data: self.data.clone(),
}
}

/// Sever the connection to the `Repository` and turn this instance into a standalone object.
pub fn detach(self) -> ObjectDetached {
self.into()
}
}
1 change: 1 addition & 0 deletions gix/src/object/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod error {

pub use error::Error;

/// Remove Lifetime
impl<'repo> Commit<'repo> {
/// Create an owned instance of this object, copying our data in the process.
pub fn detached(&self) -> ObjectDetached {
Expand Down
26 changes: 23 additions & 3 deletions gix/src/object/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl<'repo> From<Object<'repo>> for ObjectDetached {
ObjectDetached {
id: v.id,
kind: v.kind,
data: std::mem::take(&mut v.data),
data: steal_from_freelist(&mut v.data),
}
}
}
Expand All @@ -17,7 +17,7 @@ impl<'repo> From<Commit<'repo>> for ObjectDetached {
ObjectDetached {
id: v.id,
kind: gix_object::Kind::Commit,
data: std::mem::take(&mut v.data),
data: steal_from_freelist(&mut v.data),
}
}
}
Expand All @@ -27,7 +27,27 @@ impl<'repo> From<Tag<'repo>> for ObjectDetached {
ObjectDetached {
id: v.id,
kind: gix_object::Kind::Tag,
data: std::mem::take(&mut v.data),
data: steal_from_freelist(&mut v.data),
}
}
}

impl<'repo> From<Blob<'repo>> for ObjectDetached {
fn from(mut v: Blob<'repo>) -> Self {
ObjectDetached {
id: v.id,
kind: gix_object::Kind::Blob,
data: steal_from_freelist(&mut v.data),
}
}
}

impl<'repo> From<Tree<'repo>> for ObjectDetached {
fn from(mut v: Tree<'repo>) -> Self {
ObjectDetached {
id: v.id,
kind: gix_object::Kind::Tree,
data: steal_from_freelist(&mut v.data),
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion gix/src/object/tag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ext::ObjectIdExt, Tag};
use crate::{ext::ObjectIdExt, ObjectDetached, Tag};

impl<'repo> Tag<'repo> {
/// Decode the entire tag object and return it for accessing all tag information.
Expand All @@ -24,3 +24,20 @@ impl<'repo> Tag<'repo> {
gix_object::TagRefIter::from_bytes(&self.data).tagger()
}
}

/// Remove Lifetime
impl Tag<'_> {
/// Create an owned instance of this object, copying our data in the process.
pub fn detached(&self) -> ObjectDetached {
ObjectDetached {
id: self.id,
kind: gix_object::Kind::Tag,
data: self.data.clone(),
}
}

/// Sever the connection to the `Repository` and turn this instance into a standalone object.
pub fn detach(self) -> ObjectDetached {
self.into()
}
}
19 changes: 18 additions & 1 deletion gix/src/object/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use gix_object::tree::EntryMode;
use gix_object::{bstr::BStr, TreeRefIter};
use gix_odb::FindExt;

use crate::{object::find, Id, Tree};
use crate::{object::find, Id, ObjectDetached, Tree};

/// Initialization
impl<'repo> Tree<'repo> {
Expand Down Expand Up @@ -250,3 +250,20 @@ mod _impls {
}
}
}

/// Remove Lifetime
impl Tree<'_> {
/// Create an owned instance of this object, copying our data in the process.
pub fn detached(&self) -> ObjectDetached {
ObjectDetached {
id: self.id,
kind: gix_object::Kind::Tree,
data: self.data.clone(),
}
}

/// Sever the connection to the `Repository` and turn this instance into a standalone object.
pub fn detach(self) -> ObjectDetached {
self.into()
}
}

0 comments on commit 88f2e6c

Please sign in to comment.