Skip to content

gix library returning too short of a lifetime #2103

@jalil-salame

Description

@jalil-salame

Current behavior 😯

Trying to filter tags in a separate function fails currently as the lifetime is too short:

fn latest_tag(repo: &gix::Repository) -> gix::Reference {
    let refs = repo.references().unwrap();
    refs.tags() // .tags() borrows from `refs` and thus this fails to compile
        .unwrap()
        .filter_map(|tag| tag.ok())
        .map(|tag| (tag, semver::Version::parse(tag.name().shorten().to_str().unwrap())))
        .max_by_key(|(_tag, version)| version.clone())
        .map(|(tag, _version)| tag)
}

Expected behavior 🤔

.tags() should instead borrow from repo and successfully compile.

I believe the issue is here:

pub fn tags(&self) -> Result<Iter<'_>, init::Error> {
Ok(Iter::new(self.repo, self.platform.prefixed(b"refs/tags/".try_into()?)?))
}

tags should return Result<Iter<'repo>, init::Error> instead as otherwise it borrows from &self.

If this is desirable, I would be willing to contribute the fix (for tags and also the surrounding functions that look to have the same issue).

Git behavior

Not applicable

Steps to reproduce 🕹

Try to compile the following code:

# Cargo.toml
[package]
name = "gix-describe"
version = "0.1.0"
edition = "2024"

[dependencies]
gix = { version = "0.73.0", default-features = false, features = ["max-performance", "basic", "status"] }
semver = "1.0.26"
// src/main.rs
fn latest_tag(repo: &gix::Repository) -> gix::Reference {
    let refs = repo.references().unwrap();
    refs.tags() // .tags() borrows from `refs` and thus this fails to compile
        .unwrap()
        .filter_map(|tag| tag.ok())
        .map(|tag| (tag, semver::Version::parse(tag.name().shorten().to_str().unwrap())))
        .max_by_key(|(_tag, version)| version.clone())
        .map(|(tag, _version)| tag)
}

fn main() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgedan issue is accepted as shortcoming to be fixedhelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions