Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0673417d
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,发现更多精彩内容 >>
未验证
提交
0673417d
编写于
9月 12, 2018
作者:
A
Artyom Pavlov
提交者:
GitHub
9月 12, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move float ops to unstable inherent methods
上级
d48a649a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
83 addition
and
84 deletion
+83
-84
src/libcore/time.rs
src/libcore/time.rs
+83
-84
未找到文件。
src/libcore/time.rs
浏览文件 @
0673417d
...
...
@@ -459,6 +459,88 @@ pub fn checked_div(self, rhs: u32) -> Option<Duration> {
None
}
}
/// Multiply `Duration` by `f64`.
///
/// # Examples
/// ```
/// let dur = Duration::new(2, 700_000_000);
/// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
/// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
/// ```
#[unstable(feature
=
"duration_float_ops"
,
reason
=
"duration/floats operations are unstabe"
,
issue
=
"0"
)]
#[inline]
pub
fn
mul_f64
(
self
,
rhs
:
f64
)
->
Duration
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos_f64
=
rhs
*
(
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
));
if
!
nanos_f64
.is_finite
()
{
panic!
(
"got non-finite value when multiplying duration by float"
);
}
if
nanos_f64
>
MAX_NANOS_F64
{
panic!
(
"overflow when multiplying duration by float"
);
}
if
nanos_f64
<
0.0
{
panic!
(
"underflow when multiplying duration by float"
);
}
let
nanos_u128
=
nanos_f64
as
u128
;
Duration
{
secs
:
(
nanos_u128
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos_u128
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
}
}
/// Divide `Duration` by `f64`.
///
/// # Examples
/// ```
/// let dur = Duration::new(2, 700_000_000);
/// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
/// // note that truncation is used, not rounding
/// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
/// ```
#[unstable(feature
=
"duration_float_ops"
,
reason
=
"duration/floats operations are unstabe"
,
issue
=
"0"
)]
#[inline]
pub
fn
div_f64
(
self
,
rhs
:
f64
)
->
Duration
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos_f64
=
(
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
))
/
rhs
;
if
!
nanos_f64
.is_finite
()
{
panic!
(
"got non-finite value when dividing duration by float"
);
}
if
nanos_f64
>
MAX_NANOS_F64
{
panic!
(
"overflow when dividing duration by float"
);
}
if
nanos_f64
<
0.0
{
panic!
(
"underflow when multiplying duration by float"
);
}
let
nanos_u128
=
nanos_f64
as
u128
;
Duration
{
secs
:
(
nanos_u128
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos_u128
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
}
}
/// Divide `Duration` by `Duration` and return `f64`.
///
/// # Examples
/// ```
/// let dur1 = Duration::new(2, 700_000_000);
/// let dur2 = Duration::new(5, 400_000_000);
/// assert_eq!(dur1.div_duration(dur2), 0.5);
/// ```
#[unstable(feature
=
"duration_float_ops"
,
reason
=
"duration/floats operations are unstabe"
,
issue
=
"0"
)]
#[inline]
pub
fn
div_duration
(
self
,
rhs
:
Duration
)
->
f64
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos1
=
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
);
let
nanos2
=
NPS
*
(
rhs
.secs
as
f64
)
+
(
rhs
.nanos
as
f64
);
nanos1
/
nanos2
}
}
#[stable(feature
=
"duration"
,
since
=
"1.3.0"
)]
...
...
@@ -502,7 +584,7 @@ fn mul(self, rhs: u32) -> Duration {
}
}
#[stable(feature
=
"
duration_mul_div_extras
"
,
since
=
"1.30.0"
)]
#[stable(feature
=
"
symmetric_u32_duration_mul
"
,
since
=
"1.30.0"
)]
impl
Mul
<
Duration
>
for
u32
{
type
Output
=
Duration
;
...
...
@@ -511,39 +593,6 @@ fn mul(self, rhs: Duration) -> Duration {
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
Mul
<
f64
>
for
Duration
{
type
Output
=
Duration
;
fn
mul
(
self
,
rhs
:
f64
)
->
Duration
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos_f64
=
rhs
*
(
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
));
if
!
nanos_f64
.is_finite
()
{
panic!
(
"got non-finite value when multiplying duration by float"
);
}
if
nanos_f64
>
MAX_NANOS_F64
{
panic!
(
"overflow when multiplying duration by float"
);
}
if
nanos_f64
<
0.0
{
panic!
(
"underflow when multiplying duration by float"
);
}
let
nanos_u128
=
nanos_f64
as
u128
;
Duration
{
secs
:
(
nanos_u128
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos_u128
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
}
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
Mul
<
Duration
>
for
f64
{
type
Output
=
Duration
;
fn
mul
(
self
,
rhs
:
Duration
)
->
Duration
{
rhs
*
self
}
}
#[stable(feature
=
"time_augmented_assignment"
,
since
=
"1.9.0"
)]
impl
MulAssign
<
u32
>
for
Duration
{
fn
mul_assign
(
&
mut
self
,
rhs
:
u32
)
{
...
...
@@ -551,13 +600,6 @@ fn mul_assign(&mut self, rhs: u32) {
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
MulAssign
<
f64
>
for
Duration
{
fn
mul_assign
(
&
mut
self
,
rhs
:
f64
)
{
*
self
=
*
self
*
rhs
;
}
}
#[stable(feature
=
"duration"
,
since
=
"1.3.0"
)]
impl
Div
<
u32
>
for
Duration
{
type
Output
=
Duration
;
...
...
@@ -567,42 +609,6 @@ fn div(self, rhs: u32) -> Duration {
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
Div
<
f64
>
for
Duration
{
type
Output
=
Duration
;
fn
div
(
self
,
rhs
:
f64
)
->
Duration
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos_f64
=
(
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
))
/
rhs
;
if
!
nanos_f64
.is_finite
()
{
panic!
(
"got non-finite value when dividing duration by float"
);
}
if
nanos_f64
>
MAX_NANOS_F64
{
panic!
(
"overflow when dividing duration by float"
);
}
if
nanos_f64
<
0.0
{
panic!
(
"underflow when multiplying duration by float"
);
}
let
nanos_u128
=
nanos_f64
as
u128
;
Duration
{
secs
:
(
nanos_u128
/
(
NANOS_PER_SEC
as
u128
))
as
u64
,
nanos
:
(
nanos_u128
%
(
NANOS_PER_SEC
as
u128
))
as
u32
,
}
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
Div
<
Duration
>
for
Duration
{
type
Output
=
f64
;
fn
div
(
self
,
rhs
:
Duration
)
->
f64
{
const
NPS
:
f64
=
NANOS_PER_SEC
as
f64
;
let
nanos1
=
NPS
*
(
self
.secs
as
f64
)
+
(
self
.nanos
as
f64
);
let
nanos2
=
NPS
*
(
rhs
.secs
as
f64
)
+
(
rhs
.nanos
as
f64
);
nanos1
/
nanos2
}
}
#[stable(feature
=
"time_augmented_assignment"
,
since
=
"1.9.0"
)]
impl
DivAssign
<
u32
>
for
Duration
{
fn
div_assign
(
&
mut
self
,
rhs
:
u32
)
{
...
...
@@ -610,13 +616,6 @@ fn div_assign(&mut self, rhs: u32) {
}
}
#[stable(feature
=
"duration_mul_div_extras"
,
since
=
"1.30.0"
)]
impl
DivAssign
<
f64
>
for
Duration
{
fn
div_assign
(
&
mut
self
,
rhs
:
f64
)
{
*
self
=
*
self
/
rhs
;
}
}
macro_rules!
sum_durations
{
(
$iter:expr
)
=>
{{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录