Skip to content

Commit

Permalink
Rename OutOfRangeError to ConversionRangeError
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 12, 2019
1 parent 4c43ec3 commit 20b27cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/duration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "std")]
use crate::Instant;
use crate::{
NumberExt, OutOfRangeError,
NumberExt, ConversionRangeError,
Sign::{self, Negative, Positive, Zero},
};
use core::{
Expand Down Expand Up @@ -843,7 +843,7 @@ impl Duration {
since = "0.2.0",
note = "Use `Duration::try_from(value)` or `value.try_into()`"
)]
pub fn from_std(std: StdDuration) -> Result<Self, OutOfRangeError> {
pub fn from_std(std: StdDuration) -> Result<Self, ConversionRangeError> {
std.try_into()
}

Expand All @@ -853,35 +853,35 @@ impl Duration {
since = "0.2.0",
note = "Use `std::time::Duration::try_from(value)` or `value.try_into()`"
)]
pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
pub fn to_std(&self) -> Result<StdDuration, ConversionRangeError> {
if self.sign.is_negative() {
Err(OutOfRangeError)
Err(ConversionRangeError)
} else {
Ok(self.std)
}
}
}

impl TryFrom<StdDuration> for Duration {
type Error = OutOfRangeError;
type Error = ConversionRangeError;

#[inline(always)]
fn try_from(original: StdDuration) -> Result<Self, OutOfRangeError> {
fn try_from(original: StdDuration) -> Result<Self, ConversionRangeError> {
some_if_in_range!(Self {
sign: original.as_nanos().sign(),
std: original,
})
.ok_or(OutOfRangeError)
.ok_or(ConversionRangeError)
}
}

impl TryFrom<Duration> for StdDuration {
type Error = OutOfRangeError;
type Error = ConversionRangeError;

#[inline(always)]
fn try_from(duration: Duration) -> Result<Self, OutOfRangeError> {
fn try_from(duration: Duration) -> Result<Self, ConversionRangeError> {
if duration.sign.is_negative() {
Err(OutOfRangeError)
Err(ConversionRangeError)
} else {
Ok(duration.std)
}
Expand Down
17 changes: 4 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,28 +315,19 @@ mod no_std_prelude {

/// An error type indicating that a conversion failed because the target type
/// could not store the initial value.
///
/// ```rust
/// # use time::{Duration, OutOfRangeError};
/// # use core::time::Duration as StdDuration;
/// # use core::{any::Any, convert::TryFrom};
/// // "Construct" an `OutOfRangeError`.
/// let error = StdDuration::try_from(Duration::seconds(-1)).unwrap_err();
/// assert!(Any::is::<OutOfRangeError>(&error));
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutOfRangeError;
pub struct ConversionRangeError;

impl fmt::Display for OutOfRangeError {
impl fmt::Display for ConversionRangeError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("Source value is out of range for the target type")
}
}

#[cfg(feature = "std")]
impl std::error::Error for OutOfRangeError {}
impl std::error::Error for ConversionRangeError {}

#[cfg(test)]
mod test {
Expand All @@ -347,7 +338,7 @@ mod test {
#[test]
fn out_of_range_error_format() {
assert_eq!(
OutOfRangeError.to_string(),
ComponentRangeError.to_string(),
"Source value is out of range for the target type",
);
}
Expand Down

0 comments on commit 20b27cb

Please sign in to comment.