@@ -18,9 +18,7 @@ impl fmt::Display for Error {
18
18
}
19
19
}
20
20
21
- impl error:: Error for Error {
22
- fn description ( & self ) -> & str { & self . message }
23
- }
21
+ impl error:: Error for Error { }
24
22
25
23
pub type Result < T > = result:: Result < T , Error > ;
26
24
@@ -51,29 +49,27 @@ fn check(code: c_int) -> Result<c_int> {
51
49
52
50
/// A Git repository.
53
51
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.
55
53
// No other `Repository` may point to it.
56
54
raw : * mut raw:: git_repository
57
55
}
58
56
59
57
use std:: path:: Path ;
58
+ use std:: ptr;
60
59
61
60
impl Repository {
62
61
pub fn open < P : AsRef < Path > > ( path : P ) -> Result < Repository > {
63
62
ensure_initialized ( ) ;
64
63
65
64
let path = path_to_cstring ( path. as_ref ( ) ) ?;
66
- let mut repo = null_mut ( ) ;
65
+ let mut repo = ptr :: null_mut ( ) ;
67
66
unsafe {
68
67
check ( raw:: git_repository_open ( & mut repo, path. as_ptr ( ) ) ) ?;
69
68
}
70
69
Ok ( Repository { raw : repo } )
71
70
}
72
71
}
73
72
74
- use std;
75
- use libc;
76
-
77
73
fn ensure_initialized ( ) {
78
74
static ONCE : std:: sync:: Once = std:: sync:: Once :: new ( ) ;
79
75
ONCE . call_once ( || {
@@ -85,14 +81,10 @@ fn ensure_initialized() {
85
81
} ) ;
86
82
}
87
83
88
- use std:: io:: Write ;
89
-
90
84
extern fn shutdown ( ) {
91
85
unsafe {
92
86
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) ;
96
88
std:: process:: abort ( ) ;
97
89
}
98
90
}
@@ -151,16 +143,20 @@ pub struct Oid {
151
143
pub raw : raw:: git_oid
152
144
}
153
145
154
- use std:: mem:: uninitialized ;
146
+ use std:: mem;
155
147
use std:: os:: raw:: c_char;
156
148
157
149
impl Repository {
158
150
pub fn reference_name_to_id ( & self , name : & str ) -> Result < Oid > {
159
151
let name = CString :: new ( name) ?;
160
152
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
+ } ;
164
160
Ok ( Oid { raw : oid } )
165
161
}
166
162
}
@@ -174,11 +170,9 @@ pub struct Commit<'repo> {
174
170
_marker : PhantomData < & ' repo Repository >
175
171
}
176
172
177
- use std:: ptr:: null_mut;
178
-
179
173
impl Repository {
180
174
pub fn find_commit ( & self , oid : & Oid ) -> Result < Commit > {
181
- let mut commit = null_mut ( ) ;
175
+ let mut commit = ptr :: null_mut ( ) ;
182
176
unsafe {
183
177
check ( raw:: git_commit_lookup ( & mut commit, self . raw , & oid. raw ) ) ?;
184
178
}
@@ -240,7 +234,8 @@ impl<'text> Signature<'text> {
240
234
/// borrowed from `_owner`.
241
235
///
242
236
/// 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`.
244
239
unsafe fn char_ptr_to_str < T > ( _owner : & T , ptr : * const c_char ) -> Option < & str > {
245
240
if ptr. is_null ( ) {
246
241
return None ;
0 commit comments