From f5163adc9d716b58bc3678fb8cda09e9d0e9d9f5 Mon Sep 17 00:00:00 2001 From: Kubo Takehiro Date: Tue, 18 Jun 2024 07:27:19 +0900 Subject: [PATCH] Remove ::max_value() to suppress clippy::legacy_numeric_constants warning --- src/aq.rs | 39 +++++++++++++-------------------------- src/connection.rs | 3 +-- src/sql_value.rs | 4 +--- src/util.rs | 29 ----------------------------- 4 files changed, 15 insertions(+), 60 deletions(-) diff --git a/src/aq.rs b/src/aq.rs index 580b6976..cdae6c0f 100644 --- a/src/aq.rs +++ b/src/aq.rs @@ -779,12 +779,7 @@ impl DeqOptions { /// Set the time to wait for a message matching the search /// criteria. pub fn set_wait(&mut self, val: &Duration) -> Result<()> { - let secs = val.as_secs(); - let secs = if secs > u32::max_value().into() { - u32::max_value() - } else { - secs as u32 - }; + let secs = val.as_secs().try_into().unwrap_or(u32::MAX); chkerr!(self.ctxt(), dpiDeqOptions_setWait(self.handle, secs)); Ok(()) } @@ -1077,16 +1072,12 @@ where /// [`MessageState::Waiting`]: MessageState#variant.Waiting /// [`MessageState::Ready`]: MessageState#variant.Ready pub fn set_delay(&mut self, val: &Duration) -> Result<()> { - let secs = val.as_secs(); - if secs > i32::max_value() as u64 { - Err(Error::out_of_range(format!("too long duration {:?}", val))) - } else { - chkerr!( - self.ctxt(), - dpiMsgProps_setDelay(self.handle(), secs as i32) - ); - Ok(()) - } + let secs = val + .as_secs() + .try_into() + .map_err(|_| Error::out_of_range(format!("too long duration {:?}", val)))?; + chkerr!(self.ctxt(), dpiMsgProps_setDelay(self.handle(), secs)); + Ok(()) } /// Sets the name of the queue to which the message is moved if it cannot be @@ -1117,16 +1108,12 @@ where /// [`MessageState::Ready`]: MessageState#variant.Ready /// [`MessageState::Expired`]: MessageState#variant.Expired pub fn set_expiration(&mut self, val: &Duration) -> Result<()> { - let secs = val.as_secs(); - if secs > i32::max_value() as u64 { - Err(Error::out_of_range(format!("too long duration {:?}", val))) - } else { - chkerr!( - self.ctxt(), - dpiMsgProps_setExpiration(self.handle(), secs as i32) - ); - Ok(()) - } + let secs = val + .as_secs() + .try_into() + .map_err(|_| Error::out_of_range(format!("too long duration {:?}", val)))?; + chkerr!(self.ctxt(), dpiMsgProps_setExpiration(self.handle(), secs)); + Ok(()) } /// Sets the id of the message in the last queue that generated this diff --git a/src/connection.rs b/src/connection.rs index 2b674568..cd9c9c9d 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -41,7 +41,6 @@ use crate::sql_type::ObjectTypeInternal; use crate::sql_type::ToSql; use crate::to_odpi_str; use crate::to_rust_str; -use crate::util::duration_to_msecs; use crate::AssertSend; use crate::AssertSync; #[cfg(doc)] @@ -1320,7 +1319,7 @@ impl Connection { /// ``` pub fn set_call_timeout(&self, dur: Option) -> Result<()> { if let Some(dur) = dur { - let msecs = duration_to_msecs(dur).ok_or_else(|| { + let msecs = dur.as_millis().try_into().map_err(|_| { Error::out_of_range(format!( "too long duration {:?}. It must be less than 49.7 days", dur diff --git a/src/sql_value.rs b/src/sql_value.rs index 863940af..8539f262 100644 --- a/src/sql_value.rs +++ b/src/sql_value.rs @@ -59,9 +59,7 @@ use crate::Result; macro_rules! flt_to_int { ($expr:expr, $src_type:ident, $dest_type:ident) => {{ let src_val = $expr; - if $dest_type::min_value() as $src_type <= src_val - && src_val <= $dest_type::max_value() as $src_type - { + if $dest_type::MIN as $src_type <= src_val && src_val <= $dest_type::MAX as $src_type { Ok(src_val as $dest_type) } else { Err(Error::out_of_range(format!( diff --git a/src/util.rs b/src/util.rs index 0c0c2e1e..47f080cc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -22,7 +22,6 @@ use std::ffi::CString; use std::fmt; use std::result; use std::str; -use std::time::Duration; #[cfg_attr(unix, path = "util/unix.rs")] #[cfg_attr(windows, path = "util/windows.rs")] @@ -201,18 +200,6 @@ pub fn write_literal( } } -pub fn duration_to_msecs(dur: Duration) -> Option { - let msecs = dur - .as_secs() - .checked_mul(1000)? - .checked_add(dur.subsec_nanos() as u64 / 1_000_000)?; - if msecs <= u32::max_value() as u64 { - Some(msecs as u32) - } else { - None - } -} - pub fn string_into_c_string(s: String, name: &str) -> Result { CString::new(s).map_err(|err| { Error::invalid_argument(format!("{} cannot contain nul characters", name)).add_source(err) @@ -281,20 +268,4 @@ mod tests { Ok(vec![0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0]) ); } - - #[test] - fn test_duration_to_msecs() { - assert_eq!(duration_to_msecs(Duration::new(0, 0)), Some(0)); - assert_eq!(duration_to_msecs(Duration::from_nanos(999_999)), Some(0)); - assert_eq!(duration_to_msecs(Duration::from_nanos(1_000_000)), Some(1)); - assert_eq!( - duration_to_msecs(Duration::from_millis(u32::max_value() as u64)), - Some(u32::max_value()) - ); - assert_eq!( - duration_to_msecs(Duration::from_millis(u32::max_value() as u64 + 1)), - None - ); - assert_eq!(duration_to_msecs(Duration::new(50 * 24 * 60 * 60, 0)), None); - } }