1
- use std:: { ffi:: CStr , sync:: atomic:: { AtomicPtr , Ordering } } ;
1
+ use std:: {
2
+ ffi:: CStr ,
3
+ sync:: atomic:: { AtomicPtr , Ordering } ,
4
+ } ;
2
5
3
6
use libc:: { c_char, c_int} ;
4
7
@@ -61,18 +64,16 @@ impl Binding for TraceLevel {
61
64
/// see `trace_set` to register a subscriber.
62
65
pub type TracingCb = fn ( TraceLevel , & [ u8 ] ) ;
63
66
64
- /// Use an atomic pointer to store the global tracing subscriber function.
67
+ /// Use an atomic pointer to store the global tracing subscriber function.
65
68
static CALLBACK : AtomicPtr < ( ) > = AtomicPtr :: new ( std:: ptr:: null_mut ( ) ) ;
66
69
67
70
/// Set the global subscriber called when libgit2 produces a tracing message.
68
71
pub fn trace_set ( level : TraceLevel , cb : TracingCb ) -> Result < ( ) , Error > {
69
72
// Store the callback in the global atomic.
70
73
CALLBACK . store ( cb as * mut ( ) , Ordering :: SeqCst ) ;
71
74
72
- // git_trace_set returns 0 if there was no error.
73
- let return_code: c_int = unsafe {
74
- raw:: git_trace_set ( level. raw ( ) , Some ( tracing_cb_c) )
75
- } ;
75
+ // git_trace_set returns 0 if there was no error.
76
+ let return_code: c_int = unsafe { raw:: git_trace_set ( level. raw ( ) , Some ( tracing_cb_c) ) } ;
76
77
77
78
if return_code != 0 {
78
79
// Unwrap here is fine since `Error::last_error` always returns `Some`.
@@ -88,10 +89,10 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) {
88
89
let cb: * mut ( ) = CALLBACK . load ( Ordering :: SeqCst ) ;
89
90
90
91
// Transmute the callback pointer into the function pointer we know it to be.
91
- //
92
- // SAFETY: We only ever set the callback pointer with something cast from a TracingCb
93
- // so transmuting back to a TracingCb is safe. This is notably not an integer-to-pointer
94
- // transmute as described in the mem::transmute documentation and is in-line with the
92
+ //
93
+ // SAFETY: We only ever set the callback pointer with something cast from a TracingCb
94
+ // so transmuting back to a TracingCb is safe. This is notably not an integer-to-pointer
95
+ // transmute as described in the mem::transmute documentation and is in-line with the
95
96
// example in that documentation for casing between *const () to fn pointers.
96
97
let cb: TracingCb = unsafe { std:: mem:: transmute ( cb) } ;
97
98
@@ -102,10 +103,10 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) {
102
103
}
103
104
104
105
// Convert the message from a *const c_char to a &[u8] and pass it to the callback.
105
- //
106
- // SAFETY: We've just checked that the pointer is not null. The other safety requirements are left to
106
+ //
107
+ // SAFETY: We've just checked that the pointer is not null. The other safety requirements are left to
107
108
// libgit2 to enforce -- namely that it gives us a valid, nul-terminated, C string, that that string exists
108
- // entirely in one allocation, that the string will not be mutated once passed to us, and that the nul-terminator is
109
+ // entirely in one allocation, that the string will not be mutated once passed to us, and that the nul-terminator is
109
110
// within isize::MAX bytes from the given pointers data address.
110
111
let msg: & CStr = unsafe { CStr :: from_ptr ( msg) } ;
111
112
@@ -116,7 +117,7 @@ extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) {
116
117
panic:: wrap ( || {
117
118
// Convert the raw trace level into a type we can pass to the rust callback fn.
118
119
//
119
- // SAFETY: Currently the implementation of this function (above) may panic, but is only marked as unsafe to match
120
+ // SAFETY: Currently the implementation of this function (above) may panic, but is only marked as unsafe to match
120
121
// the trait definition, thus we can consider this call safe.
121
122
let level: TraceLevel = unsafe { Binding :: from_raw ( level) } ;
122
123
@@ -134,6 +135,7 @@ mod tests {
134
135
fn smoke ( ) {
135
136
super :: trace_set ( TraceLevel :: Trace , |level, msg| {
136
137
dbg ! ( level, msg) ;
137
- } ) . expect ( "libgit2 can set global trace callback" ) ;
138
+ } )
139
+ . expect ( "libgit2 can set global trace callback" ) ;
138
140
}
139
141
}
0 commit comments