Skip to content

Commit 527fd3d

Browse files
committed
feat: add CommitRef|TagRef|BlobRef::into_owned() as shortcut.
Otherwise one would have to know it converts into `Commit` via `From`.
1 parent 503098d commit 527fd3d

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

gix-object/src/blob.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ impl BlobRef<'_> {
4444
pub fn from_bytes(data: &[u8]) -> Result<BlobRef<'_>, Infallible> {
4545
Ok(BlobRef { data })
4646
}
47+
48+
/// Clone the data in this instance by allocating a new vector for a fully owned blob.
49+
pub fn into_owned(self) -> Blob {
50+
self.into()
51+
}
4752
}

gix-object/src/commit/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ impl<'a> CommitRef<'a> {
113113
}
114114
}
115115

116+
/// Conversion
117+
impl CommitRef<'_> {
118+
/// Copy all fields of this instance into a fully owned commit, consuming this instance.
119+
pub fn into_owned(self) -> Commit {
120+
self.into()
121+
}
122+
123+
/// Copy all fields of this instance into a fully owned commit, internally cloning this instance.
124+
pub fn to_owned(self) -> Commit {
125+
self.clone().into()
126+
}
127+
}
128+
116129
impl Commit {
117130
/// Returns a convenient iterator over all extra headers.
118131
pub fn extra_headers(&self) -> ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {

gix-object/src/lib.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ pub enum Kind {
5858
Commit,
5959
Tag,
6060
}
61-
/// A chunk of any [`data`][BlobRef::data].
61+
/// A chunk of any [`data`](BlobRef::data).
6262
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
6363
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6464
pub struct BlobRef<'a> {
6565
/// The bytes themselves.
6666
pub data: &'a [u8],
6767
}
6868

69-
/// A mutable chunk of any [`data`][Blob::data].
69+
/// A mutable chunk of any [`data`](Blob::data).
7070
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
7171
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7272
pub struct Blob {
7373
/// The data itself.
7474
pub data: Vec<u8>,
7575
}
7676

77-
/// A git commit parsed using [`from_bytes()`][CommitRef::from_bytes()].
77+
/// A git commit parsed using [`from_bytes()`](CommitRef::from_bytes()).
7878
///
7979
/// A commit encapsulates information about a point in time at which the state of the repository is recorded, usually after a
8080
/// change which is documented in the commit `message`.
@@ -83,18 +83,18 @@ pub struct Blob {
8383
pub struct CommitRef<'a> {
8484
/// HEX hash of tree object we point to. Usually 40 bytes long.
8585
///
86-
/// Use [`tree()`][CommitRef::tree()] to obtain a decoded version of it.
86+
/// Use [`tree()`](CommitRef::tree()) to obtain a decoded version of it.
8787
#[cfg_attr(feature = "serde", serde(borrow))]
8888
pub tree: &'a BStr,
8989
/// HEX hash of each parent commit. Empty for first commit in repository.
9090
pub parents: SmallVec<[&'a BStr; 1]>,
9191
/// Who wrote this commit. Name and email might contain whitespace and are not trimmed to ensure round-tripping.
9292
///
93-
/// Use the [`author()`][CommitRef::author()] method to received a trimmed version of it.
93+
/// Use the [`author()`](CommitRef::author()) method to received a trimmed version of it.
9494
pub author: gix_actor::SignatureRef<'a>,
9595
/// Who committed this commit. Name and email might contain whitespace and are not trimmed to ensure round-tripping.
9696
///
97-
/// Use the [`committer()`][CommitRef::committer()] method to received a trimmed version of it.
97+
/// Use the [`committer()`](CommitRef::committer()) method to received a trimmed version of it.
9898
///
9999
/// This may be different from the `author` in case the author couldn't write to the repository themselves and
100100
/// is commonly encountered with contributed commits.
@@ -103,7 +103,7 @@ pub struct CommitRef<'a> {
103103
pub encoding: Option<&'a BStr>,
104104
/// The commit message documenting the change.
105105
pub message: &'a BStr,
106-
/// Extra header fields, in order of them being encountered, made accessible with the iterator returned by [`extra_headers()`][CommitRef::extra_headers()].
106+
/// Extra header fields, in order of them being encountered, made accessible with the iterator returned by [`extra_headers()`](CommitRef::extra_headers()).
107107
pub extra_headers: Vec<(&'a BStr, Cow<'a, BStr>)>,
108108
}
109109

@@ -135,15 +135,15 @@ pub struct Commit {
135135
/// The commit message documenting the change.
136136
pub message: BString,
137137
/// Extra header fields, in order of them being encountered, made accessible with the iterator returned
138-
/// by [`extra_headers()`][Commit::extra_headers()].
138+
/// by [`extra_headers()`](Commit::extra_headers()).
139139
pub extra_headers: Vec<(BString, BString)>,
140140
}
141141

142142
/// Represents a git tag, commonly indicating a software release.
143-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
143+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
144144
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145145
pub struct TagRef<'a> {
146-
/// The hash in hexadecimal being the object this tag points to. Use [`target()`][TagRef::target()] to obtain a byte representation.
146+
/// The hash in hexadecimal being the object this tag points to. Use [`target()`](TagRef::target()) to obtain a byte representation.
147147
#[cfg_attr(feature = "serde", serde(borrow))]
148148
pub target: &'a BStr,
149149
/// The kind of object that `target` points to.
@@ -184,13 +184,13 @@ pub struct Tag {
184184
pub pgp_signature: Option<BString>,
185185
}
186186

187-
/// Immutable objects are read-only structures referencing most data from [a byte slice][crate::ObjectRef::from_bytes()].
187+
/// Immutable objects are read-only structures referencing most data from [a byte slice](ObjectRef::from_bytes()).
188188
///
189189
/// Immutable objects are expected to be deserialized from bytes that acts as backing store, and they
190-
/// cannot be mutated or serialized. Instead, one will [convert][crate::ObjectRef::into_owned()] them into their [`mutable`][Object] counterparts
190+
/// cannot be mutated or serialized. Instead, one will [convert](ObjectRef::into_owned()) them into their [`mutable`](Object) counterparts
191191
/// which support mutation and serialization.
192192
///
193-
/// An `ObjectRef` is representing [`Trees`][TreeRef], [`Blobs`][BlobRef], [`Commits`][CommitRef], or [`Tags`][TagRef].
193+
/// An `ObjectRef` is representing [`Trees`], [`Blobs`], [`Commits`], or [`Tags`].
194194
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
195195
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
196196
#[allow(missing_docs)]
@@ -206,10 +206,10 @@ pub enum ObjectRef<'a> {
206206
///
207207
/// Mutable objects are Commits, Trees, Blobs and Tags that can be changed and serialized.
208208
///
209-
/// They either created using object [construction][Object] or by [deserializing existing objects][ObjectRef::from_bytes()]
210-
/// and converting these [into mutable copies][ObjectRef::into_owned()] for adjustments.
209+
/// They either created using object [construction](Object) or by [deserializing existing objects](ObjectRef::from_bytes())
210+
/// and converting these [into mutable copies](ObjectRef::into_owned()) for adjustments.
211211
///
212-
/// An `Object` is representing [`Trees`][Tree], [`Blobs`][Blob], [`Commits`][Commit] or [`Tags`][Tag].
212+
/// An `Object` is representing [`Trees`], [`Blobs`], [`Commits`] or [`Tags`].
213213
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
214214
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
215215
#[allow(clippy::large_enum_variant, missing_docs)]

gix-object/src/tag/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ impl<'a> TagRef<'a> {
2323
pub fn target(&self) -> gix_hash::ObjectId {
2424
gix_hash::ObjectId::from_hex(self.target).expect("prior validation")
2525
}
26+
27+
/// Copy all data into a fully-owned instance.
28+
pub fn into_owned(self) -> crate::Tag {
29+
self.into()
30+
}
2631
}

gix-object/tests/object/tag/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use gix_object::{bstr::ByteSlice, Kind, TagRef, TagRefIter};
44
use crate::fixture_name;
55

66
mod method {
7+
use bstr::ByteSlice;
78
use gix_object::TagRef;
89
use pretty_assertions::assert_eq;
910

@@ -15,6 +16,21 @@ mod method {
1516
let tag = TagRef::from_bytes(&fixture)?;
1617
assert_eq!(tag.target(), hex_to_id("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec"));
1718
assert_eq!(tag.target, "ffa700b4aca13b80cb6b98a078e7c96804f8e0ec".as_bytes());
19+
20+
let gix_object::Tag {
21+
target,
22+
target_kind,
23+
name,
24+
tagger,
25+
message,
26+
pgp_signature,
27+
} = tag.into_owned();
28+
assert_eq!(target.to_string(), tag.target);
29+
assert_eq!(target_kind, tag.target_kind);
30+
assert_eq!(name, tag.name);
31+
assert_eq!(tagger.as_ref().map(|s| s.to_ref()), tag.tagger);
32+
assert_eq!(message, tag.message);
33+
assert_eq!(pgp_signature.as_ref().map(|s| s.as_bstr()), tag.pgp_signature);
1834
Ok(())
1935
}
2036
}

0 commit comments

Comments
 (0)