Skip to content

Commit f3d4649

Browse files
committed
Rename to_cstring_new to something more self-descriptive
1 parent 51fce8e commit f3d4649

File tree

10 files changed

+100
-100
lines changed

10 files changed

+100
-100
lines changed

src/http/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl EspHttpConnection {
204204
) -> Result<(), EspError> {
205205
self.assert_initial();
206206

207-
let c_uri = try_cstring_new(uri)?;
207+
let c_uri = to_cstring_arg(uri)?;
208208

209209
esp!(unsafe { esp_http_client_set_url(self.raw_client, c_uri.as_ptr() as _) })?;
210210
esp!(unsafe {
@@ -223,10 +223,10 @@ impl EspHttpConnection {
223223
}
224224
}
225225

226-
let c_name = try_cstring_new(*name)?;
226+
let c_name = to_cstring_arg(*name)?;
227227

228228
// TODO: Replace with a proper conversion from UTF8 to ISO-8859-1
229-
let c_value = try_cstring_new(*value)?;
229+
let c_value = to_cstring_arg(*value)?;
230230

231231
esp!(unsafe {
232232
esp_http_client_set_header(

src/http/server.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use uncased::{Uncased, UncasedStr};
3131
use crate::errors::EspIOError;
3232
use crate::handle::RawHandle;
3333
use crate::private::common::Newtype;
34-
use crate::private::cstr::try_cstring_new;
34+
use crate::private::cstr::to_cstring_arg;
3535
use crate::private::cstr::{CStr, CString};
3636
use crate::private::mutex::{Mutex, RawMutex};
3737
#[cfg(esp_idf_esp_https_server_enable)]
@@ -371,7 +371,7 @@ impl EspHttpServer {
371371
where
372372
H: for<'a> Handler<EspHttpConnection<'a>> + 'static,
373373
{
374-
let c_str = try_cstring_new(uri)?;
374+
let c_str = to_cstring_arg(uri)?;
375375

376376
#[allow(clippy::needless_update)]
377377
let conf = httpd_uri_t {
@@ -581,7 +581,7 @@ impl<'a> EspHttpConnection<'a> {
581581
} else {
582582
let raw_req = self.request.0 as *const httpd_req_t as *mut httpd_req_t;
583583

584-
if let Ok(c_name) = try_cstring_new(name) {
584+
if let Ok(c_name) = to_cstring_arg(name) {
585585
match unsafe { httpd_req_get_hdr_value_len(raw_req, c_name.as_ptr() as _) } {
586586
0 => None,
587587
len => {
@@ -647,27 +647,27 @@ impl<'a> EspHttpConnection<'a> {
647647
status.to_string()
648648
};
649649

650-
let c_status = try_cstring_new(status.as_str())?;
650+
let c_status = to_cstring_arg(status.as_str())?;
651651
esp!(unsafe { httpd_resp_set_status(self.request.0, c_status.as_ptr() as _) })?;
652652

653653
c_headers.push(c_status);
654654

655655
for (key, value) in headers {
656656
if key.eq_ignore_ascii_case("Content-Type") {
657-
let c_type = try_cstring_new(*value)?;
657+
let c_type = to_cstring_arg(*value)?;
658658

659659
esp!(unsafe { httpd_resp_set_type(self.request.0, c_type.as_c_str().as_ptr()) })?;
660660

661661
c_headers.push(c_type);
662662
} else if key.eq_ignore_ascii_case("Content-Length") {
663-
let c_len = try_cstring_new(*value)?;
663+
let c_len = to_cstring_arg(*value)?;
664664

665665
//esp!(unsafe { httpd_resp_set_len(self.raw_req, c_len.as_c_str().as_ptr()) })?;
666666

667667
c_headers.push(c_len);
668668
} else {
669-
let name = try_cstring_new(*key)?;
670-
let value = try_cstring_new(*value)?;
669+
let name = to_cstring_arg(*key)?;
670+
let value = to_cstring_arg(*value)?;
671671

672672
esp!(unsafe {
673673
httpd_resp_set_hdr(
@@ -910,7 +910,7 @@ pub mod ws {
910910
use esp_idf_sys::*;
911911

912912
use crate::private::common::Newtype;
913-
use crate::private::cstr::try_cstring_new;
913+
use crate::private::cstr::to_cstring_arg;
914914
use crate::private::mutex::{RawCondvar, RawMutex};
915915

916916
use super::EspHttpServer;
@@ -1232,7 +1232,7 @@ pub mod ws {
12321232
H: for<'a> Fn(&'a mut EspHttpWsConnection) -> Result<(), E> + Send + Sync + 'static,
12331233
E: Debug,
12341234
{
1235-
let c_str = try_cstring_new(uri)?;
1235+
let c_str = to_cstring_arg(uri)?;
12361236

12371237
let (req_handler, close_handler) = self.to_native_ws_handler(self.sd, handler);
12381238

src/httpd.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'r> IdfRequest<'r> {
3535
status_string.push_str(message.as_str());
3636
}
3737

38-
let c_status = try_cstring_new(status_string.as_str())?;
38+
let c_status = to_cstring_arg(status_string.as_str())?;
3939

4040
esp!(unsafe { esp_idf_sys::httpd_resp_set_status(self.0, c_status.as_ptr()) })?;
4141

@@ -45,7 +45,7 @@ impl<'r> IdfRequest<'r> {
4545

4646
for (key, value) in response.headers {
4747
if key.as_str().eq_ignore_ascii_case("Content-Type") {
48-
c_content_type = Some(try_cstring_new(value.as_str()).unwrap());
48+
c_content_type = Some(to_cstring_arg(value.as_str()).unwrap());
4949
} else if key.as_str().eq_ignore_ascii_case("Content-Length") {
5050
content_len = Some(
5151
value
@@ -55,8 +55,8 @@ impl<'r> IdfRequest<'r> {
5555
);
5656
} else {
5757
c_headers.push((
58-
try_cstring_new(key.as_str()).unwrap(),
59-
try_cstring_new(value.as_str()).unwrap(),
58+
to_cstring_arg(key.as_str()).unwrap(),
59+
to_cstring_arg(value.as_str()).unwrap(),
6060
))
6161
}
6262
}
@@ -110,7 +110,7 @@ impl<'r> IdfRequest<'r> {
110110

111111
impl<'r> RequestDelegate for IdfRequest<'r> {
112112
fn header(&self, name: &str) -> Option<String> {
113-
if let Ok(c_str) = try_cstring_new(name) {
113+
if let Ok(c_str) = to_cstring_arg(name) {
114114
unsafe {
115115
match esp_idf_sys::httpd_req_get_hdr_value_len(self.0, c_str.as_ptr()) {
116116
0 => None,
@@ -250,7 +250,7 @@ impl Server {
250250
}
251251

252252
fn register(&mut self, handler: Handler) -> Result<()> {
253-
let c_str = try_cstring_new(handler.uri().as_ref())?;
253+
let c_str = to_cstring_arg(handler.uri().as_ref())?;
254254
let method = handler.method();
255255

256256
#[allow(clippy::needless_update)]

src/log.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl EspLogger {
104104
target: impl AsRef<str>,
105105
level_filter: LevelFilter,
106106
) -> Result<(), EspError> {
107-
let ctarget = try_cstring_new(target.as_ref())?;
107+
let ctarget = to_cstring_arg(target.as_ref())?;
108108

109109
unsafe {
110110
esp_log_level_set(
@@ -163,7 +163,7 @@ impl EspLogger {
163163
break ctarget;
164164
}
165165

166-
if let Ok(ctarget) = try_cstring_new(record.target()) {
166+
if let Ok(ctarget) = to_cstring_arg(record.target()) {
167167
cache.insert(record.target().into(), ctarget);
168168
} else {
169169
return true;

src/mdns.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use embedded_svc::ipv4::{IpAddr, Ipv4Addr, Ipv6Addr};
1414

1515
use esp_idf_sys::*;
1616

17-
use crate::private::cstr::try_cstring_new;
17+
use crate::private::cstr::to_cstring_arg;
1818
use crate::private::cstr::CStr;
1919
use crate::private::mutex::{Mutex, RawMutex};
2020

@@ -151,13 +151,13 @@ impl EspMdns {
151151
}
152152

153153
pub fn set_hostname(&mut self, hostname: impl AsRef<str>) -> Result<(), EspError> {
154-
let hostname = try_cstring_new(hostname.as_ref())?;
154+
let hostname = to_cstring_arg(hostname.as_ref())?;
155155

156156
esp!(unsafe { mdns_hostname_set(hostname.as_ptr()) })
157157
}
158158

159159
pub fn set_instance_name(&mut self, instance_name: impl AsRef<str>) -> Result<(), EspError> {
160-
let instance_name = try_cstring_new(instance_name.as_ref())?;
160+
let instance_name = to_cstring_arg(instance_name.as_ref())?;
161161

162162
esp!(unsafe { mdns_instance_name_set(instance_name.as_ptr()) })
163163
}
@@ -171,17 +171,17 @@ impl EspMdns {
171171
txt: &[(&str, &str)],
172172
) -> Result<(), EspError> {
173173
let instance_name = if let Some(instance_name) = instance_name {
174-
Some(try_cstring_new(instance_name)?)
174+
Some(to_cstring_arg(instance_name)?)
175175
} else {
176176
None
177177
};
178-
let service_type = try_cstring_new(service_type.as_ref())?;
179-
let proto = try_cstring_new(proto.as_ref())?;
178+
let service_type = to_cstring_arg(service_type.as_ref())?;
179+
let proto = to_cstring_arg(proto.as_ref())?;
180180
let mut txtcstr = Vec::with_capacity(txt.len());
181181
let mut txtptr = Vec::with_capacity(txt.len());
182182
for e in txt.iter() {
183-
let key = try_cstring_new(e.0)?;
184-
let value = try_cstring_new(e.1)?;
183+
let key = to_cstring_arg(e.0)?;
184+
let value = to_cstring_arg(e.1)?;
185185
txtptr.push(mdns_txt_item_t {
186186
key: key.as_ptr(),
187187
value: value.as_ptr(),
@@ -209,8 +209,8 @@ impl EspMdns {
209209
proto: impl AsRef<str>,
210210
port: u16,
211211
) -> Result<(), EspError> {
212-
let service_type = try_cstring_new(service_type.as_ref())?;
213-
let proto = try_cstring_new(proto.as_ref())?;
212+
let service_type = to_cstring_arg(service_type.as_ref())?;
213+
let proto = to_cstring_arg(proto.as_ref())?;
214214

215215
esp!(unsafe { mdns_service_port_set(service_type.as_ptr(), proto.as_ptr(), port) })
216216
}
@@ -221,9 +221,9 @@ impl EspMdns {
221221
proto: impl AsRef<str>,
222222
instance_name: impl AsRef<str>,
223223
) -> Result<(), EspError> {
224-
let service_type = try_cstring_new(service_type.as_ref())?;
225-
let proto = try_cstring_new(proto.as_ref())?;
226-
let instance_name = try_cstring_new(instance_name.as_ref())?;
224+
let service_type = to_cstring_arg(service_type.as_ref())?;
225+
let proto = to_cstring_arg(proto.as_ref())?;
226+
let instance_name = to_cstring_arg(instance_name.as_ref())?;
227227

228228
esp!(unsafe {
229229
mdns_service_instance_name_set(
@@ -241,10 +241,10 @@ impl EspMdns {
241241
key: impl AsRef<str>,
242242
value: impl AsRef<str>,
243243
) -> Result<(), EspError> {
244-
let service_type = try_cstring_new(service_type.as_ref())?;
245-
let proto = try_cstring_new(proto.as_ref())?;
246-
let key = try_cstring_new(key.as_ref())?;
247-
let value = try_cstring_new(value.as_ref())?;
244+
let service_type = to_cstring_arg(service_type.as_ref())?;
245+
let proto = to_cstring_arg(proto.as_ref())?;
246+
let key = to_cstring_arg(key.as_ref())?;
247+
let value = to_cstring_arg(value.as_ref())?;
248248

249249
esp!(unsafe {
250250
mdns_service_txt_item_set(
@@ -262,9 +262,9 @@ impl EspMdns {
262262
proto: impl AsRef<str>,
263263
key: impl AsRef<str>,
264264
) -> Result<(), EspError> {
265-
let service_type = try_cstring_new(service_type.as_ref())?;
266-
let proto = try_cstring_new(proto.as_ref())?;
267-
let key = try_cstring_new(key.as_ref())?;
265+
let service_type = to_cstring_arg(service_type.as_ref())?;
266+
let proto = to_cstring_arg(proto.as_ref())?;
267+
let key = to_cstring_arg(key.as_ref())?;
268268

269269
esp!(unsafe {
270270
mdns_service_txt_item_remove(service_type.as_ptr(), proto.as_ptr(), key.as_ptr())
@@ -277,15 +277,15 @@ impl EspMdns {
277277
proto: impl AsRef<str>,
278278
txt: &[(&str, &str)],
279279
) -> Result<(), EspError> {
280-
let service_type = try_cstring_new(service_type.as_ref())?;
281-
let proto = try_cstring_new(proto.as_ref())?;
280+
let service_type = to_cstring_arg(service_type.as_ref())?;
281+
let proto = to_cstring_arg(proto.as_ref())?;
282282

283283
let mut txtcstr = Vec::with_capacity(txt.len());
284284
let mut txtptr = Vec::with_capacity(txt.len());
285285

286286
for e in txt.iter() {
287-
let key = try_cstring_new(e.0)?;
288-
let value = try_cstring_new(e.1)?;
287+
let key = to_cstring_arg(e.0)?;
288+
let value = to_cstring_arg(e.1)?;
289289
txtptr.push(mdns_txt_item_t {
290290
key: key.as_ptr(),
291291
value: value.as_ptr(),
@@ -308,8 +308,8 @@ impl EspMdns {
308308
service_type: impl AsRef<str>,
309309
proto: impl AsRef<str>,
310310
) -> Result<(), EspError> {
311-
let service_type = try_cstring_new(service_type.as_ref())?;
312-
let proto = try_cstring_new(proto.as_ref())?;
311+
let service_type = to_cstring_arg(service_type.as_ref())?;
312+
let proto = to_cstring_arg(proto.as_ref())?;
313313

314314
esp!(unsafe { mdns_service_remove(service_type.as_ptr(), proto.as_ptr()) })
315315
}
@@ -330,17 +330,17 @@ impl EspMdns {
330330
results: &mut [QueryResult],
331331
) -> Result<usize, EspError> {
332332
let name = if let Some(name) = name {
333-
Some(try_cstring_new(name)?)
333+
Some(to_cstring_arg(name)?)
334334
} else {
335335
None
336336
};
337337
let service_type = if let Some(service_type) = service_type {
338-
Some(try_cstring_new(service_type)?)
338+
Some(to_cstring_arg(service_type)?)
339339
} else {
340340
None
341341
};
342342
let proto = if let Some(proto) = proto {
343-
Some(try_cstring_new(proto)?)
343+
Some(to_cstring_arg(proto)?)
344344
} else {
345345
None
346346
};
@@ -370,7 +370,7 @@ impl EspMdns {
370370
hostname: impl AsRef<str>,
371371
timeout: Duration,
372372
) -> Result<Ipv4Addr, EspError> {
373-
let hostname = try_cstring_new(hostname.as_ref())?;
373+
let hostname = to_cstring_arg(hostname.as_ref())?;
374374
let mut addr: esp_ip4_addr_t = Default::default();
375375

376376
esp!(unsafe { mdns_query_a(hostname.as_ptr(), timeout.as_millis() as _, &mut addr) })?;
@@ -383,7 +383,7 @@ impl EspMdns {
383383
hostname: impl AsRef<str>,
384384
timeout: Duration,
385385
) -> Result<Ipv6Addr, EspError> {
386-
let hostname = try_cstring_new(hostname.as_ref())?;
386+
let hostname = to_cstring_arg(hostname.as_ref())?;
387387
let mut addr: esp_ip6_addr_t = Default::default();
388388

389389
esp!(unsafe { mdns_query_aaaa(hostname.as_ptr(), timeout.as_millis() as _, &mut addr) })?;
@@ -399,9 +399,9 @@ impl EspMdns {
399399
timeout: Duration,
400400
results: &mut [QueryResult],
401401
) -> Result<usize, EspError> {
402-
let instance_name = try_cstring_new(instance_name.as_ref())?;
403-
let service_type = try_cstring_new(service_type.as_ref())?;
404-
let proto = try_cstring_new(proto.as_ref())?;
402+
let instance_name = to_cstring_arg(instance_name.as_ref())?;
403+
let service_type = to_cstring_arg(service_type.as_ref())?;
404+
let proto = to_cstring_arg(proto.as_ref())?;
405405
let mut result = core::ptr::null_mut();
406406

407407
esp!(unsafe {
@@ -428,9 +428,9 @@ impl EspMdns {
428428
timeout: Duration,
429429
results: &mut [QueryResult],
430430
) -> Result<usize, EspError> {
431-
let instance_name = try_cstring_new(instance_name.as_ref())?;
432-
let service_type = try_cstring_new(service_type.as_ref())?;
433-
let proto = try_cstring_new(proto.as_ref())?;
431+
let instance_name = to_cstring_arg(instance_name.as_ref())?;
432+
let service_type = to_cstring_arg(service_type.as_ref())?;
433+
let proto = to_cstring_arg(proto.as_ref())?;
434434
let mut result = core::ptr::null_mut();
435435

436436
esp!(unsafe {
@@ -457,8 +457,8 @@ impl EspMdns {
457457
max_results: usize,
458458
results: &mut [QueryResult],
459459
) -> Result<usize, EspError> {
460-
let service_type = try_cstring_new(service_type.as_ref())?;
461-
let proto = try_cstring_new(proto.as_ref())?;
460+
let service_type = to_cstring_arg(service_type.as_ref())?;
461+
let proto = to_cstring_arg(proto.as_ref())?;
462462
let mut result = core::ptr::null_mut();
463463

464464
esp!(unsafe {

0 commit comments

Comments
 (0)