Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d9d1dfc1
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,发现更多精彩内容 >>
提交
d9d1dfc1
编写于
9月 15, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: Replace num::IntConvertible with {To,From}Primitive
上级
17548378
变更
14
展开全部
隐藏空白更改
内联
并排
Showing
14 changed file
with
582 addition
and
323 deletion
+582
-323
src/libextra/num/bigint.rs
src/libextra/num/bigint.rs
+212
-211
src/libextra/num/rational.rs
src/libextra/num/rational.rs
+3
-3
src/librustc/middle/trans/debuginfo.rs
src/librustc/middle/trans/debuginfo.rs
+1
-1
src/librustc/util/ppaux.rs
src/librustc/util/ppaux.rs
+1
-1
src/libstd/num/num.rs
src/libstd/num/num.rs
+341
-83
src/libstd/num/strconv.rs
src/libstd/num/strconv.rs
+13
-13
src/libstd/prelude.rs
src/libstd/prelude.rs
+1
-1
src/libstd/rand/mod.rs
src/libstd/rand/mod.rs
+2
-2
src/test/run-pass/numeric-method-autoexport.rs
src/test/run-pass/numeric-method-autoexport.rs
+2
-2
src/test/run-pass/trait-inheritance-num.rs
src/test/run-pass/trait-inheritance-num.rs
+2
-2
src/test/run-pass/trait-inheritance-num0.rs
src/test/run-pass/trait-inheritance-num0.rs
+1
-1
src/test/run-pass/trait-inheritance-num1.rs
src/test/run-pass/trait-inheritance-num1.rs
+1
-1
src/test/run-pass/trait-inheritance-num3.rs
src/test/run-pass/trait-inheritance-num3.rs
+1
-1
src/test/run-pass/trait-inheritance-num5.rs
src/test/run-pass/trait-inheritance-num5.rs
+1
-1
未找到文件。
src/libextra/num/bigint.rs
浏览文件 @
d9d1dfc1
此差异已折叠。
点击以展开。
src/libextra/num/rational.rs
浏览文件 @
d9d1dfc1
...
...
@@ -306,7 +306,7 @@ fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
mod
test
{
use
super
::
*
;
use
std
::
num
::{
Zero
,
One
,
FromStrRadix
,
IntConvertibl
e
};
use
std
::
num
::{
Zero
,
One
,
FromStrRadix
,
FromPrimitiv
e
};
use
std
::
from_str
::
FromStr
;
pub
static
_0
:
Rational
=
Ratio
{
numer
:
0
,
denom
:
1
};
...
...
@@ -318,8 +318,8 @@ mod test {
pub
fn
to_big
(
n
:
Rational
)
->
BigRational
{
Ratio
::
new
(
IntConvertible
::
from_int
(
n
.numer
),
IntConvertible
::
from_int
(
n
.denom
)
FromPrimitive
::
from_int
(
n
.numer
)
.unwrap
(
),
FromPrimitive
::
from_int
(
n
.denom
)
.unwrap
(
)
)
}
...
...
src/librustc/middle/trans/debuginfo.rs
浏览文件 @
d9d1dfc1
...
...
@@ -107,7 +107,7 @@ struct List {
use
std
::
libc
::{
c_uint
,
c_ulonglong
,
c_longlong
};
use
std
::
ptr
;
use
std
::
vec
;
use
syntax
::
codemap
::
Span
;
use
syntax
::
codemap
::
{
Span
,
Pos
}
;
use
syntax
::{
ast
,
codemap
,
ast_util
,
ast_map
,
opt_vec
,
visit
};
use
syntax
::
parse
::
token
;
use
syntax
::
parse
::
token
::
special_idents
;
...
...
src/librustc/util/ppaux.rs
浏览文件 @
d9d1dfc1
...
...
@@ -26,7 +26,7 @@
use
middle
::
typeck
;
use
syntax
::
abi
::
AbiSet
;
use
syntax
::
ast_map
;
use
syntax
::
codemap
::
Span
;
use
syntax
::
codemap
::
{
Span
,
Pos
}
;
use
syntax
::
parse
::
token
;
use
syntax
::
print
::
pprust
;
use
syntax
::{
ast
,
ast_util
};
...
...
src/libstd/num/num.rs
浏览文件 @
d9d1dfc1
...
...
@@ -32,11 +32,6 @@ pub trait Num: Eq + Zero + One
+
Div
<
Self
,
Self
>
+
Rem
<
Self
,
Self
>
{}
pub
trait
IntConvertible
{
fn
to_int
(
&
self
)
->
int
;
fn
from_int
(
n
:
int
)
->
Self
;
}
pub
trait
Orderable
:
Ord
{
// These should be methods on `Ord`, with overridable default implementations. We don't want
// to encumber all implementors of Ord by requiring them to implement these functions, but at
...
...
@@ -353,6 +348,298 @@ pub trait Float: Real
#[inline(always)]
pub
fn
ln_1p
<
T
:
Float
>
(
value
:
T
)
->
T
{
value
.ln_1p
()
}
#[inline(always)]
pub
fn
mul_add
<
T
:
Float
>
(
a
:
T
,
b
:
T
,
c
:
T
)
->
T
{
a
.mul_add
(
b
,
c
)
}
/// A generic trait for converting a value to a number.
pub
trait
ToPrimitive
{
/// Converts the value of `self` to an `int`.
fn
to_int
(
&
self
)
->
Option
<
int
>
;
/// Converts the value of `self` to an `i8`.
#[inline]
fn
to_i8
(
&
self
)
->
Option
<
i8
>
{
// XXX: Check for range.
self
.to_int
()
.and_then
(|
x
|
Some
(
x
as
i8
))
}
/// Converts the value of `self` to an `i16`.
#[inline]
fn
to_i16
(
&
self
)
->
Option
<
i16
>
{
// XXX: Check for range.
self
.to_int
()
.and_then
(|
x
|
Some
(
x
as
i16
))
}
/// Converts the value of `self` to an `i32`.
#[inline]
fn
to_i32
(
&
self
)
->
Option
<
i32
>
{
// XXX: Check for range.
self
.to_int
()
.and_then
(|
x
|
Some
(
x
as
i32
))
}
/// Converts the value of `self` to an `i64`.
#[inline]
fn
to_i64
(
&
self
)
->
Option
<
i64
>
{
// XXX: Check for range.
self
.to_int
()
.and_then
(|
x
|
Some
(
x
as
i64
))
}
/// Converts the value of `self` to an `uint`.
fn
to_uint
(
&
self
)
->
Option
<
uint
>
;
/// Converts the value of `self` to an `u8`.
#[inline]
fn
to_u8
(
&
self
)
->
Option
<
u8
>
{
// XXX: Check for range.
self
.to_uint
()
.and_then
(|
x
|
Some
(
x
as
u8
))
}
/// Converts the value of `self` to an `u16`.
#[inline]
fn
to_u16
(
&
self
)
->
Option
<
u16
>
{
// XXX: Check for range.
self
.to_uint
()
.and_then
(|
x
|
Some
(
x
as
u16
))
}
/// Converts the value of `self` to an `u32`.
#[inline]
fn
to_u32
(
&
self
)
->
Option
<
u32
>
{
// XXX: Check for range.
self
.to_uint
()
.and_then
(|
x
|
Some
(
x
as
u32
))
}
/// Converts the value of `self` to an `u64`.
#[inline]
fn
to_u64
(
&
self
)
->
Option
<
u64
>
{
// XXX: Check for range.
self
.to_uint
()
.and_then
(|
x
|
Some
(
x
as
u64
))
}
/// Converts the value of `self` to an `f32`.
#[inline]
fn
to_f32
(
&
self
)
->
Option
<
f32
>
{
// XXX: Check for range.
self
.to_float
()
.and_then
(|
x
|
Some
(
x
as
f32
))
}
/// Converts the value of `self` to an `f64`.
#[inline]
fn
to_f64
(
&
self
)
->
Option
<
f64
>
{
// XXX: Check for range.
self
.to_float
()
.and_then
(|
x
|
Some
(
x
as
f64
))
}
}
macro_rules!
impl_to_primitive
(
(
$T:ty
)
=>
(
impl
ToPrimitive
for
$T
{
#[inline]
fn
to_int
(
&
self
)
->
Option
<
int
>
{
Some
(
*
self
as
int
)
}
#[inline]
fn
to_i8
(
&
self
)
->
Option
<
i8
>
{
Some
(
*
self
as
i8
)
}
#[inline]
fn
to_i16
(
&
self
)
->
Option
<
i16
>
{
Some
(
*
self
as
i16
)
}
#[inline]
fn
to_i32
(
&
self
)
->
Option
<
i32
>
{
Some
(
*
self
as
i32
)
}
#[inline]
fn
to_i64
(
&
self
)
->
Option
<
i64
>
{
Some
(
*
self
as
i64
)
}
#[inline]
fn
to_uint
(
&
self
)
->
Option
<
uint
>
{
Some
(
*
self
as
uint
)
}
#[inline]
fn
to_u8
(
&
self
)
->
Option
<
u8
>
{
Some
(
*
self
as
u8
)
}
#[inline]
fn
to_u16
(
&
self
)
->
Option
<
u16
>
{
Some
(
*
self
as
u16
)
}
#[inline]
fn
to_u32
(
&
self
)
->
Option
<
u32
>
{
Some
(
*
self
as
u32
)
}
#[inline]
fn
to_u64
(
&
self
)
->
Option
<
u64
>
{
Some
(
*
self
as
u64
)
}
#[inline]
fn
to_float
(
&
self
)
->
Option
<
float
>
{
Some
(
*
self
as
float
)
}
#[inline]
fn
to_f32
(
&
self
)
->
Option
<
f32
>
{
Some
(
*
self
as
f32
)
}
#[inline]
fn
to_f64
(
&
self
)
->
Option
<
f64
>
{
Some
(
*
self
as
f64
)
}
}
)
)
impl_to_primitive!
(
u8
)
impl_to_primitive!
(
u16
)
impl_to_primitive!
(
u32
)
impl_to_primitive!
(
u64
)
impl_to_primitive!
(
uint
)
impl_to_primitive!
(
i8
)
impl_to_primitive!
(
i16
)
impl_to_primitive!
(
i32
)
impl_to_primitive!
(
i64
)
impl_to_primitive!
(
int
)
impl_to_primitive!
(
f32
)
impl_to_primitive!
(
f64
)
impl_to_primitive!
(
float
)
/// A generic trait for converting a number to a value.
pub
trait
FromPrimitive
{
/// Convert an `int` to return an optional value of this type. If the
/// value cannot be represented by this value, the `None` is returned.
fn
from_int
(
n
:
int
)
->
Option
<
Self
>
;
/// Convert an `i8` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_i8
(
n
:
i8
)
->
Option
<
Self
>
{
FromPrimitive
::
from_int
(
n
as
int
)
}
/// Convert an `i16` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_i16
(
n
:
i16
)
->
Option
<
Self
>
{
FromPrimitive
::
from_int
(
n
as
int
)
}
/// Convert an `i32` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_i32
(
n
:
i32
)
->
Option
<
Self
>
{
FromPrimitive
::
from_int
(
n
as
int
)
}
/// Convert an `i64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_i64
(
n
:
i64
)
->
Option
<
Self
>
{
FromPrimitive
::
from_int
(
n
as
int
)
}
/// Convert an `uint` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
fn
from_uint
(
n
:
uint
)
->
Option
<
Self
>
;
/// Convert an `u8` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_u8
(
n
:
u8
)
->
Option
<
Self
>
{
FromPrimitive
::
from_uint
(
n
as
uint
)
}
/// Convert an `u16` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_u16
(
n
:
u16
)
->
Option
<
Self
>
{
FromPrimitive
::
from_uint
(
n
as
uint
)
}
/// Convert an `u32` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_u32
(
n
:
u32
)
->
Option
<
Self
>
{
FromPrimitive
::
from_uint
(
n
as
uint
)
}
/// Convert an `u64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_u64
(
n
:
u64
)
->
Option
<
Self
>
{
FromPrimitive
::
from_uint
(
n
as
uint
)
}
/// Convert a `f32` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_f32
(
n
:
f32
)
->
Option
<
Self
>
{
FromPrimitive
::
from_float
(
n
as
float
)
}
/// Convert a `f64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn
from_f64
(
n
:
f64
)
->
Option
<
Self
>
{
FromPrimitive
::
from_float
(
n
as
float
)
}
}
/// A utility function that just calls `FromPrimitive::from_int`.
pub
fn
from_int
<
A
:
FromPrimitive
>
(
n
:
int
)
->
Option
<
A
>
{
FromPrimitive
::
from_int
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_i8`.
pub
fn
from_i8
<
A
:
FromPrimitive
>
(
n
:
i8
)
->
Option
<
A
>
{
FromPrimitive
::
from_i8
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_i16`.
pub
fn
from_i16
<
A
:
FromPrimitive
>
(
n
:
i16
)
->
Option
<
A
>
{
FromPrimitive
::
from_i16
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_i32`.
pub
fn
from_i32
<
A
:
FromPrimitive
>
(
n
:
i32
)
->
Option
<
A
>
{
FromPrimitive
::
from_i32
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_i64`.
pub
fn
from_i64
<
A
:
FromPrimitive
>
(
n
:
i64
)
->
Option
<
A
>
{
FromPrimitive
::
from_i64
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_uint`.
pub
fn
from_uint
<
A
:
FromPrimitive
>
(
n
:
uint
)
->
Option
<
A
>
{
FromPrimitive
::
from_uint
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_u8`.
pub
fn
from_u8
<
A
:
FromPrimitive
>
(
n
:
u8
)
->
Option
<
A
>
{
FromPrimitive
::
from_u8
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_u16`.
pub
fn
from_u16
<
A
:
FromPrimitive
>
(
n
:
u16
)
->
Option
<
A
>
{
FromPrimitive
::
from_u16
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_u32`.
pub
fn
from_u32
<
A
:
FromPrimitive
>
(
n
:
u32
)
->
Option
<
A
>
{
FromPrimitive
::
from_u32
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_u64`.
pub
fn
from_u64
<
A
:
FromPrimitive
>
(
n
:
u64
)
->
Option
<
A
>
{
FromPrimitive
::
from_u64
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_f32`.
pub
fn
from_f32
<
A
:
FromPrimitive
>
(
n
:
f32
)
->
Option
<
A
>
{
FromPrimitive
::
from_f32
(
n
)
}
/// A utility function that just calls `FromPrimitive::from_f64`.
pub
fn
from_f64
<
A
:
FromPrimitive
>
(
n
:
f64
)
->
Option
<
A
>
{
FromPrimitive
::
from_f64
(
n
)
}
macro_rules!
impl_from_primitive
(
(
$T:ty
)
=>
(
impl
FromPrimitive
for
$T
{
#[inline]
fn
from_int
(
n
:
int
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_i8
(
n
:
i8
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_i16
(
n
:
i16
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_i32
(
n
:
i32
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_i64
(
n
:
i64
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_uint
(
n
:
uint
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_u8
(
n
:
u8
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_u16
(
n
:
u16
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_u32
(
n
:
u32
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_u64
(
n
:
u64
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_float
(
n
:
float
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_f32
(
n
:
f32
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
#[inline]
fn
from_f64
(
n
:
f64
)
->
Option
<
$T
>
{
Some
(
n
as
$T
)
}
}
)
)
impl_from_primitive!
(
u8
)
impl_from_primitive!
(
u16
)
impl_from_primitive!
(
u32
)
impl_from_primitive!
(
u64
)
impl_from_primitive!
(
uint
)
impl_from_primitive!
(
i8
)
impl_from_primitive!
(
i16
)
impl_from_primitive!
(
i32
)
impl_from_primitive!
(
i64
)
impl_from_primitive!
(
int
)
impl_from_primitive!
(
f32
)
impl_from_primitive!
(
f64
)
impl_from_primitive!
(
float
)
/// Cast from one machine scalar to another
///
/// # Example
...
...
@@ -363,54 +650,24 @@ pub trait Float: Real
/// ```
///
#[inline]
pub
fn
cast
<
T
:
NumCast
,
U
:
NumCast
>
(
n
:
T
)
->
U
{
pub
fn
cast
<
T
:
NumCast
,
U
:
NumCast
>
(
n
:
T
)
->
Option
<
U
>
{
NumCast
::
from
(
n
)
}
/// An interface for casting between machine scalars
pub
trait
NumCast
{
fn
from
<
T
:
NumCast
>
(
n
:
T
)
->
Self
;
fn
to_u8
(
&
self
)
->
u8
;
fn
to_u16
(
&
self
)
->
u16
;
fn
to_u32
(
&
self
)
->
u32
;
fn
to_u64
(
&
self
)
->
u64
;
fn
to_uint
(
&
self
)
->
uint
;
fn
to_i8
(
&
self
)
->
i8
;
fn
to_i16
(
&
self
)
->
i16
;
fn
to_i32
(
&
self
)
->
i32
;
fn
to_i64
(
&
self
)
->
i64
;
fn
to_int
(
&
self
)
->
int
;
fn
to_f32
(
&
self
)
->
f32
;
fn
to_f64
(
&
self
)
->
f64
;
pub
trait
NumCast
:
ToPrimitive
{
fn
from
<
T
:
ToPrimitive
>
(
n
:
T
)
->
Option
<
Self
>
;
}
macro_rules!
impl_num_cast
(
(
$T:ty
,
$conv:ident
)
=>
(
impl
NumCast
for
$T
{
#[inline]
fn
from
<
N
:
NumCast
>
(
n
:
N
)
->
$T
{
fn
from
<
N
:
ToPrimitive
>
(
n
:
N
)
->
Option
<
$T
>
{
// `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment
n
.
$conv
()
}
#[inline]
fn
to_u8
(
&
self
)
->
u8
{
*
self
as
u8
}
#[inline]
fn
to_u16
(
&
self
)
->
u16
{
*
self
as
u16
}
#[inline]
fn
to_u32
(
&
self
)
->
u32
{
*
self
as
u32
}
#[inline]
fn
to_u64
(
&
self
)
->
u64
{
*
self
as
u64
}
#[inline]
fn
to_uint
(
&
self
)
->
uint
{
*
self
as
uint
}
#[inline]
fn
to_i8
(
&
self
)
->
i8
{
*
self
as
i8
}
#[inline]
fn
to_i16
(
&
self
)
->
i16
{
*
self
as
i16
}
#[inline]
fn
to_i32
(
&
self
)
->
i32
{
*
self
as
i32
}
#[inline]
fn
to_i64
(
&
self
)
->
i64
{
*
self
as
i64
}
#[inline]
fn
to_int
(
&
self
)
->
int
{
*
self
as
int
}
#[inline]
fn
to_f32
(
&
self
)
->
f32
{
*
self
as
f32
}
#[inline]
fn
to_f64
(
&
self
)
->
f64
{
*
self
as
f64
}
}
)
)
...
...
@@ -461,7 +718,7 @@ pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uin
if
radix
==
0u
{
return
_0
;
}
let
mut
my_pow
=
pow
;
let
mut
total
=
_1
;
let
mut
multiplier
=
cast
(
radix
);
let
mut
multiplier
=
cast
(
radix
)
.unwrap
()
;
while
(
my_pow
>
0u
)
{
if
my_pow
%
2u
==
1u
{
total
=
total
*
multiplier
;
...
...
@@ -543,11 +800,11 @@ pub trait CheckedDiv: Div<Self, Self> {
/// Helper function for testing numeric operations
#[cfg(test)]
pub
fn
test_num
<
T
:
Num
+
NumCast
>
(
ten
:
T
,
two
:
T
)
{
assert_eq!
(
ten
.add
(
&
two
),
cast
(
12
));
assert_eq!
(
ten
.sub
(
&
two
),
cast
(
8
));
assert_eq!
(
ten
.mul
(
&
two
),
cast
(
20
));
assert_eq!
(
ten
.div
(
&
two
),
cast
(
5
));
assert_eq!
(
ten
.rem
(
&
two
),
cast
(
0
));
assert_eq!
(
ten
.add
(
&
two
),
cast
(
12
)
.unwrap
()
);
assert_eq!
(
ten
.sub
(
&
two
),
cast
(
8
)
.unwrap
()
);
assert_eq!
(
ten
.mul
(
&
two
),
cast
(
20
)
.unwrap
()
);
assert_eq!
(
ten
.div
(
&
two
),
cast
(
5
)
.unwrap
()
);
assert_eq!
(
ten
.rem
(
&
two
),
cast
(
0
)
.unwrap
()
);
assert_eq!
(
ten
.add
(
&
two
),
ten
+
two
);
assert_eq!
(
ten
.sub
(
&
two
),
ten
-
two
);
...
...
@@ -566,44 +823,45 @@ mod tests {
(
$_20:expr
)
=>
({
let
_20
=
$_20
;
assert_eq!
(
20u
,
_20
.to_uint
());
assert_eq!
(
20u8
,
_20
.to_u8
());
assert_eq!
(
20u16
,
_20
.to_u16
());
assert_eq!
(
20u32
,
_20
.to_u32
());
assert_eq!
(
20u64
,
_20
.to_u64
());
assert_eq!
(
20
i
,
_20
.to_int
());
assert_eq!
(
20i8
,
_20
.to_i8
());
assert_eq!
(
20i16
,
_20
.to_i16
());
assert_eq!
(
20i32
,
_20
.to_i32
());
assert_eq!
(
20i64
,
_20
.to_i64
());
assert_eq!
(
20f32
,
_20
.to_f32
());
assert_eq!
(
20f64
,
_20
.to_f64
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u
));
assert_eq!
(
_20
,
NumCast
::
from
(
20u8
));
assert_eq!
(
_20
,
NumCast
::
from
(
20u16
));
assert_eq!
(
_20
,
NumCast
::
from
(
20u32
));
assert_eq!
(
_20
,
NumCast
::
from
(
20u64
));
assert_eq!
(
_20
,
NumCast
::
from
(
20
i
));
assert_eq!
(
_20
,
NumCast
::
from
(
20i8
));
assert_eq!
(
_20
,
NumCast
::
from
(
20i16
));
assert_eq!
(
_20
,
NumCast
::
from
(
20i32
));
assert_eq!
(
_20
,
NumCast
::
from
(
20i64
));
assert_eq!
(
_20
,
NumCast
::
from
(
20f32
));
assert_eq!
(
_20
,
NumCast
::
from
(
20f64
));
assert_eq!
(
_20
,
cast
(
20u
));
assert_eq!
(
_20
,
cast
(
20u8
));
assert_eq!
(
_20
,
cast
(
20u16
));
assert_eq!
(
_20
,
cast
(
20u32
));
assert_eq!
(
_20
,
cast
(
20u64
));
assert_eq!
(
_20
,
cast
(
20
i
));
assert_eq!
(
_20
,
cast
(
20i8
));
assert_eq!
(
_20
,
cast
(
20i16
));
assert_eq!
(
_20
,
cast
(
20i32
));
assert_eq!
(
_20
,
cast
(
20i64
));
assert_eq!
(
_20
,
cast
(
20f32
));
assert_eq!
(
_20
,
cast
(
20f64
));
assert_eq!
(
20u
,
_20
.to_uint
()
.unwrap
());
assert_eq!
(
20u8
,
_20
.to_u8
()
.unwrap
());
assert_eq!
(
20u16
,
_20
.to_u16
()
.unwrap
());
assert_eq!
(
20u32
,
_20
.to_u32
()
.unwrap
());
assert_eq!
(
20u64
,
_20
.to_u64
()
.unwrap
());
assert_eq!
(
20
i
,
_20
.to_int
()
.unwrap
());
assert_eq!
(
20i8
,
_20
.to_i8
()
.unwrap
());
assert_eq!
(
20i16
,
_20
.to_i16
()
.unwrap
());
assert_eq!
(
20i32
,
_20
.to_i32
()
.unwrap
());
assert_eq!
(
20i64
,
_20
.to_i64
()
.unwrap
());
assert_eq!
(
20
f
,
_20
.to_float
()
.unwrap
());
assert_eq!
(
20f32
,
_20
.to_f32
()
.unwrap
());
assert_eq!
(
20f64
,
_20
.to_f64
()
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u8
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u16
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u32
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20u64
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20
i
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20i8
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20i16
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20i32
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20i64
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20f32
)
.unwrap
());
assert_eq!
(
_20
,
NumCast
::
from
(
20f64
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20u
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20u8
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20u16
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20u32
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20u64
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20
i
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20i8
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20i16
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20i32
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20i64
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20f32
)
.unwrap
());
assert_eq!
(
_20
,
cast
(
20f64
)
.unwrap
());
})
)
...
...
src/libstd/num/strconv.rs
浏览文件 @
d9d1dfc1
...
...
@@ -140,7 +140,7 @@ pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
let
_0
:
T
=
Zero
::
zero
();
let
neg
=
num
<
_0
;
let
radix_gen
:
T
=
cast
(
radix
);
let
radix_gen
:
T
=
cast
(
radix
)
.unwrap
()
;
let
mut
deccum
=
num
;
// This is just for integral types, the largest of which is a u64. The
...
...
@@ -163,7 +163,7 @@ pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
}
else
{
current_digit_signed
};
buf
[
cur
]
=
match
current_digit
.to_u8
()
{
buf
[
cur
]
=
match
current_digit
.to_u8
()
.unwrap
()
{
i
@
0
..
9
=>
'0'
as
u8
+
i
,
i
=>
'a'
as
u8
+
(
i
-
10
),
};
...
...
@@ -247,7 +247,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
let
neg
=
num
<
_0
||
(
negative_zero
&&
_1
/
num
==
Float
::
neg_infinity
());
let
mut
buf
:
~
[
u8
]
=
~
[];
let
radix_gen
:
T
=
cast
(
radix
as
int
);
let
radix_gen
:
T
=
cast
(
radix
as
int
)
.unwrap
()
;
// First emit the non-fractional part, looping at least once to make
// sure at least a `0` gets emitted.
...
...
@@ -265,7 +265,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
deccum
=
deccum
/
radix_gen
;
deccum
=
deccum
.trunc
();
buf
.push
(
char
::
from_digit
(
current_digit
.to_int
()
as
uint
,
radix
)
buf
.push
(
char
::
from_digit
(
current_digit
.to_int
()
.unwrap
()
as
uint
,
radix
)
.unwrap
()
as
u8
);
// No more digits to calculate for the non-fractional part -> break
...
...
@@ -322,7 +322,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
let
current_digit
=
deccum
.trunc
()
.abs
();
buf
.push
(
char
::
from_digit
(
current_digit
.to_int
()
as
uint
,
radix
)
.unwrap
()
as
u8
);
current_digit
.to_int
()
.unwrap
()
as
uint
,
radix
)
.unwrap
()
as
u8
);
// Decrease the deccumulator one fractional digit at a time
deccum
=
deccum
.fract
();
...
...
@@ -492,7 +492,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
let
_0
:
T
=
Zero
::
zero
();
let
_1
:
T
=
One
::
one
();
let
radix_gen
:
T
=
cast
(
radix
as
int
);
let
radix_gen
:
T
=
cast
(
radix
as
int
)
.unwrap
()
;
let
len
=
buf
.len
();
...
...
@@ -543,9 +543,9 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
// add/subtract current digit depending on sign
if
accum_positive
{
accum
=
accum
+
cast
(
digit
as
int
);
accum
=
accum
+
cast
(
digit
as
int
)
.unwrap
()
;
}
else
{
accum
=
accum
-
cast
(
digit
as
int
);
accum
=
accum
-
cast
(
digit
as
int
)
.unwrap
()
;
}
// Detect overflow by comparing to last value, except
...
...
@@ -556,11 +556,11 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
// Detect overflow by reversing the shift-and-add proccess
if
accum_positive
&&
(
last_accum
!=
((
accum
-
cast
(
digit
as
int
))
/
radix_gen
.clone
()))
{
(
last_accum
!=
((
accum
-
cast
(
digit
as
int
)
.unwrap
()
)
/
radix_gen
.clone
()))
{
return
NumStrConv
::
inf
();
}
if
!
accum_positive
&&
(
last_accum
!=
((
accum
+
cast
(
digit
as
int
))
/
radix_gen
.clone
()))
{
(
last_accum
!=
((
accum
+
cast
(
digit
as
int
)
.unwrap
()
)
/
radix_gen
.clone
()))
{
return
NumStrConv
::
neg_inf
();
}
}
...
...
@@ -596,7 +596,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
// Decrease power one order of magnitude
power
=
power
/
radix_gen
;
let
digit_t
:
T
=
cast
(
digit
);
let
digit_t
:
T
=
cast
(
digit
)
.unwrap
()
;
// add/subtract current digit depending on sign
if
accum_positive
{
...
...
@@ -654,9 +654,9 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
match
exp
{
Some
(
exp_pow
)
=>
{
multiplier
=
if
exp_pow
<
0
{
_1
/
pow_with_uint
::
<
T
>
(
base
,
(
-
exp_pow
.to_int
())
as
uint
)
_1
/
pow_with_uint
::
<
T
>
(
base
,
(
-
exp_pow
.to_int
()
.unwrap
()
)
as
uint
)
}
else
{
pow_with_uint
::
<
T
>
(
base
,
exp_pow
.to_int
()
as
uint
)
pow_with_uint
::
<
T
>
(
base
,
exp_pow
.to_int
()
.unwrap
()
as
uint
)
}
}
None
=>
return
None
// invalid exponent -> invalid number
...
...
src/libstd/prelude.rs
浏览文件 @
d9d1dfc1
...
...
@@ -59,7 +59,7 @@
pub
use
num
::{
Algebraic
,
Trigonometric
,
Exponential
,
Hyperbolic
};
pub
use
num
::{
Integer
,
Fractional
,
Real
,
RealExt
};
pub
use
num
::{
Bitwise
,
BitCount
,
Bounded
};
pub
use
num
::{
Primitive
,
Int
,
Float
,
ToStrRadix
};
pub
use
num
::{
Primitive
,
Int
,
Float
,
ToStrRadix
,
ToPrimitive
,
FromPrimitive
};
pub
use
path
::
GenericPath
;
pub
use
path
::
Path
;
pub
use
path
::
PosixPath
;
...
...
src/libstd/rand/mod.rs
浏览文件 @
d9d1dfc1
...
...
@@ -314,12 +314,12 @@ fn gen_vec<T: Rand>(&mut self, len: uint) -> ~[T] {
/// ```
fn
gen_integer_range
<
T
:
Rand
+
Int
>
(
&
mut
self
,
low
:
T
,
high
:
T
)
->
T
{
assert
!
(
low
<
high
,
"RNG.gen_integer_range called with low >= high"
);
let
range
=
(
high
-
low
)
.to_u64
();
let
range
=
(
high
-
low
)
.to_u64
()
.unwrap
()
;
let
accept_zone
=
u64
::
max_value
-
u64
::
max_value
%
range
;
loop
{
let
rand
=
self
.gen
::
<
u64
>
();
if
rand
<
accept_zone
{
return
low
+
NumCast
::
from
(
rand
%
range
);
return
low
+
NumCast
::
from
(
rand
%
range
)
.unwrap
()
;
}
}
}
...
...
src/test/run-pass/numeric-method-autoexport.rs
浏览文件 @
d9d1dfc1
...
...
@@ -36,6 +36,6 @@ pub fn main() {
// floats
// num
assert_eq!
(
10f32
.to_int
(),
10
);
assert_eq!
(
10f64
.to_int
(),
10
);
assert_eq!
(
10f32
.to_int
()
.unwrap
()
,
10
);
assert_eq!
(
10f64
.to_int
()
.unwrap
()
,
10
);
}
src/test/run-pass/trait-inheritance-num.rs
浏览文件 @
d9d1dfc1
...
...
@@ -19,7 +19,7 @@ pub trait NumExt: Num + NumCast + Eq + Ord {}
pub
trait
FloatExt
:
NumExt
+
ApproxEq
<
Self
>
{}
fn
greater_than_one
<
T
:
NumExt
>
(
n
:
&
T
)
->
bool
{
*
n
>
NumCast
::
from
(
1
)
}
fn
greater_than_one_float
<
T
:
FloatExt
>
(
n
:
&
T
)
->
bool
{
*
n
>
NumCast
::
from
(
1
)
}
fn
greater_than_one
<
T
:
NumExt
>
(
n
:
&
T
)
->
bool
{
*
n
>
NumCast
::
from
(
1
)
.unwrap
()
}
fn
greater_than_one_float
<
T
:
FloatExt
>
(
n
:
&
T
)
->
bool
{
*
n
>
NumCast
::
from
(
1
)
.unwrap
()
}
pub
fn
main
()
{}
src/test/run-pass/trait-inheritance-num0.rs
浏览文件 @
d9d1dfc1
...
...
@@ -22,7 +22,7 @@ trait Num {
pub
trait
NumExt
:
Num
+
NumCast
{
}
fn
greater_than_one
<
T
:
NumExt
>
(
n
:
&
T
)
->
bool
{
n
.gt
(
&
NumCast
::
from
(
1
))
n
.gt
(
&
NumCast
::
from
(
1
)
.unwrap
()
)
}
pub
fn
main
()
{}
src/test/run-pass/trait-inheritance-num1.rs
浏览文件 @
d9d1dfc1
...
...
@@ -14,7 +14,7 @@
pub
trait
NumExt
:
Num
+
NumCast
+
Ord
{
}
fn
greater_than_one
<
T
:
NumExt
>
(
n
:
&
T
)
->
bool
{
*
n
>
NumCast
::
from
(
1
)
*
n
>
NumCast
::
from
(
1
)
.unwrap
()
}
pub
fn
main
()
{}
src/test/run-pass/trait-inheritance-num3.rs
浏览文件 @
d9d1dfc1
...
...
@@ -16,7 +16,7 @@ pub trait NumExt: Eq + Ord + Num + NumCast {}
impl
NumExt
for
f32
{}
fn
num_eq_one
<
T
:
NumExt
>
(
n
:
T
)
{
println!
(
"{}"
,
n
==
NumCast
::
from
(
1
))
println!
(
"{}"
,
n
==
NumCast
::
from
(
1
)
.unwrap
()
)
}
pub
fn
main
()
{
...
...
src/test/run-pass/trait-inheritance-num5.rs
浏览文件 @
d9d1dfc1
...
...
@@ -17,7 +17,7 @@ impl NumExt for f32 {}
impl
NumExt
for
int
{}
fn
num_eq_one
<
T
:
NumExt
>
()
->
T
{
NumCast
::
from
(
1
)
NumCast
::
from
(
1
)
.unwrap
()
}
pub
fn
main
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录