Skip to content

Commit 42faa66

Browse files
committed
capi: use c_int::from(bool) and naked signed integers for returning c_ints
c_int::from(bool) relies on the semantics documented in the Rust reference so that: i{32,64,etc}::from(b: bool) will return 0 with b == false and 1 with b == true. This change also uses `return {0,1,-1}` without an explicit From::from for c_int as suggested by clippy. The semantics now require that a (signed) C integer has a direct representation as a signed Rust one _as inferred by the compiler_ (ie. i32, i64, ...).
1 parent 602a459 commit 42faa66

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

src/capi/encoding.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ pub unsafe extern "C" fn encoding_encode_buffer(
3131
let c_slice_dst = if let Some(c_slice_mut) = CSliceMut::new_from_size_ptr(dst, dstlen_ptr) {
3232
c_slice_mut
3333
} else {
34-
return c_int::from(-1);
34+
return -1;
3535
};
3636

3737
let c_slice_src = if let Some(c_slice) = CSlice::new(src, srclen) {
3838
c_slice
3939
} else {
40-
return c_int::from(-1);
40+
return -1;
4141
};
4242

4343
let cow = encoding::encode(&c_slice_src);
@@ -46,7 +46,7 @@ pub unsafe extern "C" fn encoding_encode_buffer(
4646
unsafe { *dstlen_ptr = required_len };
4747

4848
if required_len > c_slice_dst.len() {
49-
return c_int::from(-1);
49+
return -1;
5050
}
5151

5252
unsafe {
@@ -55,5 +55,5 @@ pub unsafe extern "C" fn encoding_encode_buffer(
5555
core::ptr::copy_nonoverlapping(cow.as_ptr(), dst as *mut _, required_len);
5656
}
5757

58-
c_int::from(0)
58+
0
5959
}

src/capi/mapping_rule.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,26 @@ pub unsafe extern "C" fn rest_rule_matches(
8383
path_qs: *const c_char,
8484
) -> c_int {
8585
if rule.is_null() {
86-
return c_int::from(-1);
86+
return -1;
8787
}
8888

8989
let method_s = if let Some(c_slice) = CSlice::new(method, 0) {
9090
c_slice.as_cow()
9191
} else {
92-
return c_int::from(-1);
92+
return -1;
9393
};
9494
let path_qs = if let Some(c_slice) = CSlice::new(path_qs, 0) {
9595
c_slice.as_cow()
9696
} else {
97-
return c_int::from(-1);
97+
return -1;
9898
};
9999

100100
let rule = unsafe { std::ptr::read::<RestRule>(rule) };
101101
let rule = ManuallyDrop::new(rule);
102102

103103
let method = Method::from(method_s.as_ref());
104104

105-
if rule.matches(&method, path_qs) {
106-
c_int::from(1)
107-
} else {
108-
c_int::from(0)
109-
}
105+
c_int::from(rule.matches(&method, path_qs))
110106
}
111107

112108
#[no_mangle]
@@ -117,18 +113,18 @@ pub unsafe extern "C" fn rest_rule_matches_path_n_qs(
117113
qs: *const c_char,
118114
) -> c_int {
119115
if rule.is_null() {
120-
return c_int::from(-1);
116+
return -1;
121117
}
122118

123119
let method_s = if let Some(c_slice) = CSlice::new(method, 0) {
124120
c_slice.as_cow()
125121
} else {
126-
return c_int::from(-1);
122+
return -1;
127123
};
128124
let path = if let Some(c_slice) = CSlice::new(path, 0) {
129125
c_slice.as_cow()
130126
} else {
131-
return c_int::from(-1);
127+
return -1;
132128
};
133129
let qs = if let Some(c_slice) = CSlice::new(qs, 0) {
134130
c_slice.as_cow().into()
@@ -141,11 +137,7 @@ pub unsafe extern "C" fn rest_rule_matches_path_n_qs(
141137

142138
let method = Method::from(method_s.as_ref());
143139

144-
if &method == rule.method() && rule.matches_path_n_qs(path, qs) {
145-
c_int::from(1)
146-
} else {
147-
c_int::from(0)
148-
}
140+
c_int::from(&method == rule.method() && rule.matches_path_n_qs(path, qs))
149141
}
150142

151143
#[no_mangle]
@@ -154,27 +146,21 @@ pub unsafe extern "C" fn rest_rule_matches_request_line(
154146
http_request_line: *const c_char,
155147
) -> c_int {
156148
if rule.is_null() {
157-
return c_int::from(-1);
149+
return -1;
158150
}
159151

160152
let http_request_line = if let Some(c_slice) = CSlice::new(http_request_line, 0) {
161153
c_slice.as_cow()
162154
} else {
163-
return c_int::from(-1);
155+
return -1;
164156
};
165157

166158
let rule = unsafe { std::ptr::read::<RestRule>(rule) };
167159
let rule = ManuallyDrop::new(rule);
168160

169161
match rule.matches_request_line(http_request_line) {
170-
Ok(b) => {
171-
if b {
172-
c_int::from(1)
173-
} else {
174-
c_int::from(0)
175-
}
176-
}
177-
Err(_) => c_int::from(-1),
162+
Ok(b) => c_int::from(b),
163+
Err(_) => -1,
178164
}
179165
}
180166

0 commit comments

Comments
 (0)