History

feat: `CommitRefIter::parent_ids()` (#364)

Allocation-free parent retrieval.

Sebastian Thiel committed 4 years ago

Commit 91065cd1 parent 9c8e4493 files · +34 −3Browse files

Showing the changes to git-object/src/commit (1 of 3 files). Show all

Unified · Split

git-object/src/commit/ref_iter.rs+12−1
@@ -17,6 +17,7 @@ pub(crate) enum SignatureKind { · 16 unmodified lines
1717 Committer,
1818 }
1919
20+#[derive(Copy, Clone)]
2021 pub(crate) enum State {
2122 Tree,
2223 Parents,
@@ -52,12 +53,22 @@ impl<'a> CommitRefIter<'a> { · 29 unmodified lines
5253 self.next().and_then(Result::ok).and_then(Token::try_into_id)
5354 }
5455
56+ /// Return all parent_ids as iterator.
57+ ///
58+ /// Parsing errors are ignored quietly.
59+ pub fn parent_ids(self) -> impl Iterator<Item = git_hash::ObjectId> + 'a {
60+ self.filter_map(|t| match t {
61+ Ok(Token::Parent { id }) => Some(id),
62+ _ => None,
63+ })
64+ }
65+
5566 /// Returns all signatures, first the author, then the committer, if there is no decoding error.
5667 ///
5768 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not
5869 /// if not exactly two signatures were iterable.
5970 /// Errors are not the common case - if an error needs to be detectable, use this instance as iterator.
60− pub fn signatures(&'a mut self) -> impl Iterator<Item = git_actor::SignatureRef<'_>> + 'a {
71+ pub fn signatures(self) -> impl Iterator<Item = git_actor::SignatureRef<'a>> + 'a {
6172 self.filter_map(|t| match t {
6273 Ok(Token::Author { signature }) | Ok(Token::Committer { signature }) => Some(signature),
6374 _ => None,