From 7d446c6929f8d7cd30c42596fb693135cad8fdbd Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Thu, 27 Feb 2025 11:37:06 -0700 Subject: [PATCH] Squashed PR #397 --- .github/workflows/test.yml | 2 +- Cargo.toml | 2 +- flake.nix | 2 +- gel-auth/src/handshake/client_auth.rs | 2 +- gel-protocol/src/annotations.rs | 4 +- gel-protocol/src/codec.rs | 2 +- gel-protocol/src/model/time.rs | 2 +- .../src/serialization/decode/raw_scalar.rs | 34 ++++++++--------- gel-protocol/tests/datetime_system.rs | 6 +-- gel-stream/src/common/openssl.rs | 4 +- gel-stream/src/lib.rs | 4 +- gel-stream/src/server/acceptor.rs | 2 +- gel-stream/tests/tls.rs | 4 +- gel-tokio/examples/transaction_errors.rs | 12 ++---- gel-tokio/src/builder.rs | 38 ++++++++----------- gel-tokio/src/raw/connection.rs | 34 +++++++---------- rust-toolchain.toml | 2 +- 17 files changed, 69 insertions(+), 87 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d90ee76..323c2e13 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust_version: [default, 1.75, beta] + rust_version: [default, 1.81, beta] fail-fast: false timeout-minutes: 30 permissions: diff --git a/Cargo.toml b/Cargo.toml index 68906087..950b3a19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ tokio = { version = "1.43" } tracing = { version = "0.1" } [workspace.package] -rust-version = "1.75" # keep in sync with flake.nix +rust-version = "1.81" # keep in sync with flake.nix [workspace.lints.clippy] useless_format = 'allow' diff --git a/flake.nix b/flake.nix index fdbaf1aa..12b943a0 100644 --- a/flake.nix +++ b/flake.nix @@ -55,7 +55,7 @@ devShells.minimum = pkgs.mkShell { buildInputs = [ (fenix_pkgs.toolchainOf { - channel = "1.75"; # keep in sync with ./Cargo.toml rust-version + channel = "1.81"; # keep in sync with ./Cargo.toml rust-version sha256 = "sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8="; }).defaultToolchain ] ++ common; diff --git a/gel-auth/src/handshake/client_auth.rs b/gel-auth/src/handshake/client_auth.rs index e6bee062..f35bf359 100644 --- a/gel-auth/src/handshake/client_auth.rs +++ b/gel-auth/src/handshake/client_auth.rs @@ -83,7 +83,7 @@ impl ClientAuth { } // SCRAM authentication: Handle SCRAM protocol messages. (ClientAuthState::Sasl(tx, env), ClientAuthDrive::ScramResponse(message)) => { - let response = tx.process_message(&message, env)?; + let response = tx.process_message(message, env)?; match response { Some(response) => Ok(ClientAuthResponse::Continue(response)), None => { diff --git a/gel-protocol/src/annotations.rs b/gel-protocol/src/annotations.rs index 713e661b..ccec30cd 100644 --- a/gel-protocol/src/annotations.rs +++ b/gel-protocol/src/annotations.rs @@ -70,8 +70,8 @@ impl std::fmt::Display for Warning { .as_ref() .map(|f| format!("{f}:")) .unwrap_or_default(); - let line = line.clone().unwrap_or(1); - let col = col.clone().unwrap_or(1); + let line = (*line).unwrap_or(1); + let col = (*col).unwrap_or(1); write!(f, "{type} at {filename}{line}:{col} {message}") } diff --git a/gel-protocol/src/codec.rs b/gel-protocol/src/codec.rs index 0777f412..96af55ae 100644 --- a/gel-protocol/src/codec.rs +++ b/gel-protocol/src/codec.rs @@ -426,7 +426,7 @@ pub fn scalar_codec(uuid: &UuidVal) -> Result, CodecError> { POSTGIS_GEOGRAPHY => Ok(Arc::new(PostGisGeography {})), POSTGIS_BOX_2D => Ok(Arc::new(PostGisBox2d {})), POSTGIS_BOX_3D => Ok(Arc::new(PostGisBox3d {})), - _ => errors::UndefinedBaseScalar { uuid: uuid.clone() }.fail()?, + _ => errors::UndefinedBaseScalar { uuid: *uuid }.fail()?, } } diff --git a/gel-protocol/src/model/time.rs b/gel-protocol/src/model/time.rs index 5cfbe908..ce4b6cc1 100644 --- a/gel-protocol/src/model/time.rs +++ b/gel-protocol/src/model/time.rs @@ -329,7 +329,7 @@ impl Duration { if seen.contains(&u) { Err(ParseDurationError::new("specified more than once").pos(start)) } else { - seen.push(u.clone()); + seen.push(u); Ok(u) } }) diff --git a/gel-protocol/src/serialization/decode/raw_scalar.rs b/gel-protocol/src/serialization/decode/raw_scalar.rs index 70d0bee2..85a591a3 100644 --- a/gel-protocol/src/serialization/decode/raw_scalar.rs +++ b/gel-protocol/src/serialization/decode/raw_scalar.rs @@ -145,7 +145,7 @@ impl ScalarArg for Uuid { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Uuid(self.clone())) + Ok(Value::Uuid(*self)) } } @@ -174,7 +174,7 @@ impl ScalarArg for bool { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Bool(self.clone())) + Ok(Value::Bool(*self)) } } @@ -195,7 +195,7 @@ impl ScalarArg for i16 { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Int16(self.clone())) + Ok(Value::Int16(*self)) } } @@ -216,7 +216,7 @@ impl ScalarArg for i32 { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Int32(self.clone())) + Ok(Value::Int32(*self)) } } @@ -244,7 +244,7 @@ impl ScalarArg for i64 { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Int64(self.clone())) + Ok(Value::Int64(*self)) } } @@ -265,7 +265,7 @@ impl ScalarArg for f32 { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Float32(self.clone())) + Ok(Value::Float32(*self)) } } @@ -286,7 +286,7 @@ impl ScalarArg for f64 { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Float64(self.clone())) + Ok(Value::Float64(*self)) } } @@ -338,7 +338,7 @@ impl ScalarArg for ConfigMemory { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::ConfigMemory(self.clone())) + Ok(Value::ConfigMemory(*self)) } } @@ -497,7 +497,7 @@ impl ScalarArg for Duration { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Duration(self.clone())) + Ok(Value::Duration(*self)) } } @@ -534,7 +534,7 @@ impl ScalarArg for RelativeDuration { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::RelativeDuration(self.clone())) + Ok(Value::RelativeDuration(*self)) } } @@ -547,7 +547,7 @@ impl<'t> RawCodec<'t> for SystemTime { impl ScalarArg for SystemTime { fn encode(&self, encoder: &mut Encoder) -> Result<(), Error> { - let val = self.clone().try_into().map_err(|e| { + let val = (*self).try_into().map_err(|e| { ClientEncodingError::with_source(e).context("cannot serialize SystemTime value") })?; codec::encode_datetime(encoder.buf, &val).map_err(ClientEncodingError::with_source) @@ -556,7 +556,7 @@ impl ScalarArg for SystemTime { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - let val = self.clone().try_into().map_err(|e| { + let val = (*self).try_into().map_err(|e| { ClientEncodingError::with_source(e).context("cannot serialize SystemTime value") })?; Ok(Value::Datetime(val)) @@ -578,7 +578,7 @@ impl ScalarArg for Datetime { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::Datetime(self.clone())) + Ok(Value::Datetime(*self)) } } @@ -597,7 +597,7 @@ impl ScalarArg for LocalDatetime { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::LocalDatetime(self.clone())) + Ok(Value::LocalDatetime(*self)) } } @@ -616,7 +616,7 @@ impl ScalarArg for LocalDate { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::LocalDate(self.clone())) + Ok(Value::LocalDate(*self)) } } @@ -641,7 +641,7 @@ impl ScalarArg for DateDuration { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::DateDuration(self.clone())) + Ok(Value::DateDuration(*self)) } } @@ -653,7 +653,7 @@ impl ScalarArg for LocalTime { check_scalar(ctx, pos, Self::uuid(), Self::typename()) } fn to_value(&self) -> Result { - Ok(Value::LocalTime(self.clone())) + Ok(Value::LocalTime(*self)) } } diff --git a/gel-protocol/tests/datetime_system.rs b/gel-protocol/tests/datetime_system.rs index dc130b7b..c6dbeda3 100644 --- a/gel-protocol/tests/datetime_system.rs +++ b/gel-protocol/tests/datetime_system.rs @@ -109,7 +109,7 @@ fn datetime(input: &str, micros: i64, formatted: &str) { // assert_eq!(format!("{:?}", edgedb), formatted); let mut buf = BytesMut::new(); - let val = Value::Datetime(edgedb.clone()); + let val = Value::Datetime(edgedb); codec::Datetime.encode(&mut buf, &val).unwrap(); let serialized_micros = buf.get_i64(); @@ -191,7 +191,7 @@ fn datetime_pre_1970(input: StdDuration, micros: i64, output: StdDuration) { // assert_eq!(format!("{:?}", edgedb), formatted); let mut buf = BytesMut::new(); - let val = Value::Datetime(edgedb.clone()); + let val = Value::Datetime(edgedb); codec::Datetime.encode(&mut buf, &val).unwrap(); let serialized_micros = buf.get_i64(); @@ -253,7 +253,7 @@ fn duration(input: StdDuration, micros: i64, output: StdDuration) { let edgedb: Duration = input.try_into().unwrap(); let mut buf = BytesMut::new(); - let val = Value::Duration(edgedb.clone()); + let val = Value::Duration(edgedb); codec::Duration.encode(&mut buf, &val).unwrap(); let serialized_micros = buf.get_i64(); diff --git a/gel-stream/src/common/openssl.rs b/gel-stream/src/common/openssl.rs index e88233ff..9f3826c7 100644 --- a/gel-stream/src/common/openssl.rs +++ b/gel-stream/src/common/openssl.rs @@ -171,7 +171,7 @@ impl TlsDriver for OpensslDriver { let webpki_roots = WEBPKI_ROOTS.get_or_init(|| { let webpki_roots = webpki_root_certs::TLS_SERVER_ROOT_CERTS; let mut roots = Vec::new(); - for root in webpki_roots.iter().cloned() { + for root in webpki_roots { // Don't expect the roots to fail to load if let Ok(root) = openssl::x509::X509::from_der(root.as_ref()) { roots.push(root); @@ -352,7 +352,7 @@ impl TlsDriver for OpensslDriver { .peer_certificate() .map(|cert| cert.to_der()) .transpose()?; - let cert = cert.map(|cert| CertificateDer::from(cert)); + let cert = cert.map(CertificateDer::from); Ok(( TlsStream(stream), TlsHandshake { diff --git a/gel-stream/src/lib.rs b/gel-stream/src/lib.rs index 57fc0d61..eac3a1f0 100644 --- a/gel-stream/src/lib.rs +++ b/gel-stream/src/lib.rs @@ -117,7 +117,7 @@ impl SslError { _ => None, }, #[cfg(feature = "openssl")] - SslError::OpenSslErrorStack(e) => match e.errors().get(0).map(|err| err.code()) { + SslError::OpenSslErrorStack(e) => match e.errors().first().map(|err| err.code()) { // SSL_R_WRONG_VERSION_NUMBER Some(0xa00010b) => Some(CommonError::InvalidTlsProtocolData), // SSL_R_PACKET_LENGTH_TOO_LONG @@ -130,7 +130,7 @@ impl SslError { openssl_sys::SSL_ERROR_SSL => { match e .ssl_error() - .and_then(|e| e.errors().get(0)) + .and_then(|e| e.errors().first()) .map(|err| err.code()) { // SSL_R_WRONG_VERSION_NUMBER diff --git a/gel-stream/src/server/acceptor.rs b/gel-stream/src/server/acceptor.rs index 5d75dec7..d49aeadf 100644 --- a/gel-stream/src/server/acceptor.rs +++ b/gel-stream/src/server/acceptor.rs @@ -1,6 +1,6 @@ use crate::{ common::tokio_stream::TokioListenerStream, ConnectionError, LocalAddress, ResolvedTarget, - RewindStream, Ssl, SslError, StreamUpgrade, Target, TlsDriver, TlsServerParameterProvider, + RewindStream, Ssl, SslError, StreamUpgrade, TlsDriver, TlsServerParameterProvider, UpgradableStream, }; use futures::{FutureExt, StreamExt}; diff --git a/gel-stream/tests/tls.rs b/gel-stream/tests/tls.rs index bb0e0170..3d64ec77 100644 --- a/gel-stream/tests/tls.rs +++ b/gel-stream/tests/tls.rs @@ -99,7 +99,7 @@ async fn spawn_tls_server( assert_eq!(handshake.sni.as_deref(), expected_hostname.as_deref()); if validate_cert { assert!(handshake.cert.is_some()); - let cert = parse_cert(&handshake.cert.as_ref().unwrap()); + let cert = parse_cert(handshake.cert.as_ref().unwrap()); let subject = cert.subject().to_string(); assert!( subject.to_ascii_lowercase().contains("ssl_user"), @@ -713,6 +713,6 @@ tls_client_test! { fn parse_cert<'a>( cert: &'a rustls_pki_types::CertificateDer<'a>, ) -> x509_parser::prelude::X509Certificate<'a> { - let (_, cert) = x509_parser::parse_x509_certificate(&cert).unwrap(); + let (_, cert) = x509_parser::parse_x509_certificate(cert).unwrap(); cert } diff --git a/gel-tokio/examples/transaction_errors.rs b/gel-tokio/examples/transaction_errors.rs index 252a11a6..9380940a 100644 --- a/gel-tokio/examples/transaction_errors.rs +++ b/gel-tokio/examples/transaction_errors.rs @@ -8,19 +8,15 @@ use gel_errors::{ErrorKind, UserError}; struct CounterError; fn check_val0(val: i64) -> anyhow::Result<()> { - if val % 3 == 0 { - if rng().random_bool(0.9) { - Err(CounterError)?; - } + if val % 3 == 0 && rng().random_bool(0.9) { + Err(CounterError)?; } Ok(()) } fn check_val1(val: i64) -> Result<(), CounterError> { - if val % 3 == 1 { - if rng().random_bool(0.1) { - Err(CounterError)?; - } + if val % 3 == 1 && rng().random_bool(0.1) { + Err(CounterError)?; } Ok(()) } diff --git a/gel-tokio/src/builder.rs b/gel-tokio/src/builder.rs index c0950b38..e4d76d1d 100644 --- a/gel-tokio/src/builder.rs +++ b/gel-tokio/src/builder.rs @@ -230,7 +230,7 @@ impl CertCheck { pub fn new_fn> + Send + Sync + 'static>(function: impl for <'a> Fn(&'a [u8]) -> F + Send + Sync + 'static) -> Self { let function = Arc::new(move |cert: &'_[u8]| { let fut = function(cert); - Box::pin(async move { fut.await }) as _ + Box::pin(fut) as _ }); Self { function } @@ -1404,21 +1404,19 @@ impl Builder { instance )) })?; - if matches!(instance, InstanceName::Cloud { .. }) { - if cfg.secret_key.is_none() && cfg.cloud_profile.is_none() { - let path = stash_path.join("cloud-profile"); - let profile = fs::read_to_string(&path) - .await - .map_err(|e| { - ClientError::with_source(e).context(format!( - "error reading project settings {:?}: {:?}", - project_dir, path - )) - })? - .trim() - .into(); - cfg.cloud_profile = Some(profile); - } + if matches!(instance, InstanceName::Cloud { .. }) && cfg.secret_key.is_none() && cfg.cloud_profile.is_none() { + let path = stash_path.join("cloud-profile"); + let profile = fs::read_to_string(&path) + .await + .map_err(|e| { + ClientError::with_source(e).context(format!( + "error reading project settings {:?}: {:?}", + project_dir, path + )) + })? + .trim() + .into(); + cfg.cloud_profile = Some(profile); } read_instance(cfg, &instance).await?; let path = stash_path.join("database"); @@ -1941,7 +1939,7 @@ impl Config { tls.root_cert = TlsCert::Webpki; match &self.0.pem_certificates { Some(pem_certificates) => { - tls.root_cert = TlsCert::Custom(read_root_cert_pem(&pem_certificates)?); + tls.root_cert = TlsCert::Custom(read_root_cert_pem(pem_certificates)?); } None => { if let Some(cloud_certs) = self.0.cloud_certs { @@ -1967,11 +1965,7 @@ impl Config { Some(Cow::from(host)) } } else { - if let Some(host) = self.0.address.host() { - Some(Cow::from(host.to_string())) - } else { - None - } + self.0.address.host().map(|host| Cow::from(host.to_string())) } } else { None diff --git a/gel-tokio/src/raw/connection.rs b/gel-tokio/src/raw/connection.rs index 3cde1434..c111a087 100644 --- a/gel-tokio/src/raw/connection.rs +++ b/gel-tokio/src/raw/connection.rs @@ -288,16 +288,14 @@ async fn connect2( // Allow plaintext reconnection if and only if ClientSecurity is InsecureDevMode and // the server replied with something that looks like TLS handshake failure. if let Err(ConnectionError::SslError(e)) = &res { - if e.common_error() == Some(CommonError::InvalidTlsProtocolData) { - if cfg.0.client_security == ClientSecurity::InsecureDevMode { - target.try_remove_tls(); - warn!("TLS handshake failed, trying again without TLS"); - *warned = true; - - let mut connector = Connector::new(target.clone()).map_err(ClientConnectionError::with_source)?; - connector.set_keepalive(cfg.0.tcp_keepalive); - res = connector.connect().await; - } + if e.common_error() == Some(CommonError::InvalidTlsProtocolData) && cfg.0.client_security == ClientSecurity::InsecureDevMode { + target.try_remove_tls(); + warn!("TLS handshake failed, trying again without TLS"); + *warned = true; + + let mut connector = Connector::new(target.clone()).map_err(ClientConnectionError::with_source)?; + connector.set_keepalive(cfg.0.tcp_keepalive); + res = connector.connect().await; } } @@ -310,7 +308,7 @@ async fn connect4(cfg: &Config, mut stream: gel_stream::RawStream) -> Result Result { - resp = client_auth.drive(ClientAuthDrive::ScramResponse(&data)).map_err(AuthenticationError::with_source)?; + resp = client_auth.drive(ClientAuthDrive::ScramResponse(data)).map_err(AuthenticationError::with_source)?; } ServerMessage::Authentication(Authentication::SaslFinal { ref data }) => { - resp = client_auth.drive(ClientAuthDrive::ScramResponse(&data)).map_err(AuthenticationError::with_source)?; + resp = client_auth.drive(ClientAuthDrive::ScramResponse(data)).map_err(AuthenticationError::with_source)?; } ServerMessage::ErrorResponse(err) => { return Err(err.into()); @@ -414,9 +412,7 @@ async fn connect4(cfg: &Config, mut stream: gel_stream::RawStream) -> Result { - return Err(ProtocolError::with_message(format!( - "Unexpected authentication response", - ))); + return Err(ProtocolError::with_message("Unexpected authentication response".to_string())); } ClientAuthResponse::Complete => { break; @@ -708,11 +704,7 @@ fn is_temporary(e: &Error) -> bool { let mut e: &dyn std::error::Error = &e; while let Some(src) = e.source() { if let Some(io_err) = src.downcast_ref::() { - if is_io_error_temporary(io_err) { - return true; - } else { - return false; - } + return is_io_error_temporary(io_err) } e = src; } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index e65ffcbf..b0295e09 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.80" +channel = "1.81" components = ["rustc", "cargo", "rust-std", "rust-src", "clippy", "rustfmt", "rust-analyzer"]