rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.65k stars 381 forks source link

Unnecessary lifetimes #945

Open edrevo opened 1 year ago

edrevo commented 1 year ago

Both ODB and Repository are ref-counted objects (see https://github.com/libgit2/libgit2/blob/2f20fe8869d7a1df7c9b7a9e2939c1a20533c6dc/src/libgit2/odb.c#L930 for example) so calling free doesn't necessarily mean that the underlying object is being freed.

There are several methods in git2-rs that return an object with a lifetime that is bound to either the original Repo or Odb, but there's nothing wrong with freeing the corresponding Repo or Odb. For example, this git2go code runs fine:

w, err := odb.NewWritePack(nil)
odb.Free() // Explicitly free the odb object
repo.Free() // Explicitly free the repo object
w.Write(response.PackfileData) // works fine
err = w.Commit() // works fine

Having these lifetimes makes git2-rs less ergonomic and they don't provide any extra safety. I guess removing them would be a breaking change to the git2-rs API, but maybe they could be replaced by 'static.