Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d431a67c
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
d431a67c
编写于
11月 09, 2014
作者:
B
Brendan Zabarauskas
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move saturating operator methods into Int
上级
d1eb68e8
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
26 addition
and
42 deletion
+26
-42
src/libcore/iter.rs
src/libcore/iter.rs
+1
-1
src/libcore/num/mod.rs
src/libcore/num/mod.rs
+22
-37
src/libcore/slice.rs
src/libcore/slice.rs
+1
-1
src/libcore/str.rs
src/libcore/str.rs
+1
-1
src/libstd/num/mod.rs
src/libstd/num/mod.rs
+1
-1
src/libtest/lib.rs
src/libtest/lib.rs
+0
-1
未找到文件。
src/libcore/iter.rs
浏览文件 @
d431a67c
...
...
@@ -62,7 +62,7 @@ trait defined in this module. For loops can be viewed as a syntactical expansion
use
cmp
;
use
cmp
::{
PartialEq
,
PartialOrd
,
Ord
};
use
mem
;
use
num
::{
Zero
,
One
,
CheckedAdd
,
CheckedSub
,
Saturating
,
ToPrimitive
,
Int
};
use
num
::{
Zero
,
One
,
CheckedAdd
,
CheckedSub
,
ToPrimitive
,
Int
};
use
ops
::{
Add
,
Mul
,
Sub
};
use
option
::{
Option
,
Some
,
None
};
use
uint
;
...
...
src/libcore/num/mod.rs
浏览文件 @
d431a67c
...
...
@@ -525,6 +525,28 @@ fn to_be(self) -> Self { // or not to be?
fn
to_le
(
self
)
->
Self
{
if
cfg!
(
target_endian
=
"little"
)
{
self
}
else
{
self
.swap_bytes
()
}
}
/// Saturating addition. Returns `self + other`, saturating at the
/// numeric bounds instead of overflowing.
#[inline]
fn
saturating_add
(
self
,
other
:
Self
)
->
Self
{
match
self
.checked_add
(
&
other
)
{
Some
(
x
)
=>
x
,
None
if
other
>=
Zero
::
zero
()
=>
Bounded
::
max_value
(),
None
=>
Bounded
::
min_value
(),
}
}
/// Saturating subtraction. Returns `self - other`, saturating at the
/// numeric bounds instead of overflowing.
#[inline]
fn
saturating_sub
(
self
,
other
:
Self
)
->
Self
{
match
self
.checked_sub
(
&
other
)
{
Some
(
x
)
=>
x
,
None
if
other
>=
Zero
::
zero
()
=>
Bounded
::
min_value
(),
None
=>
Bounded
::
max_value
(),
}
}
}
macro_rules!
int_impl
{
...
...
@@ -1150,43 +1172,6 @@ fn from<N: ToPrimitive>(n: N) -> Option<$T> {
impl_num_cast!
(
f32
,
to_f32
)
impl_num_cast!
(
f64
,
to_f64
)
/// Saturating math operations
pub
trait
Saturating
{
/// Saturating addition operator.
/// Returns a+b, saturating at the numeric bounds instead of overflowing.
fn
saturating_add
(
self
,
v
:
Self
)
->
Self
;
/// Saturating subtraction operator.
/// Returns a-b, saturating at the numeric bounds instead of overflowing.
fn
saturating_sub
(
self
,
v
:
Self
)
->
Self
;
}
impl
<
T
:
CheckedAdd
+
CheckedSub
+
Zero
+
PartialOrd
+
Bounded
>
Saturating
for
T
{
#[inline]
fn
saturating_add
(
self
,
v
:
T
)
->
T
{
match
self
.checked_add
(
&
v
)
{
Some
(
x
)
=>
x
,
None
=>
if
v
>=
Zero
::
zero
()
{
Bounded
::
max_value
()
}
else
{
Bounded
::
min_value
()
}
}
}
#[inline]
fn
saturating_sub
(
self
,
v
:
T
)
->
T
{
match
self
.checked_sub
(
&
v
)
{
Some
(
x
)
=>
x
,
None
=>
if
v
>=
Zero
::
zero
()
{
Bounded
::
min_value
()
}
else
{
Bounded
::
max_value
()
}
}
}
}
/// Performs addition that returns `None` instead of wrapping around on overflow.
pub
trait
CheckedAdd
:
Add
<
Self
,
Self
>
{
/// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
...
...
src/libcore/slice.rs
浏览文件 @
d431a67c
...
...
@@ -40,7 +40,7 @@
use
cmp
;
use
default
::
Default
;
use
iter
::
*
;
use
num
::{
CheckedAdd
,
Saturating
,
div_rem
};
use
num
::{
CheckedAdd
,
Int
,
div_rem
};
use
ops
;
use
option
::{
None
,
Option
,
Some
};
use
ptr
;
...
...
src/libcore/str.rs
浏览文件 @
d431a67c
...
...
@@ -25,7 +25,7 @@
use
iter
::{
DoubleEndedIterator
,
ExactSize
};
use
iter
::
range
;
use
kinds
::
Sized
;
use
num
::{
CheckedMul
,
Saturating
};
use
num
::{
CheckedMul
,
Int
};
use
option
::{
Option
,
None
,
Some
};
use
raw
::
Repr
;
use
slice
::{
mod
,
SlicePrelude
};
...
...
src/libstd/num/mod.rs
浏览文件 @
d431a67c
...
...
@@ -23,7 +23,7 @@
pub
use
core
::
num
::{
Num
,
div_rem
,
Zero
,
zero
,
One
,
one
};
pub
use
core
::
num
::{
Signed
,
abs
,
signum
};
pub
use
core
::
num
::{
Unsigned
,
pow
,
Bounded
};
pub
use
core
::
num
::{
Primitive
,
Int
,
UnsignedInt
,
Saturating
};
pub
use
core
::
num
::{
Primitive
,
Int
,
UnsignedInt
};
pub
use
core
::
num
::{
CheckedAdd
,
CheckedSub
,
CheckedMul
,
CheckedDiv
};
pub
use
core
::
num
::{
cast
,
FromPrimitive
,
NumCast
,
ToPrimitive
};
pub
use
core
::
num
::{
next_power_of_two
,
is_power_of_two
};
...
...
src/libtest/lib.rs
浏览文件 @
d431a67c
...
...
@@ -106,7 +106,6 @@ enum NamePadding { PadNone, PadOnLeft, PadOnRight }
impl
TestDesc
{
fn
padded_name
(
&
self
,
column_count
:
uint
,
align
:
NamePadding
)
->
String
{
use
std
::
num
::
Saturating
;
let
mut
name
=
String
::
from_str
(
self
.name
.as_slice
());
let
fill
=
column_count
.saturating_sub
(
name
.len
());
let
mut
pad
=
" "
.repeat
(
fill
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录