Skip to content

Commit 55bd11a

Browse files
cursoragentlovasoa
andcommitted
Refactor: Improve sqlx internal code quality and consistency
Co-authored-by: contact <[email protected]>
1 parent 6893425 commit 55bd11a

File tree

33 files changed

+108
-87
lines changed

33 files changed

+108
-87
lines changed

sqlx-core/src/io/buf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait BufExt: Buf {
2222
impl BufExt for Bytes {
2323
fn get_bytes_nul(&mut self) -> Result<Bytes, Error> {
2424
let nul =
25-
memchr(b'\0', &self).ok_or_else(|| err_protocol!("expected NUL in byte sequence"))?;
25+
memchr(b'\0', self).ok_or_else(|| err_protocol!("expected NUL in byte sequence"))?;
2626

2727
let v = self.slice(0..nul);
2828

@@ -40,7 +40,7 @@ impl BufExt for Bytes {
4040

4141
fn get_str_nul(&mut self) -> Result<String, Error> {
4242
self.get_bytes_nul().and_then(|bytes| {
43-
from_utf8(&*bytes)
43+
from_utf8(&bytes)
4444
.map(ToOwned::to_owned)
4545
.map_err(|err| err_protocol!("{}", err))
4646
})

sqlx-core/src/logger.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ impl<'q> QueryLogger<'q> {
4747
.to_level()
4848
.filter(|lvl| log::log_enabled!(target: "sqlx::query", *lvl))
4949
{
50-
let mut summary = parse_query_summary(&self.sql);
50+
let mut summary = parse_query_summary(self.sql);
5151

5252
let sql = if summary != self.sql {
5353
summary.push_str(" …");
54-
format!("\n\n{}\n", &self.sql)
54+
format!("\n\n{}\n", self.sql)
5555
} else {
5656
String::new()
5757
};
@@ -105,9 +105,9 @@ impl<'q, O: Debug + Hash + Eq, R: Debug, P: Debug> QueryPlanLogger<'q, O, R, P>
105105
.to_level()
106106
.filter(|lvl| log::log_enabled!(target: "sqlx::explain", *lvl))
107107
{
108-
return true;
108+
true
109109
} else {
110-
return false;
110+
false
111111
}
112112
}
113113

@@ -126,11 +126,11 @@ impl<'q, O: Debug + Hash + Eq, R: Debug, P: Debug> QueryPlanLogger<'q, O, R, P>
126126
.to_level()
127127
.filter(|lvl| log::log_enabled!(target: "sqlx::explain", *lvl))
128128
{
129-
let mut summary = parse_query_summary(&self.sql);
129+
let mut summary = parse_query_summary(self.sql);
130130

131131
let sql = if summary != self.sql {
132132
summary.push_str(" …");
133-
format!("\n\n{}\n", &self.sql)
133+
format!("\n\n{}\n", self.sql)
134134
} else {
135135
String::new()
136136
};

sqlx-core/src/mssql/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl MssqlArguments {
8686
// @p1 int, @p2 nvarchar(10), ...
8787

8888
if !declarations.is_empty() {
89-
declarations.push_str(",");
89+
declarations.push(',');
9090
}
9191

9292
declarations.push_str(name);

sqlx-core/src/mssql/connection/establish.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ impl MssqlConnection {
4343
stream.setup_encryption().await?;
4444
}
4545
(Encrypt::Required, Encrypt::Off | Encrypt::NotSupported) => {
46-
return Err(Error::Tls(Box::new(std::io::Error::new(
47-
std::io::ErrorKind::Other,
46+
return Err(Error::Tls(Box::new(std::io::Error::other(
4847
"TLS encryption required but not supported by server",
4948
))));
5049
}

sqlx-core/src/mssql/connection/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'c> Executor<'c> for &'c mut MssqlConnection {
140140
where
141141
'c: 'e,
142142
'q: 'e,
143-
E: Execute<'q, Self::Database>,
143+
E: Execute<'q, Self::Database> + 'e,
144144
{
145145
let mut s = self.fetch_many(query);
146146

sqlx-core/src/mssql/connection/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl MssqlStream {
142142
// TDS communicates in streams of packets that are themselves streams of messages
143143
pub(super) async fn recv_message(&mut self) -> Result<Message, Error> {
144144
loop {
145-
while self.response.as_ref().map_or(false, |r| !r.1.is_empty()) {
145+
while self.response.as_ref().is_some_and(|r| !r.1.is_empty()) {
146146
let buf = if let Some((_, buf)) = self.response.as_mut() {
147147
buf
148148
} else {

sqlx-core/src/mssql/connection/tls_prelogin_stream_wrapper.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use std::task::{self, ready, Poll};
3535
///
3636
/// This allows us to use standard TLS libraries while still conforming to the TDS protocol
3737
/// requirements for the PRELOGIN phase.
38-
3938
const HEADER_BYTES: usize = 8;
4039

4140
pub(crate) struct TlsPreloginWrapper<S> {
@@ -101,14 +100,14 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncRead for TlsPreloginWrapper<
101100

102101
let read = header_buf.filled().len();
103102
if read == 0 {
104-
return Poll::Ready(Ok(PollReadOut::default()));
103+
return Poll::Ready(Ok(()));
105104
}
106105

107106
inner.header_pos += read;
108107
}
109108

110109
let header: PacketHeader = Decode::decode(Bytes::copy_from_slice(&inner.header_buf))
111-
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
110+
.map_err(|err| io::Error::other(err))?;
112111

113112
inner.read_remaining = usize::from(header.length) - HEADER_BYTES;
114113

@@ -122,7 +121,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncRead for TlsPreloginWrapper<
122121
let max_read = std::cmp::min(inner.read_remaining, buf.remaining());
123122
let mut limited_buf = buf.take(max_read);
124123

125-
let res = ready!(Pin::new(&mut inner.stream).poll_read(cx, &mut limited_buf))?;
124+
ready!(Pin::new(&mut inner.stream).poll_read(cx, &mut limited_buf))?;
126125

127126
let read = limited_buf.filled().len();
128127
buf.advance(read);
@@ -132,7 +131,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> AsyncRead for TlsPreloginWrapper<
132131
inner.header_pos = 0;
133132
}
134133

135-
Poll::Ready(Ok(res))
134+
Poll::Ready(Ok(()))
136135
}
137136

138137
#[cfg(feature = "_rt-async-std")]

sqlx-core/src/mssql/options/parse.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ impl FromStr for MssqlConnectOptions {
6060
let username = url.username();
6161
if !username.is_empty() {
6262
options = options.username(
63-
&*percent_decode_str(username)
63+
&percent_decode_str(username)
6464
.decode_utf8()
6565
.map_err(Error::config)?,
6666
);
6767
}
6868

6969
if let Some(password) = url.password() {
7070
options = options.password(
71-
&*percent_decode_str(password)
71+
&percent_decode_str(password)
7272
.decode_utf8()
7373
.map_err(Error::config)?,
7474
);
@@ -82,7 +82,7 @@ impl FromStr for MssqlConnectOptions {
8282
for (key, value) in url.query_pairs() {
8383
match key.as_ref() {
8484
"instance" => {
85-
options = options.instance(&*value);
85+
options = options.instance(&value);
8686
}
8787
"encrypt" => {
8888
match value.to_lowercase().as_str() {
@@ -104,7 +104,7 @@ impl FromStr for MssqlConnectOptions {
104104
options = options.trust_server_certificate(trust);
105105
}
106106
"hostname_in_certificate" => {
107-
options = options.hostname_in_certificate(&*value);
107+
options = options.hostname_in_certificate(&value);
108108
}
109109
"packet_size" => {
110110
let size = value.parse().map_err(Error::config)?;
@@ -116,11 +116,11 @@ impl FromStr for MssqlConnectOptions {
116116
options = options.client_program_version(value.parse().map_err(Error::config)?)
117117
}
118118
"client_pid" => options = options.client_pid(value.parse().map_err(Error::config)?),
119-
"hostname" => options = options.hostname(&*value),
120-
"app_name" => options = options.app_name(&*value),
121-
"server_name" => options = options.server_name(&*value),
122-
"client_interface_name" => options = options.client_interface_name(&*value),
123-
"language" => options = options.language(&*value),
119+
"hostname" => options = options.hostname(&value),
120+
"app_name" => options = options.app_name(&value),
121+
"server_name" => options = options.server_name(&value),
122+
"client_interface_name" => options = options.client_interface_name(&value),
123+
"language" => options = options.language(&value),
124124
_ => {
125125
return Err(Error::config(MssqlInvalidOption(key.into())));
126126
}

sqlx-core/src/mysql/connection/auth.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ impl AuthPlugin {
1919
) -> Result<Vec<u8>, Error> {
2020
match self {
2121
// https://mariadb.com/kb/en/caching_sha2_password-authentication-plugin/
22-
AuthPlugin::CachingSha2Password => Ok(scramble_sha256(password, nonce)),
22+
AuthPlugin::CachingSha2 => Ok(scramble_sha256(password, nonce)),
2323

24-
AuthPlugin::MySqlNativePassword => Ok(scramble_sha1(password, nonce)),
24+
AuthPlugin::MySqlNative => Ok(scramble_sha1(password, nonce)),
2525

2626
// https://mariadb.com/kb/en/sha256_password-plugin/
27-
AuthPlugin::Sha256Password => encrypt_rsa(stream, 0x01, password, nonce).await,
27+
AuthPlugin::Sha256 => encrypt_rsa(stream, 0x01, password, nonce).await,
2828
}
2929
}
3030

@@ -36,7 +36,7 @@ impl AuthPlugin {
3636
nonce: &Chain<Bytes, Bytes>,
3737
) -> Result<bool, Error> {
3838
match self {
39-
AuthPlugin::CachingSha2Password if packet[0] == 0x01 => {
39+
AuthPlugin::CachingSha2 if packet[0] == 0x01 => {
4040
match packet[1] {
4141
// AUTH_OK
4242
0x03 => Ok(true),

sqlx-core/src/mysql/connection/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'c> Executor<'c> for &'c mut MySqlConnection {
242242
where
243243
'c: 'e,
244244
'q: 'e,
245-
E: Execute<'q, Self::Database>,
245+
E: Execute<'q, Self::Database> + 'e,
246246
{
247247
let mut s = self.fetch_many(query);
248248

0 commit comments

Comments
 (0)