Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c2c1ca07
R
Rust
项目概览
int
/
Rust
接近 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c2c1ca07
编写于
6月 14, 2021
作者:
M
mbartlett21
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add functions `Duration::try_from_secs_{f32, f64}`
This also adds the error type used, `FromSecsError` and its `impl`s.
上级
26c2d1f4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
132 addition
and
0 deletion
+132
-0
library/core/src/time.rs
library/core/src/time.rs
+128
-0
library/std/src/error.rs
library/std/src/error.rs
+3
-0
library/std/src/lib.rs
library/std/src/lib.rs
+1
-0
未找到文件。
library/core/src/time.rs
浏览文件 @
c2c1ca07
...
...
@@ -697,6 +697,44 @@ pub const fn from_secs_f64(secs: f64) -> Duration {
}
}
/// The checked version of [`from_secs_f64`].
///
/// [`from_secs_f64`]: Duration::from_secs_f64
///
/// This constructor will return an `Err` if `secs` is not finite, negative or overflows `Duration`.
///
/// # Examples
/// ```
/// #![feature(duration_checked_float)]
///
/// use std::time::Duration;
///
/// let dur = Duration::try_from_secs_f64(2.7);
/// assert_eq!(dur, Ok(Duration::new(2, 700_000_000)));
///
/// let negative = Duration::try_from_secs_f64(-5.0);
/// assert!(negative.is_err());
/// ```
#[unstable(feature
=
"duration_checked_float"
,
issue
=
"83400"
)]
#[inline]
pub
const
fn
try_from_secs_f64
(
secs
:
f64
)
->
Result
<
Duration
,
FromSecsError
>
{
const
MAX_NANOS_F64
:
f64
=
((
u64
::
MAX
as
u128
+
1
)
*
(
NANOS_PER_SEC
as
u128
))
as
f64
;
let
nanos
=
secs
*
(
NANOS_PER_SEC
as
f64
);
if
!
nanos
.is_finite
()
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
NonFinite
})
}
else
if
nanos
>=
MAX_NANOS_F64
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
Overflow
})
}
else
if
nanos
<
0.0
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
Underflow
})
}
else
{
let
nanos
=
nanos
as
u128
;
Ok
(
Duration
{
secs
:
(
nanos
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
})
}
}
/// Creates a new `Duration` from the specified number of seconds represented
/// as `f32`.
///
...
...
@@ -732,6 +770,44 @@ pub const fn from_secs_f32(secs: f32) -> Duration {
}
}
/// The checked version of [`from_secs_f32`].
///
/// [`from_secs_f32`]: Duration::from_secs_f32
///
/// This constructor will return an `Err` if `secs` is not finite, negative or overflows `Duration`.
///
/// # Examples
/// ```
/// #![feature(duration_checked_float)]
///
/// use std::time::Duration;
///
/// let dur = Duration::try_from_secs_f32(2.7);
/// assert_eq!(dur, Ok(Duration::new(2, 700_000_000)));
///
/// let negative = Duration::try_from_secs_f32(-5.0);
/// assert!(negative.is_err());
/// ```
#[unstable(feature
=
"duration_checked_float"
,
issue
=
"83400"
)]
#[inline]
pub
const
fn
try_from_secs_f32
(
secs
:
f32
)
->
Result
<
Duration
,
FromSecsError
>
{
const
MAX_NANOS_F32
:
f32
=
((
u64
::
MAX
as
u128
+
1
)
*
(
NANOS_PER_SEC
as
u128
))
as
f32
;
let
nanos
=
secs
*
(
NANOS_PER_SEC
as
f32
);
if
!
nanos
.is_finite
()
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
NonFinite
})
}
else
if
nanos
>=
MAX_NANOS_F32
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
Overflow
})
}
else
if
nanos
<
0.0
{
Err
(
FromSecsError
{
kind
:
FromSecsErrorKind
::
Underflow
})
}
else
{
let
nanos
=
nanos
as
u128
;
Ok
(
Duration
{
secs
:
(
nanos
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
})
}
}
/// Multiplies `Duration` by `f64`.
///
/// # Panics
...
...
@@ -1081,3 +1157,55 @@ fn fmt_decimal(
}
}
}
/// An error which can be returned when converting a floating-point value of seconds
/// into a [`Duration`].
///
/// This error is used as the error type for [`Duration::try_from_secs_f32`] and
/// [`Duration::try_from_secs_f64`].
///
/// # Example
///
/// ```
/// #![feature(duration_checked_float)]
///
/// use std::time::Duration;
///
/// if let Err(e) = Duration::try_from_secs_f32(-1.0) {
/// println!("Failed conversion to Duration: {}", e);
/// }
/// ```
#[derive(Debug,
Clone,
PartialEq,
Eq)]
#[unstable(feature
=
"duration_checked_float"
,
issue
=
"83400"
)]
pub
struct
FromSecsError
{
kind
:
FromSecsErrorKind
,
}
impl
FromSecsError
{
const
fn
description
(
&
self
)
->
&
'static
str
{
match
self
.kind
{
FromSecsErrorKind
::
NonFinite
=>
{
"got non-finite value when converting float to duration"
}
FromSecsErrorKind
::
Overflow
=>
"overflow when converting float to duration"
,
FromSecsErrorKind
::
Underflow
=>
"underflow when converting float to duration"
,
}
}
}
#[unstable(feature
=
"duration_checked_float"
,
issue
=
"83400"
)]
impl
fmt
::
Display
for
FromSecsError
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
<
'_
>
)
->
fmt
::
Result
{
fmt
::
Display
::
fmt
(
self
.description
(),
f
)
}
}
#[derive(Debug,
Clone,
PartialEq,
Eq)]
enum
FromSecsErrorKind
{
// Value is not a finite value (either infinity or NaN).
NonFinite
,
// Value is too large to store in a `Duration`.
Overflow
,
// Value is less than `0.0`.
Underflow
,
}
library/std/src/error.rs
浏览文件 @
c2c1ca07
...
...
@@ -529,6 +529,9 @@ fn description(&self) -> &str {
#[unstable(feature
=
"try_reserve"
,
reason
=
"new API"
,
issue
=
"48043"
)]
impl
Error
for
alloc
::
collections
::
TryReserveError
{}
#[unstable(feature
=
"duration_checked_float"
,
issue
=
"83400"
)]
impl
Error
for
core
::
time
::
FromSecsError
{}
// Copied from `any.rs`.
impl
dyn
Error
+
'static
{
/// Returns `true` if the boxed type is the same as `T`
...
...
library/std/src/lib.rs
浏览文件 @
c2c1ca07
...
...
@@ -256,6 +256,7 @@
#![feature(doc_masked)]
#![feature(doc_spotlight)]
#![feature(dropck_eyepatch)]
#![feature(duration_checked_float)]
#![feature(duration_constants)]
#![feature(duration_zero)]
#![feature(exact_size_is_empty)]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录