Skip to content

Commit f1efa66

Browse files
committed
libgit2-rs-safe: Update for 2nd edition.
1 parent cd2e8fd commit f1efa66

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

libgit2-rs-safe/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "git-toy"
33
version = "0.1.0"
44
authors = ["You <[email protected]>"]
5-
build = "build.rs"
5+
edition = "2018"
66

77
[dependencies]
8-
libc = "0.2.23"
8+
libc = "0.2"

libgit2-rs-safe/src/git/mod.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ impl fmt::Display for Error {
1818
}
1919
}
2020

21-
impl error::Error for Error {
22-
fn description(&self) -> &str { &self.message }
23-
}
21+
impl error::Error for Error { }
2422

2523
pub type Result<T> = result::Result<T, Error>;
2624

@@ -51,29 +49,27 @@ fn check(code: c_int) -> Result<c_int> {
5149

5250
/// A Git repository.
5351
pub struct Repository {
54-
// This must always be a pointer to a live `git_repository` structure,
52+
// This must always be a pointer to a live `git_repository` structure.
5553
// No other `Repository` may point to it.
5654
raw: *mut raw::git_repository
5755
}
5856

5957
use std::path::Path;
58+
use std::ptr;
6059

6160
impl Repository {
6261
pub fn open<P: AsRef<Path>>(path: P) -> Result<Repository> {
6362
ensure_initialized();
6463

6564
let path = path_to_cstring(path.as_ref())?;
66-
let mut repo = null_mut();
65+
let mut repo = ptr::null_mut();
6766
unsafe {
6867
check(raw::git_repository_open(&mut repo, path.as_ptr()))?;
6968
}
7069
Ok(Repository { raw: repo })
7170
}
7271
}
7372

74-
use std;
75-
use libc;
76-
7773
fn ensure_initialized() {
7874
static ONCE: std::sync::Once = std::sync::Once::new();
7975
ONCE.call_once(|| {
@@ -85,14 +81,10 @@ fn ensure_initialized() {
8581
});
8682
}
8783

88-
use std::io::Write;
89-
9084
extern fn shutdown() {
9185
unsafe {
9286
if let Err(e) = check(raw::git_libgit2_shutdown()) {
93-
let _ = writeln!(std::io::stderr(),
94-
"shutting down libgit2 failed: {}",
95-
e);
87+
eprintln!("shutting down libgit2 failed: {}", e);
9688
std::process::abort();
9789
}
9890
}
@@ -151,16 +143,20 @@ pub struct Oid {
151143
pub raw: raw::git_oid
152144
}
153145

154-
use std::mem::uninitialized;
146+
use std::mem;
155147
use std::os::raw::c_char;
156148

157149
impl Repository {
158150
pub fn reference_name_to_id(&self, name: &str) -> Result<Oid> {
159151
let name = CString::new(name)?;
160152
unsafe {
161-
let mut oid = uninitialized();
162-
check(raw::git_reference_name_to_id(&mut oid, self.raw,
163-
name.as_ptr() as *const c_char))?;
153+
let oid = {
154+
let mut oid = mem::MaybeUninit::uninit();
155+
check(raw::git_reference_name_to_id(
156+
oid.as_mut_ptr(), self.raw,
157+
name.as_ptr() as *const c_char))?;
158+
oid.assume_init()
159+
};
164160
Ok(Oid { raw: oid })
165161
}
166162
}
@@ -174,11 +170,9 @@ pub struct Commit<'repo> {
174170
_marker: PhantomData<&'repo Repository>
175171
}
176172

177-
use std::ptr::null_mut;
178-
179173
impl Repository {
180174
pub fn find_commit(&self, oid: &Oid) -> Result<Commit> {
181-
let mut commit = null_mut();
175+
let mut commit = ptr::null_mut();
182176
unsafe {
183177
check(raw::git_commit_lookup(&mut commit, self.raw, &oid.raw))?;
184178
}
@@ -240,7 +234,8 @@ impl<'text> Signature<'text> {
240234
/// borrowed from `_owner`.
241235
///
242236
/// Safety: if `ptr` is non-null, it must point to a null-terminated C
243-
/// string that is safe to access.
237+
/// string that is safe to access for at least as long as the lifetime of
238+
/// `_owner`.
244239
unsafe fn char_ptr_to_str<T>(_owner: &T, ptr: *const c_char) -> Option<&str> {
245240
if ptr.is_null() {
246241
return None;

libgit2-rs-safe/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
mod git;
1+
#![warn(rust_2018_idioms)]
2+
#![allow(elided_lifetimes_in_paths)]
23

3-
extern crate libc;
4+
mod git;
45

56
fn main() {
67
let path = std::env::args_os().skip(1).next()
7-
.expect("usage: libgit2-rs PATH");
8+
.expect("usage: git-toy PATH");
89

910
let repo = git::Repository::open(&path)
1011
.expect("opening repository");

0 commit comments

Comments
 (0)