Skip to content

Commit

Permalink
Restore Time::try_from.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed May 7, 2021
1 parent 5d5c710 commit de7fb02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ all-features = true
name = "webpki"

[features]
# TODO: In the next release, make this non-default.
default = ["std"]
alloc = ["ring/alloc"]
std = ["alloc"]
# TODO: In the next release, remove this.
trust_anchor_util = ["std"]

[dependencies]
ring = { version = "0.16.19", default-features = false }
Expand Down
15 changes: 12 additions & 3 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
pub struct Time(u64);

impl Time {
/// Deprecated. Use `TryFrom::try_from`.
#[cfg(feature = "std")]
// Soft deprecation. #[deprecated(note = "Use TryFrom::try_from")]
pub fn try_from(time: std::time::SystemTime) -> Result<Self, ring::error::Unspecified> {
core::convert::TryFrom::try_from(time)
}

/// Create a `webpki::Time` from a unix timestamp.
///
/// It is usually better to use the less error-prone
Expand All @@ -37,7 +44,8 @@ impl Time {

#[cfg(feature = "std")]
impl core::convert::TryFrom<std::time::SystemTime> for Time {
type Error = std::time::SystemTimeError;
// TODO: In the next release, make this `std::time::SystemTimeError`.
type Error = ring::error::Unspecified;

/// Create a `webpki::Time` from a `std::time::SystemTime`.
///
Expand All @@ -50,9 +58,9 @@ impl core::convert::TryFrom<std::time::SystemTime> for Time {
/// # extern crate webpki;
/// #
/// #![cfg(feature = "std")]
/// use std::{convert::TryFrom, time::{SystemTime, SystemTimeError}};
/// use std::{convert::TryFrom, time::SystemTime};
///
/// # fn foo() -> Result<(), SystemTimeError> {
/// # fn foo() -> Result<(), ring::error::Unspecified> {
/// let time = webpki::Time::try_from(SystemTime::now())?;
/// # Ok(())
/// # }
Expand All @@ -61,5 +69,6 @@ impl core::convert::TryFrom<std::time::SystemTime> for Time {
value
.duration_since(std::time::UNIX_EPOCH)
.map(|d| Self::from_seconds_since_unix_epoch(d.as_secs()))
.map_err(|_| ring::error::Unspecified)
}
}

0 comments on commit de7fb02

Please sign in to comment.