Skip to content

Commit

Permalink
Remove <integer>::max_value() to suppress clippy::legacy_numeric_cons…
Browse files Browse the repository at this point in the history
…tants warning
  • Loading branch information
kubo committed Jun 17, 2024
1 parent 1b5051a commit f5163ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 60 deletions.
39 changes: 13 additions & 26 deletions src/aq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -1320,7 +1319,7 @@ impl Connection {
/// ```
pub fn set_call_timeout(&self, dur: Option<Duration>) -> 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
Expand Down
4 changes: 1 addition & 3 deletions src/sql_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
29 changes: 0 additions & 29 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -201,18 +200,6 @@ pub fn write_literal(
}
}

pub fn duration_to_msecs(dur: Duration) -> Option<u32> {
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> {
CString::new(s).map_err(|err| {
Error::invalid_argument(format!("{} cannot contain nul characters", name)).add_source(err)
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f5163ad

Please sign in to comment.