1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use libc::{c_char, c_int, size_t};
use std::cmp::Ordering;
use std::ffi::{CString, OsStr, OsString};
use std::iter::IntoIterator;
use std::path::{Path, PathBuf};
use crate::{raw, Error};
#[doc(hidden)]
pub trait IsNull {
fn is_ptr_null(&self) -> bool;
}
impl<T> IsNull for *const T {
fn is_ptr_null(&self) -> bool {
self.is_null()
}
}
impl<T> IsNull for *mut T {
fn is_ptr_null(&self) -> bool {
self.is_null()
}
}
#[doc(hidden)]
pub trait Binding: Sized {
type Raw;
unsafe fn from_raw(raw: Self::Raw) -> Self;
fn raw(&self) -> Self::Raw;
unsafe fn from_raw_opt<T>(raw: T) -> Option<Self>
where
T: Copy + IsNull,
Self: Binding<Raw = T>,
{
if raw.is_ptr_null() {
None
} else {
Some(Binding::from_raw(raw))
}
}
}
pub fn iter2cstrs<T, I>(
iter: I,
) -> Result<(Vec<CString>, Vec<*const c_char>, raw::git_strarray), Error>
where
T: IntoCString,
I: IntoIterator<Item = T>,
{
let cstrs = iter
.into_iter()
.map(|i| i.into_c_string())
.collect::<Result<Vec<CString>, _>>()?;
let ptrs = cstrs.iter().map(|i| i.as_ptr()).collect::<Vec<_>>();
let raw = raw::git_strarray {
strings: ptrs.as_ptr() as *mut _,
count: ptrs.len() as size_t,
};
Ok((cstrs, ptrs, raw))
}
#[cfg(unix)]
pub fn bytes2path(b: &[u8]) -> &Path {
use std::os::unix::prelude::*;
Path::new(OsStr::from_bytes(b))
}
#[cfg(windows)]
pub fn bytes2path(b: &[u8]) -> &Path {
use std::str;
Path::new(str::from_utf8(b).unwrap())
}
pub trait IntoCString {
fn into_c_string(self) -> Result<CString, Error>;
}
impl<'a, T: IntoCString + Clone> IntoCString for &'a T {
fn into_c_string(self) -> Result<CString, Error> {
self.clone().into_c_string()
}
}
impl<'a> IntoCString for &'a str {
fn into_c_string(self) -> Result<CString, Error> {
Ok(CString::new(self)?)
}
}
impl IntoCString for String {
fn into_c_string(self) -> Result<CString, Error> {
Ok(CString::new(self.into_bytes())?)
}
}
impl IntoCString for CString {
fn into_c_string(self) -> Result<CString, Error> {
Ok(self)
}
}
impl<'a> IntoCString for &'a Path {
fn into_c_string(self) -> Result<CString, Error> {
let s: &OsStr = self.as_ref();
s.into_c_string()
}
}
impl IntoCString for PathBuf {
fn into_c_string(self) -> Result<CString, Error> {
let s: OsString = self.into();
s.into_c_string()
}
}
impl<'a> IntoCString for &'a OsStr {
fn into_c_string(self) -> Result<CString, Error> {
self.to_os_string().into_c_string()
}
}
impl IntoCString for OsString {
#[cfg(unix)]
fn into_c_string(self) -> Result<CString, Error> {
use std::os::unix::prelude::*;
let s: &OsStr = self.as_ref();
Ok(CString::new(s.as_bytes())?)
}
#[cfg(windows)]
fn into_c_string(self) -> Result<CString, Error> {
match self.to_str() {
Some(s) => s.into_c_string(),
None => Err(Error::from_str(
"only valid unicode paths are accepted \
on windows",
)),
}
}
}
impl<'a> IntoCString for &'a [u8] {
fn into_c_string(self) -> Result<CString, Error> {
Ok(CString::new(self)?)
}
}
impl IntoCString for Vec<u8> {
fn into_c_string(self) -> Result<CString, Error> {
Ok(CString::new(self)?)
}
}
pub fn into_opt_c_string<S>(opt_s: Option<S>) -> Result<Option<CString>, Error>
where
S: IntoCString,
{
match opt_s {
None => Ok(None),
Some(s) => Ok(Some(s.into_c_string()?)),
}
}
pub fn c_cmp_to_ordering(cmp: c_int) -> Ordering {
match cmp {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
}