From f1efa660cafb5f63cfda396344ceb1f564af229a Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Sun, 13 Jun 2021 16:57:16 -0700 Subject: [PATCH] libgit2-rs-safe: Update for 2nd edition. --- libgit2-rs-safe/Cargo.toml | 4 ++-- libgit2-rs-safe/src/git/mod.rs | 37 +++++++++++++++------------------- libgit2-rs-safe/src/main.rs | 7 ++++--- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/libgit2-rs-safe/Cargo.toml b/libgit2-rs-safe/Cargo.toml index 98ddf5c..00aee25 100644 --- a/libgit2-rs-safe/Cargo.toml +++ b/libgit2-rs-safe/Cargo.toml @@ -2,7 +2,7 @@ name = "git-toy" version = "0.1.0" authors = ["You "] -build = "build.rs" +edition = "2018" [dependencies] -libc = "0.2.23" +libc = "0.2" diff --git a/libgit2-rs-safe/src/git/mod.rs b/libgit2-rs-safe/src/git/mod.rs index 8a657b6..fdade8f 100644 --- a/libgit2-rs-safe/src/git/mod.rs +++ b/libgit2-rs-safe/src/git/mod.rs @@ -18,9 +18,7 @@ impl fmt::Display for Error { } } -impl error::Error for Error { - fn description(&self) -> &str { &self.message } -} +impl error::Error for Error { } pub type Result = result::Result; @@ -51,19 +49,20 @@ fn check(code: c_int) -> Result { /// A Git repository. pub struct Repository { - // This must always be a pointer to a live `git_repository` structure, + // This must always be a pointer to a live `git_repository` structure. // No other `Repository` may point to it. raw: *mut raw::git_repository } use std::path::Path; +use std::ptr; impl Repository { pub fn open>(path: P) -> Result { ensure_initialized(); let path = path_to_cstring(path.as_ref())?; - let mut repo = null_mut(); + let mut repo = ptr::null_mut(); unsafe { check(raw::git_repository_open(&mut repo, path.as_ptr()))?; } @@ -71,9 +70,6 @@ impl Repository { } } -use std; -use libc; - fn ensure_initialized() { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| { @@ -85,14 +81,10 @@ fn ensure_initialized() { }); } -use std::io::Write; - extern fn shutdown() { unsafe { if let Err(e) = check(raw::git_libgit2_shutdown()) { - let _ = writeln!(std::io::stderr(), - "shutting down libgit2 failed: {}", - e); + eprintln!("shutting down libgit2 failed: {}", e); std::process::abort(); } } @@ -151,16 +143,20 @@ pub struct Oid { pub raw: raw::git_oid } -use std::mem::uninitialized; +use std::mem; use std::os::raw::c_char; impl Repository { pub fn reference_name_to_id(&self, name: &str) -> Result { let name = CString::new(name)?; unsafe { - let mut oid = uninitialized(); - check(raw::git_reference_name_to_id(&mut oid, self.raw, - name.as_ptr() as *const c_char))?; + let oid = { + let mut oid = mem::MaybeUninit::uninit(); + check(raw::git_reference_name_to_id( + oid.as_mut_ptr(), self.raw, + name.as_ptr() as *const c_char))?; + oid.assume_init() + }; Ok(Oid { raw: oid }) } } @@ -174,11 +170,9 @@ pub struct Commit<'repo> { _marker: PhantomData<&'repo Repository> } -use std::ptr::null_mut; - impl Repository { pub fn find_commit(&self, oid: &Oid) -> Result { - let mut commit = null_mut(); + let mut commit = ptr::null_mut(); unsafe { check(raw::git_commit_lookup(&mut commit, self.raw, &oid.raw))?; } @@ -240,7 +234,8 @@ impl<'text> Signature<'text> { /// borrowed from `_owner`. /// /// Safety: if `ptr` is non-null, it must point to a null-terminated C -/// string that is safe to access. +/// string that is safe to access for at least as long as the lifetime of +/// `_owner`. unsafe fn char_ptr_to_str(_owner: &T, ptr: *const c_char) -> Option<&str> { if ptr.is_null() { return None; diff --git a/libgit2-rs-safe/src/main.rs b/libgit2-rs-safe/src/main.rs index df5d1af..091dd0d 100644 --- a/libgit2-rs-safe/src/main.rs +++ b/libgit2-rs-safe/src/main.rs @@ -1,10 +1,11 @@ -mod git; +#![warn(rust_2018_idioms)] +#![allow(elided_lifetimes_in_paths)] -extern crate libc; +mod git; fn main() { let path = std::env::args_os().skip(1).next() - .expect("usage: libgit2-rs PATH"); + .expect("usage: git-toy PATH"); let repo = git::Repository::open(&path) .expect("opening repository");