Skip to content

Commit efcd4da

Browse files
committed
refactor TimeError
- define individual error `SystemTimeError`. - do not re-export std's `SystemTimeError`. - update feature-gates.
1 parent be8e9e2 commit efcd4da

File tree

6 files changed

+57
-68
lines changed

6 files changed

+57
-68
lines changed

DOCS/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The format is based on [Keep a Changelog], and this project adheres to
2828
- new structs:
2929
- `Interval`, `Pnm`.
3030
- new namespaces: `Alloc`, `Arch`, `ByteSearch`, `Char`, `Env`, `Mem`, `Ptr`, `Str`.
31-
- new standalone error types: `FailedErrorConversion`, `DataNotEnough`, `NotImplemented`, `NotSupported`, `ElementNotFound`, `InvalidAxisLength`, `KeyAlreadyExists`, `MismatchedCapacity`, `MismatchedDimensions`, `MismatchedIndices`, `NodeEmpty`, `NodeLinkNotSet`, `NodeLinkNotUnique`, `NotEnoughElements`, `NotEnoughSpace`, `IndexOutOfBounds`, `DataOverflow`, `PartiallyAdded`.
31+
- new standalone error types: `FailedErrorConversion`, `DataNotEnough`, `NotImplemented`, `NotSupported`, `ElementNotFound`, `InvalidAxisLength`, `KeyAlreadyExists`, `MismatchedCapacity`, `MismatchedDimensions`, `MismatchedIndices`, `NodeEmpty`, `NodeLinkNotSet`, `NodeLinkNotUnique`, `NotEnoughElements`, `NotEnoughSpace`, `IndexOutOfBounds`, `DataOverflow`, `PartiallyAdded`, `InvalidChar`, `InvalidUtf8`, `SystemTimeError`.
3232
- new composite error types: `NotAvailable`, `DataNotEnough`, `MismatchedBounds`, `PartialSpace`.
3333
- `False`, `True`, `UnitBi`, `UnitSi`.
3434
- `HasherPengy`.
@@ -84,7 +84,7 @@ The format is based on [Keep a Changelog], and this project adheres to
8484
- `NonZero`, `Saturating`, `Wrapping`, `OsStr`, `OsString`.
8585
- `HashMapEntry` and `BTreeMapEntry`.
8686
- `HashMap` and `BTreeMap` from `std` if `hashbrown` is disabled.
87-
- `FromStr`, `IterChars`, `Utf8Error`.
87+
- `FromStr`, `IterChars`.
8888
- crate items from multiple related modules, like errors and strings.
8989
- new modules: `num::alg`, `sys::sound`, `media::{audio, color, draw, font, image, layout}`, `phys`, `ui`.
9090
- new `sys::os::linux` module and example `linux`.

src/phys/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
// safety
1010
#![cfg_attr(feature = "safe_phys", forbid(unsafe_code))]
1111

12-
// #[cfg(feature = "time")]
13-
// #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "time")))]
1412
pub mod time;
1513

1614
crate::items! { // structural access: _mods, _all, _always
@@ -20,7 +18,6 @@ crate::items! { // structural access: _mods, _all, _always
2018
pub use _always::*;
2119

2220
mod _mods {
23-
// #[cfg(feature = "time")]
2421
pub use super::time::_all::*;
2522
}
2623
pub(super) mod _all {

src/phys/time/error.rs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,53 @@
33
//!
44
//
55

6-
use super::Duration;
7-
8-
#[doc = crate::TAG_RESULT!()]
9-
/// A time-related result.
10-
pub type TimeResult<T> = crate::Result<T, TimeError>;
11-
12-
/// A time-related error.
13-
#[non_exhaustive]
14-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15-
pub enum TimeError {
16-
/// The `Duration` from a [`SystemTimeError`][std::time::SystemTimeError].
17-
///
18-
/// Used to learn how far in the opposite direction a [`SystemTime`][super::SystemTime] lies.
19-
// IMPROVE: generalize.
20-
SystemTimeError(Duration),
6+
use crate::impl_error;
7+
#[cfg(feature = "std")]
8+
use crate::Duration;
219

22-
/// The given value is out of bounds.
23-
OutOfBounds(Option<usize>),
10+
#[cfg(feature = "std")]
11+
use std::time::SystemTimeError as StdSystemTimeError;
12+
13+
impl_error! { individual:
14+
#[cfg(feature = "std")]
15+
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
16+
pub struct SystemTimeError(Duration);
17+
DOC_SYSTEM_TIME_ERROR =
18+
"An error returned from the `duration_since` and `elapsed` methods on `SystemTime`.\n\n
19+
This is basically a replication of `std::time::`[`SystemTimeError`][StdSystemTimeError].",
20+
self+f => write!(f, "SystemTimeError difference: {:?}", self.0)
2421
}
25-
2622
#[cfg(feature = "std")]
2723
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
28-
mod std_impls {
29-
use super::TimeError;
30-
use std::time::SystemTimeError;
31-
32-
impl From<SystemTimeError> for TimeError {
33-
fn from(time: SystemTimeError) -> Self {
34-
TimeError::SystemTimeError(time.duration())
35-
}
24+
impl From<StdSystemTimeError> for SystemTimeError {
25+
fn from(from: StdSystemTimeError) -> Self {
26+
SystemTimeError(from.duration())
3627
}
3728
}
3829

39-
mod core_impls {
40-
use crate::{impl_trait, TimeError};
41-
42-
impl crate::Error for TimeError {}
43-
impl crate::ExtError for TimeError {
44-
type Kind = ();
45-
fn error_eq(&self, other: &Self) -> bool {
46-
self == other
30+
#[cfg(all(feature = "error", feature = "time"))]
31+
pub use full_composite::*;
32+
#[cfg(all(feature = "error", feature = "time"))]
33+
#[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = "error", feature = "time"))))]
34+
mod full_composite {
35+
use super::*;
36+
use crate::{DataOverflow, DOC_DATA_OVERFLOW};
37+
38+
#[doc = crate::TAG_RESULT!()]
39+
/// A text-related result.
40+
pub type TimeResult<T> = crate::Result<T, TimeError>;
41+
42+
impl_error! { composite: fmt(f)
43+
/// A text-related composite error.
44+
#[non_exhaustive]
45+
pub enum TimeError {
46+
DOC_DATA_OVERFLOW:
47+
DataOverflow(o|0: Option<usize>) => DataOverflow(*o),
48+
49+
#[cfg(feature = "std")]
50+
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
51+
DOC_SYSTEM_TIME_ERROR:
52+
SystemTime(d|0: Duration) => SystemTimeError(*d),
4753
}
48-
fn error_kind(&self) -> Self::Kind {}
4954
}
50-
impl_trait! { fmt::Display for TimeError |self, f| {
51-
use TimeError as E;
52-
match self {
53-
E::SystemTimeError(d) => {
54-
write!(f, "SystemTimeError({d:?})")
55-
}
56-
E::OutOfBounds(v) => match v {
57-
Some(v) => write!(f, "The given value {v} is out of bounds."),
58-
None => write!(f, "The given value is out of bounds."),
59-
},
60-
}
61-
}}
6255
}

src/phys/time/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// safety
88
#![cfg_attr(feature = "safe_time", forbid(unsafe_code))]
99

10+
mod error;
1011
mod reexports;
1112

1213
#[cfg(feature = "time")]
1314
crate::items! {
1415
mod calendar;
15-
mod error;
1616
mod fmt;
1717
mod no;
1818
mod split;
@@ -25,12 +25,12 @@ crate::items! { // structural access: _mods, _all, _always
2525
#[allow(unused)] #[doc(hidden)] #[doc(no_inline)]
2626
pub use _always::*;
2727

28-
mod _mods {
29-
pub use super::reexports::*;
28+
mod _mods { #![allow(unused)]
29+
pub use super::{error::*, reexports::*};
3030

3131
#[cfg(feature = "time")]
3232
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "time")))]
33-
pub use super::{calendar::*, error::*, fmt::*, no::*, split::*, unix::*};
33+
pub use super::{calendar::*, fmt::*, no::*, split::*, unix::*};
3434
}
3535
pub(super) mod _all {
3636
#[doc(inline)]

src/phys/time/reexports.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ reexport! { rust: std::time,
2727
doc: "A measurement of the system clock.",
2828
SystemTime
2929
}
30-
reexport! { rust: std::time,
31-
tag: crate::TAG_ERROR!(),
32-
doc: "Error returned from the `duration_since` and `elapsed` methods on [`SystemTime`].",
33-
SystemTimeError
34-
}
30+
// reexport! { rust: std::time,
31+
// tag: crate::TAG_ERROR!(),
32+
// doc: "Error returned from the `duration_since` and `elapsed` methods on [`SystemTime`].",
33+
// SystemTimeError
34+
// }
3535
reexport! { rust: std::time,
3636
doc: "A [`SystemTime`] anchored to “1970-01-01 00:00:00 UTC”.",
3737
UNIX_EPOCH

src/phys/time/unix.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl TryFrom<UnixTimeI64> for UnixTimeU32 {
313313
#[cfg(feature = "std")]
314314
mod std_impls {
315315
#[cfg(feature = "cast")]
316-
use crate::{Cast, TimeError, UnixTimeU32};
316+
use crate::{Cast, DataOverflow, UnixTimeU32};
317317
use crate::{SystemTime, SystemTimeError, UnixTimeI64};
318318

319319
impl TryFrom<SystemTime> for UnixTimeI64 {
@@ -327,16 +327,15 @@ mod std_impls {
327327
}
328328
}
329329

330-
#[cfg(feature = "cast")]
331-
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "cast")))]
330+
#[cfg(all(feature = "cast", feature = "error"))]
331+
#[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = "cast", feature = "error"))))]
332332
impl TryFrom<SystemTime> for UnixTimeU32 {
333-
type Error = TimeError;
333+
type Error = crate::TimeError;
334334

335335
fn try_from(time: SystemTime) -> Result<Self, Self::Error> {
336336
let since = time.duration_since(SystemTime::UNIX_EPOCH)?;
337-
let seconds = u32::try_from(since.as_secs()).map_err(|_| {
338-
TimeError::OutOfBounds(Cast(since.as_secs()).checked_cast_to_usize().ok())
339-
})?;
337+
let seconds = u32::try_from(since.as_secs())
338+
.map_err(|_| DataOverflow(Cast(since.as_secs()).checked_cast_to_usize().ok()))?;
340339
Ok(UnixTimeU32 { seconds })
341340
}
342341
}

0 commit comments

Comments
 (0)