From 87e324d72071e0ae9eb658a1f7cbe1a5cb8382b8 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sun, 10 Mar 2024 11:53:29 +0100 Subject: [PATCH] Use CStr::to_str where possible --- src/collation.rs | 7 +++---- src/column.rs | 7 +++++-- src/hooks.rs | 3 ++- src/lib.rs | 3 +-- src/session.rs | 10 ++-------- src/statement.rs | 5 +++-- src/trace.rs | 9 +++------ 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/collation.rs b/src/collation.rs index c1fe3f7..ade51e0 100644 --- a/src/collation.rs +++ b/src/collation.rs @@ -128,10 +128,9 @@ impl InnerConnection { let callback: fn(&Connection, &str) -> Result<()> = mem::transmute(arg1); let res = catch_unwind(|| { let conn = Connection::from_handle(arg2).unwrap(); - let collation_name = { - let c_slice = CStr::from_ptr(arg3).to_bytes(); - str::from_utf8(c_slice).expect("illegal collation sequence name") - }; + let collation_name = CStr::from_ptr(arg3) + .to_str() + .expect("illegal collation sequence name"); callback(&conn, collation_name) }); if res.is_err() { diff --git a/src/column.rs b/src/column.rs index b18eb8a..05c709b 100644 --- a/src/column.rs +++ b/src/column.rs @@ -104,7 +104,9 @@ impl Statement<'_> { // clippy::or_fun_call (nightly) vs clippy::unnecessary-lazy-evaluations (stable) .ok_or(Error::InvalidColumnIndex(col)) .map(|slice| { - str::from_utf8(slice.to_bytes()).expect("Invalid UTF-8 sequence in column name") + slice + .to_str() + .expect("Invalid UTF-8 sequence in column name") }) } @@ -149,7 +151,8 @@ impl Statement<'_> { let name = self.column_name_unwrap(i); let slice = self.stmt.column_decltype(i); let decl_type = slice.map(|s| { - str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration") + s.to_str() + .expect("Invalid UTF-8 sequence in column declaration") }); cols.push(Column { name, decl_type }); } diff --git a/src/hooks.rs b/src/hooks.rs index 52a53a3..108334b 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -666,7 +666,8 @@ unsafe fn expect_optional_utf8<'a>( if p_str.is_null() { return None; } - std::str::from_utf8(std::ffi::CStr::from_ptr(p_str).to_bytes()) + std::ffi::CStr::from_ptr(p_str) + .to_str() .unwrap_or_else(|_| panic!("received non-utf8 string as {description}")) .into() } diff --git a/src/lib.rs b/src/lib.rs index d6c8156..b682145 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -289,8 +289,7 @@ impl OptionalExtension for Result { } unsafe fn errmsg_to_string(errmsg: *const c_char) -> String { - let c_slice = CStr::from_ptr(errmsg).to_bytes(); - String::from_utf8_lossy(c_slice).into_owned() + CStr::from_ptr(errmsg).to_string_lossy().into_owned() } fn str_to_cstring(s: &str) -> Result { diff --git a/src/session.rs b/src/session.rs index 0169a1c..41bd575 100644 --- a/src/session.rs +++ b/src/session.rs @@ -71,10 +71,7 @@ impl Session<'_> { use std::str; let boxed_filter: *mut F = p_arg as *mut F; - let tbl_name = { - let c_slice = CStr::from_ptr(tbl_str).to_bytes(); - str::from_utf8(c_slice) - }; + let tbl_name = CStr::from_ptr(tbl_str).to_str(); c_int::from( catch_unwind(|| (*boxed_filter)(tbl_name.expect("non-utf8 table name"))) .unwrap_or_default(), @@ -700,10 +697,7 @@ where use std::str; let tuple: *mut (Option, C) = p_ctx as *mut (Option, C); - let tbl_name = { - let c_slice = CStr::from_ptr(tbl_str).to_bytes(); - str::from_utf8(c_slice) - }; + let tbl_name = CStr::from_ptr(tbl_str).to_str(); match *tuple { (Some(ref filter), _) => c_int::from( catch_unwind(|| filter(tbl_name.expect("illegal table name"))).unwrap_or_default(), diff --git a/src/statement.rs b/src/statement.rs index 3d1f5a6..84b7e26 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -441,7 +441,8 @@ impl Statement<'_> { #[inline] pub fn parameter_name(&self, index: usize) -> Option<&'_ str> { self.stmt.bind_parameter_name(index as i32).map(|name| { - str::from_utf8(name.to_bytes()).expect("Invalid UTF-8 sequence in parameter name") + name.to_str() + .expect("Invalid UTF-8 sequence in parameter name") }) } @@ -766,7 +767,7 @@ impl fmt::Debug for Statement<'_> { let sql = if self.stmt.is_null() { Ok("") } else { - str::from_utf8(self.stmt.sql().unwrap().to_bytes()) + self.stmt.sql().unwrap().to_str() }; f.debug_struct("Statement") .field("conn", self.conn) diff --git a/src/trace.rs b/src/trace.rs index 7317a0c..c070c35 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -27,10 +27,9 @@ use crate::Connection; #[cfg(not(feature = "loadable_extension"))] pub unsafe fn config_log(callback: Option) -> crate::Result<()> { extern "C" fn log_callback(p_arg: *mut c_void, err: c_int, msg: *const c_char) { - let c_slice = unsafe { CStr::from_ptr(msg).to_bytes() }; + let s = unsafe { CStr::from_ptr(msg).to_string_lossy() }; let callback: fn(c_int, &str) = unsafe { mem::transmute(p_arg) }; - let s = String::from_utf8_lossy(c_slice); drop(catch_unwind(|| callback(err, &s))); } @@ -72,8 +71,7 @@ impl Connection { pub fn trace(&mut self, trace_fn: Option) { unsafe extern "C" fn trace_callback(p_arg: *mut c_void, z_sql: *const c_char) { let trace_fn: fn(&str) = mem::transmute(p_arg); - let c_slice = CStr::from_ptr(z_sql).to_bytes(); - let s = String::from_utf8_lossy(c_slice); + let s = CStr::from_ptr(z_sql).to_string_lossy(); drop(catch_unwind(|| trace_fn(&s))); } @@ -100,8 +98,7 @@ impl Connection { nanoseconds: u64, ) { let profile_fn: fn(&str, Duration) = mem::transmute(p_arg); - let c_slice = CStr::from_ptr(z_sql).to_bytes(); - let s = String::from_utf8_lossy(c_slice); + let s = CStr::from_ptr(z_sql).to_string_lossy(); const NANOS_PER_SEC: u64 = 1_000_000_000; let duration = Duration::new(