auto merge of #13901 : alexcrichton/rust/facade, r=brson

This is the second step in implementing #13851. This PR cannot currently land until a snapshot exists with #13892, but I imagine that this review will take longer.

This PR refactors a large amount of functionality outside of the standard library into a new library, libcore. This new library has 0 dependencies (in theory). In practice, this library currently depends on these symbols being available:

* `rust_begin_unwind` and `rust_fail_bounds_check` - These are the two entry points of failure in libcore. The symbols are provided by libstd currently. In the future (see the bullets on #13851) this will be officially supported with nice error mesages. Additionally, there will only be one failure entry point once `std::fmt` migrates to libcore.
* `memcpy` - This is often generated by LLVM. This is also quite trivial to implement for any platform, so I'm not too worried about this.
* `memcmp` - This is required for comparing strings. This function is quite common *everywhere*, so I don't feel to bad about relying on a consumer of libcore to define it.
* `malloc` and `free` - This is quite unfortunate, and is a temporary stopgap until we deal with the `~` situation. More details can be found in the module `core::should_not_exist`
* `fmod` and `fmodf` - These exist because the `Rem` trait is defined in libcore, so the `Rem` implementation for floats must also be defined in libcore. I imagine that any platform using floating-point modulus will have these symbols anyway, and otherwise they will be optimized out.
* `fdim` and `fdimf` - Like `fmod`, these are from the `Signed` trait being defined in libcore. I don't expect this to be much of a problem

These dependencies all "Just Work" for now because libcore only exists as an rlib, not as a dylib.

The commits themselves are organized to show that the overall diff of this extraction is not all that large. Most modules were able to be moved with very few modifications. The primary module left out of this iteration is `std::fmt`. I plan on migrating the `fmt` module to libcore, but I chose to not do so at this time because it had implications on the `Writer` trait that I wanted to deal with in isolation. There are a few breaking changes in these commits, but they are fairly minor, and are all labeled with `[breaking-change]`.

The nastiest parts of this movement come up with `~[T]` and `~str` being language-defined types today. I believe that much of this nastiness will get better over time as we migrate towards `Vec<T>` and `Str` (or whatever the types will be named). There will likely always be some extension traits, but the situation won't be as bad as it is today.

Known deficiencies:

* rustdoc will get worse in terms of readability. This is the next issue I will tackle as part of #13851. If others think that the rustdoc change should happen first, I can also table this to fix rustdoc first.
* The compiler reveals that all these types are reexports via error messages like `core::option::Option`. This is filed as #13065, and I believe that issue would have a higher priority now. I do not currently plan on fixing that as part of #13851. If others believe that this issue should be fixed, I can also place it on the roadmap for #13851.

I recommend viewing these changes on a commit-by-commit basis. The overall change is likely too overwhelming to take in.
This commit is contained in:
bors 2014-05-07 11:06:45 -07:00
commit 87115fd001
115 changed files with 14041 additions and 12792 deletions

View File

@ -51,12 +51,13 @@
TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
uuid serialize sync getopts collections num test time rand \
workcache url log regex graphviz
workcache url log regex graphviz core
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
TOOLS := compiletest rustdoc rustc
DEPS_std := libc native:rustrt native:compiler-rt native:backtrace
DEPS_core :=
DEPS_std := core libc native:rustrt native:compiler-rt native:backtrace
DEPS_green := std rand native:context_switch
DEPS_rustuv := std native:uv native:uv_support
DEPS_native := std
@ -95,6 +96,8 @@ TOOL_SOURCE_compiletest := $(S)src/compiletest/compiletest.rs
TOOL_SOURCE_rustdoc := $(S)src/driver/driver.rs
TOOL_SOURCE_rustc := $(S)src/driver/driver.rs
ONLY_RLIB_core := 1
################################################################################
# You should not need to edit below this line
################################################################################

View File

@ -18,6 +18,7 @@
# $(5) - the name of the crate being processed
define CP_HOST_STAGE_N_CRATE
ifeq ($$(ONLY_RLIB_$(5)),)
$$(HLIB$(2)_H_$(4))/stamp.$(5): \
$$(TLIB$(1)_T_$(3)_H_$(4))/stamp.$(5) \
$$(RUST_DEPS_$(5):%=$$(HLIB$(2)_H_$(4))/stamp.%) \
@ -30,6 +31,10 @@ $$(HLIB$(2)_H_$(4))/stamp.$(5): \
$$(HLIB$(2)_H_$(4))
$$(call LIST_ALL_OLD_GLOB_MATCHES,\
$$(dir $$@)$$(call CFG_LIB_GLOB_$(3),$(5)))
else
$$(HLIB$(2)_H_$(4))/stamp.$(5):
$$(Q)touch $$@
endif
endef
@ -54,9 +59,6 @@ endef
# $(4) - the host triple (same as $(3))
define CP_HOST_STAGE_N
$$(HBIN$(2)_H_$(4))/:
@mkdir -p $$@
ifneq ($(CFG_LIBDIR_RELATIVE),bin)
$$(HLIB$(2)_H_$(4))/:
@mkdir -p $$@

View File

@ -3274,7 +3274,7 @@ The machine types are the following:
* The signed two's complement word types `i8`, `i16`, `i32` and `i64`, with
values drawn from the integer intervals [-(2^(7)), 2^7 - 1],
[-(2^(15)), 2^15 - 1], $[-(2^(31)), 2^31 - 1], [-(2^(63)), 2^63 - 1]
[-(2^(15)), 2^15 - 1], [-(2^(31)), 2^31 - 1], [-(2^(63)), 2^63 - 1]
respectively.
* The IEEE 754-2008 `binary32` and `binary64` floating-point types: `f32` and

View File

@ -21,7 +21,6 @@
//! the extension traits (`*Ext`) for the full details.
use cast::{transmute, transmute_copy};
use fmt;
use option::{Option, Some, None};
use owned::Box;
use raw::TraitObject;
@ -145,28 +144,12 @@ impl AnyOwnExt for Box<Any> {
}
}
///////////////////////////////////////////////////////////////////////////////
// Trait implementations
///////////////////////////////////////////////////////////////////////////////
impl fmt::Show for Box<Any> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("Box<Any>")
}
}
impl<'a> fmt::Show for &'a Any {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("&Any")
}
}
#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use owned::Box;
use str::StrSlice;
use realstd::str::StrAllocating;
#[deriving(Eq, Show)]
struct Test;
@ -291,13 +274,13 @@ mod tests {
#[test]
fn test_show() {
let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;
let a = box 8u as Box<::realcore::any::Any>;
let b = box Test as Box<::realcore::any::Any>;
assert_eq!(format!("{}", a), "Box<Any>".to_owned());
assert_eq!(format!("{}", b), "Box<Any>".to_owned());
let a = &8u as &Any;
let b = &Test as &Any;
let a = &8u as &::realcore::any::Any;
let b = &Test as &::realcore::any::Any;
assert_eq!(format!("{}", a), "&Any".to_owned());
assert_eq!(format!("{}", b), "&Any".to_owned());
}

View File

@ -14,7 +14,6 @@
//!
//! Implementations of the following traits:
//!
//! * `FromStr`
//! * `Not`
//! * `Ord`
//! * `TotalOrd`
@ -24,11 +23,9 @@
//!
//! A `to_bit` conversion function.
use from_str::FromStr;
use num::{Int, one, zero};
use option::{None, Option, Some};
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering, TotalEq};
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
#[cfg(not(test))] use default::Default;
@ -55,28 +52,6 @@ pub fn to_bit<N: Int>(p: bool) -> N {
// Trait impls on `bool`
/////////////////////////////////////////////////////////////////////////////
impl FromStr for bool {
/// Parse a `bool` from a string.
///
/// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
///
/// # Examples
///
/// ```rust
/// assert_eq!(from_str::<bool>("true"), Some(true));
/// assert_eq!(from_str::<bool>("false"), Some(false));
/// assert_eq!(from_str::<bool>("not even a boolean"), None);
/// ```
#[inline]
fn from_str(s: &str) -> Option<bool> {
match s {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
}
#[cfg(not(test))]
impl Not<bool> for bool {
/// The logical complement of a boolean value.
@ -190,6 +165,9 @@ impl Eq for bool {
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
}
#[cfg(not(test))]
impl TotalEq for bool {}
#[cfg(not(test))]
impl Default for bool {
fn default() -> bool { false }
@ -197,9 +175,8 @@ impl Default for bool {
#[cfg(test)]
mod tests {
use prelude::*;
use realstd::prelude::*;
use super::to_bit;
use str::StrSlice;
#[test]
fn test_to_bit() {
@ -260,13 +237,6 @@ mod tests {
assert_eq!(!false, true);
}
#[test]
fn test_from_str() {
assert_eq!(from_str::<bool>("true"), Some(true));
assert_eq!(from_str::<bool>("false"), Some(false));
assert_eq!(from_str::<bool>("not even a boolean"), None);
}
#[test]
fn test_to_str() {
assert_eq!(false.to_str(), "false".to_owned());

View File

@ -108,7 +108,7 @@ pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
mod tests {
use cast::{bump_box_refcount, transmute};
use raw;
use str::StrSlice;
use realstd::str::StrAllocating;
#[test]
fn test_transmute_copy() {

View File

@ -12,7 +12,6 @@
use clone::Clone;
use cmp::Eq;
use fmt;
use kinds::{marker, Copy};
use ops::{Deref, DerefMut, Drop};
use option::{None, Option, Some};
@ -60,12 +59,6 @@ impl<T:Eq + Copy> Eq for Cell<T> {
}
}
impl<T: Copy + fmt::Show> fmt::Show for Cell<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, r"Cell \{ value: {} \}", self.get())
}
}
/// A mutable memory location with dynamically checked borrow rules
pub struct RefCell<T> {
value: Unsafe<T>,
@ -228,22 +221,22 @@ mod test {
#[test]
fn smoketest_cell() {
let x = Cell::new(10);
assert_eq!(x, Cell::new(10));
assert_eq!(x.get(), 10);
assert!(x == Cell::new(10));
assert!(x.get() == 10);
x.set(20);
assert_eq!(x, Cell::new(20));
assert_eq!(x.get(), 20);
assert!(x == Cell::new(20));
assert!(x.get() == 20);
let y = Cell::new((30, 40));
assert_eq!(y, Cell::new((30, 40)));
assert_eq!(y.get(), (30, 40));
assert!(y == Cell::new((30, 40)));
assert!(y.get() == (30, 40));
}
#[test]
fn cell_has_sensible_show() {
use str::StrSlice;
let x = Cell::new("foo bar");
let x = ::realcore::cell::Cell::new("foo bar");
assert!(format!("{}", x).contains(x.get()));
x.set("baz qux");

View File

@ -27,14 +27,9 @@
use cast::transmute;
use option::{None, Option, Some};
use iter::{Iterator, range_step};
use str::StrSlice;
use unicode::{derived_property, property, general_category, decompose, conversions};
#[cfg(test)] use str::Str;
#[cfg(test)] use strbuf::StrBuf;
#[cfg(test)] use slice::ImmutableVector;
#[cfg(not(test))] use cmp::{Eq, Ord};
#[cfg(not(test))] use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
#[cfg(not(test))] use default::Default;
// UTF-8 ranges and tags for encoding characters
@ -215,7 +210,7 @@ pub fn is_digit_radix(c: char, radix: uint) -> bool {
#[inline]
pub fn to_digit(c: char, radix: uint) -> Option<uint> {
if radix > 36 {
fail!("to_digit: radix {} is too high (maximum 36)", radix);
fail!("to_digit: radix is too high (maximum 36)");
}
let val = match c {
'0' .. '9' => c as uint - ('0' as uint),
@ -274,7 +269,7 @@ pub fn to_lowercase(c: char) -> char {
#[inline]
pub fn from_digit(num: uint, radix: uint) -> Option<char> {
if radix > 36 {
fail!("from_digit: radix {} is to high (maximum 36)", num);
fail!("from_digit: radix is to high (maximum 36)");
}
if num < radix {
unsafe {
@ -659,187 +654,208 @@ impl Eq for char {
fn eq(&self, other: &char) -> bool { (*self) == (*other) }
}
#[cfg(not(test))]
impl TotalEq for char {}
#[cfg(not(test))]
impl Ord for char {
#[inline]
fn lt(&self, other: &char) -> bool { *self < *other }
}
#[cfg(not(test))]
impl TotalOrd for char {
fn cmp(&self, other: &char) -> Ordering {
(*self as u32).cmp(&(*other as u32))
}
}
#[cfg(not(test))]
impl Default for char {
#[inline]
fn default() -> char { '\x00' }
}
#[test]
fn test_is_lowercase() {
assert!('a'.is_lowercase());
assert!('ö'.is_lowercase());
assert!('ß'.is_lowercase());
assert!(!'Ü'.is_lowercase());
assert!(!'P'.is_lowercase());
}
#[cfg(test)]
mod test {
use super::{escape_unicode, escape_default};
#[test]
fn test_is_uppercase() {
assert!(!'h'.is_uppercase());
assert!(!'ä'.is_uppercase());
assert!(!'ß'.is_uppercase());
assert!('Ö'.is_uppercase());
assert!('T'.is_uppercase());
}
use realcore::char::Char;
use slice::ImmutableVector;
use realstd::option::{Some, None};
use realstd::strbuf::StrBuf;
use realstd::str::StrAllocating;
#[test]
fn test_is_whitespace() {
assert!(' '.is_whitespace());
assert!('\u2007'.is_whitespace());
assert!('\t'.is_whitespace());
assert!('\n'.is_whitespace());
assert!(!'a'.is_whitespace());
assert!(!'_'.is_whitespace());
assert!(!'\u0000'.is_whitespace());
}
#[test]
fn test_to_digit() {
assert_eq!('0'.to_digit(10u), Some(0u));
assert_eq!('1'.to_digit(2u), Some(1u));
assert_eq!('2'.to_digit(3u), Some(2u));
assert_eq!('9'.to_digit(10u), Some(9u));
assert_eq!('a'.to_digit(16u), Some(10u));
assert_eq!('A'.to_digit(16u), Some(10u));
assert_eq!('b'.to_digit(16u), Some(11u));
assert_eq!('B'.to_digit(16u), Some(11u));
assert_eq!('z'.to_digit(36u), Some(35u));
assert_eq!('Z'.to_digit(36u), Some(35u));
assert_eq!(' '.to_digit(10u), None);
assert_eq!('$'.to_digit(36u), None);
}
#[test]
fn test_to_lowercase() {
assert_eq!('A'.to_lowercase(), 'a');
assert_eq!('Ö'.to_lowercase(), 'ö');
assert_eq!('ß'.to_lowercase(), 'ß');
assert_eq!('Ü'.to_lowercase(), 'ü');
assert_eq!('💩'.to_lowercase(), '💩');
assert_eq!('Σ'.to_lowercase(), 'σ');
assert_eq!('Τ'.to_lowercase(), 'τ');
assert_eq!('Ι'.to_lowercase(), 'ι');
assert_eq!('Γ'.to_lowercase(), 'γ');
assert_eq!('Μ'.to_lowercase(), 'μ');
assert_eq!('Α'.to_lowercase(), 'α');
assert_eq!('Σ'.to_lowercase(), 'σ');
}
#[test]
fn test_to_uppercase() {
assert_eq!('a'.to_uppercase(), 'A');
assert_eq!('ö'.to_uppercase(), 'Ö');
assert_eq!('ß'.to_uppercase(), 'ß'); // not ẞ: Latin capital letter sharp s
assert_eq!('ü'.to_uppercase(), 'Ü');
assert_eq!('💩'.to_uppercase(), '💩');
assert_eq!('σ'.to_uppercase(), 'Σ');
assert_eq!('τ'.to_uppercase(), 'Τ');
assert_eq!('ι'.to_uppercase(), 'Ι');
assert_eq!('γ'.to_uppercase(), 'Γ');
assert_eq!('μ'.to_uppercase(), 'Μ');
assert_eq!('α'.to_uppercase(), 'Α');
assert_eq!('ς'.to_uppercase(), 'Σ');
}
#[test]
fn test_is_control() {
assert!('\u0000'.is_control());
assert!('\u0003'.is_control());
assert!('\u0006'.is_control());
assert!('\u0009'.is_control());
assert!('\u007f'.is_control());
assert!('\u0092'.is_control());
assert!(!'\u0020'.is_control());
assert!(!'\u0055'.is_control());
assert!(!'\u0068'.is_control());
}
#[test]
fn test_is_digit() {
assert!('2'.is_digit());
assert!('7'.is_digit());
assert!(!'c'.is_digit());
assert!(!'i'.is_digit());
assert!(!'z'.is_digit());
assert!(!'Q'.is_digit());
}
#[test]
fn test_escape_default() {
fn string(c: char) -> ~str {
let mut result = StrBuf::new();
escape_default(c, |c| { result.push_char(c); });
return result.into_owned();
}
assert_eq!(string('\n'), "\\n".to_owned());
assert_eq!(string('\r'), "\\r".to_owned());
assert_eq!(string('\''), "\\'".to_owned());
assert_eq!(string('"'), "\\\"".to_owned());
assert_eq!(string(' '), " ".to_owned());
assert_eq!(string('a'), "a".to_owned());
assert_eq!(string('~'), "~".to_owned());
assert_eq!(string('\x00'), "\\x00".to_owned());
assert_eq!(string('\x1f'), "\\x1f".to_owned());
assert_eq!(string('\x7f'), "\\x7f".to_owned());
assert_eq!(string('\xff'), "\\xff".to_owned());
assert_eq!(string('\u011b'), "\\u011b".to_owned());
assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned());
}
#[test]
fn test_escape_unicode() {
fn string(c: char) -> ~str {
let mut result = StrBuf::new();
escape_unicode(c, |c| { result.push_char(c); });
return result.into_owned();
}
assert_eq!(string('\x00'), "\\x00".to_owned());
assert_eq!(string('\n'), "\\x0a".to_owned());
assert_eq!(string(' '), "\\x20".to_owned());
assert_eq!(string('a'), "\\x61".to_owned());
assert_eq!(string('\u011b'), "\\u011b".to_owned());
assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned());
}
#[test]
fn test_to_str() {
use to_str::ToStr;
let s = 't'.to_str();
assert_eq!(s, "t".to_owned());
}
#[test]
fn test_encode_utf8() {
fn check(input: char, expect: &[u8]) {
let mut buf = [0u8, ..4];
let n = input.encode_utf8(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
#[test]
fn test_is_lowercase() {
assert!('a'.is_lowercase());
assert!('ö'.is_lowercase());
assert!('ß'.is_lowercase());
assert!(!'Ü'.is_lowercase());
assert!(!'P'.is_lowercase());
}
check('x', [0x78]);
check('\u00e9', [0xc3, 0xa9]);
check('\ua66e', [0xea, 0x99, 0xae]);
check('\U0001f4a9', [0xf0, 0x9f, 0x92, 0xa9]);
}
#[test]
fn test_encode_utf16() {
fn check(input: char, expect: &[u16]) {
let mut buf = [0u16, ..2];
let n = input.encode_utf16(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
#[test]
fn test_is_uppercase() {
assert!(!'h'.is_uppercase());
assert!(!'ä'.is_uppercase());
assert!(!'ß'.is_uppercase());
assert!('Ö'.is_uppercase());
assert!('T'.is_uppercase());
}
check('x', [0x0078]);
check('\u00e9', [0x00e9]);
check('\ua66e', [0xa66e]);
check('\U0001f4a9', [0xd83d, 0xdca9]);
#[test]
fn test_is_whitespace() {
assert!(' '.is_whitespace());
assert!('\u2007'.is_whitespace());
assert!('\t'.is_whitespace());
assert!('\n'.is_whitespace());
assert!(!'a'.is_whitespace());
assert!(!'_'.is_whitespace());
assert!(!'\u0000'.is_whitespace());
}
#[test]
fn test_to_digit() {
assert_eq!('0'.to_digit(10u), Some(0u));
assert_eq!('1'.to_digit(2u), Some(1u));
assert_eq!('2'.to_digit(3u), Some(2u));
assert_eq!('9'.to_digit(10u), Some(9u));
assert_eq!('a'.to_digit(16u), Some(10u));
assert_eq!('A'.to_digit(16u), Some(10u));
assert_eq!('b'.to_digit(16u), Some(11u));
assert_eq!('B'.to_digit(16u), Some(11u));
assert_eq!('z'.to_digit(36u), Some(35u));
assert_eq!('Z'.to_digit(36u), Some(35u));
assert_eq!(' '.to_digit(10u), None);
assert_eq!('$'.to_digit(36u), None);
}
#[test]
fn test_to_lowercase() {
assert_eq!('A'.to_lowercase(), 'a');
assert_eq!('Ö'.to_lowercase(), 'ö');
assert_eq!('ß'.to_lowercase(), 'ß');
assert_eq!('Ü'.to_lowercase(), 'ü');
assert_eq!('💩'.to_lowercase(), '💩');
assert_eq!('Σ'.to_lowercase(), 'σ');
assert_eq!('Τ'.to_lowercase(), 'τ');
assert_eq!('Ι'.to_lowercase(), 'ι');
assert_eq!('Γ'.to_lowercase(), 'γ');
assert_eq!('Μ'.to_lowercase(), 'μ');
assert_eq!('Α'.to_lowercase(), 'α');
assert_eq!('Σ'.to_lowercase(), 'σ');
}
#[test]
fn test_to_uppercase() {
assert_eq!('a'.to_uppercase(), 'A');
assert_eq!('ö'.to_uppercase(), 'Ö');
assert_eq!('ß'.to_uppercase(), 'ß'); // not ẞ: Latin capital letter sharp s
assert_eq!('ü'.to_uppercase(), 'Ü');
assert_eq!('💩'.to_uppercase(), '💩');
assert_eq!('σ'.to_uppercase(), 'Σ');
assert_eq!('τ'.to_uppercase(), 'Τ');
assert_eq!('ι'.to_uppercase(), 'Ι');
assert_eq!('γ'.to_uppercase(), 'Γ');
assert_eq!('μ'.to_uppercase(), 'Μ');
assert_eq!('α'.to_uppercase(), 'Α');
assert_eq!('ς'.to_uppercase(), 'Σ');
}
#[test]
fn test_is_control() {
assert!('\u0000'.is_control());
assert!('\u0003'.is_control());
assert!('\u0006'.is_control());
assert!('\u0009'.is_control());
assert!('\u007f'.is_control());
assert!('\u0092'.is_control());
assert!(!'\u0020'.is_control());
assert!(!'\u0055'.is_control());
assert!(!'\u0068'.is_control());
}
#[test]
fn test_is_digit() {
assert!('2'.is_digit());
assert!('7'.is_digit());
assert!(!'c'.is_digit());
assert!(!'i'.is_digit());
assert!(!'z'.is_digit());
assert!(!'Q'.is_digit());
}
#[test]
fn test_escape_default() {
fn string(c: char) -> ~str {
let mut result = StrBuf::new();
escape_default(c, |c| { result.push_char(c); });
return result.into_owned();
}
assert_eq!(string('\n'), "\\n".to_owned());
assert_eq!(string('\r'), "\\r".to_owned());
assert_eq!(string('\''), "\\'".to_owned());
assert_eq!(string('"'), "\\\"".to_owned());
assert_eq!(string(' '), " ".to_owned());
assert_eq!(string('a'), "a".to_owned());
assert_eq!(string('~'), "~".to_owned());
assert_eq!(string('\x00'), "\\x00".to_owned());
assert_eq!(string('\x1f'), "\\x1f".to_owned());
assert_eq!(string('\x7f'), "\\x7f".to_owned());
assert_eq!(string('\xff'), "\\xff".to_owned());
assert_eq!(string('\u011b'), "\\u011b".to_owned());
assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned());
}
#[test]
fn test_escape_unicode() {
fn string(c: char) -> ~str {
let mut result = StrBuf::new();
escape_unicode(c, |c| { result.push_char(c); });
return result.into_owned();
}
assert_eq!(string('\x00'), "\\x00".to_owned());
assert_eq!(string('\n'), "\\x0a".to_owned());
assert_eq!(string(' '), "\\x20".to_owned());
assert_eq!(string('a'), "\\x61".to_owned());
assert_eq!(string('\u011b'), "\\u011b".to_owned());
assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned());
}
#[test]
fn test_to_str() {
use realstd::to_str::ToStr;
let s = 't'.to_str();
assert_eq!(s, "t".to_owned());
}
#[test]
fn test_encode_utf8() {
fn check(input: char, expect: &[u8]) {
let mut buf = [0u8, ..4];
let n = input.encode_utf8(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
}
check('x', [0x78]);
check('\u00e9', [0xc3, 0xa9]);
check('\ua66e', [0xea, 0x99, 0xae]);
check('\U0001f4a9', [0xf0, 0x9f, 0x92, 0xa9]);
}
#[test]
fn test_encode_utf16() {
fn check(input: char, expect: &[u16]) {
let mut buf = [0u16, ..2];
let n = input.encode_utf16(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
}
check('x', [0x0078]);
check('\u00e9', [0x00e9]);
check('\ua66e', [0xa66e]);
check('\U0001f4a9', [0xd83d, 0xdca9]);
}
}

View File

@ -126,46 +126,52 @@ extern_fn_clone!(A, B, C, D, E, F)
extern_fn_clone!(A, B, C, D, E, F, G)
extern_fn_clone!(A, B, C, D, E, F, G, H)
#[test]
fn test_owned_clone() {
let a = box 5i;
let b: Box<int> = a.clone();
assert_eq!(a, b);
}
#[test]
fn test_managed_clone() {
let a = @5i;
let b: @int = a.clone();
assert_eq!(a, b);
}
#[test]
fn test_borrowed_clone() {
let x = 5i;
let y: &int = &x;
let z: &int = (&y).clone();
assert_eq!(*z, 5);
}
#[test]
fn test_clone_from() {
let a = box 5;
let mut b = box 10;
b.clone_from(&a);
assert_eq!(*b, 5);
}
#[test]
fn test_extern_fn_clone() {
trait Empty {}
impl Empty for int {}
fn test_fn_a() -> f64 { 1.0 }
fn test_fn_b<T: Empty>(x: T) -> T { x }
fn test_fn_c(_: int, _: f64, _: ~[int], _: int, _: int, _: int) {}
let _ = test_fn_a.clone();
let _ = test_fn_b::<int>.clone();
let _ = test_fn_c.clone();
#[cfg(test)]
mod test {
use prelude::*;
use owned::Box;
#[test]
fn test_owned_clone() {
let a = box 5i;
let b: Box<int> = a.clone();
assert_eq!(a, b);
}
#[test]
fn test_managed_clone() {
let a = @5i;
let b: @int = a.clone();
assert_eq!(a, b);
}
#[test]
fn test_borrowed_clone() {
let x = 5i;
let y: &int = &x;
let z: &int = (&y).clone();
assert_eq!(*z, 5);
}
#[test]
fn test_clone_from() {
let a = box 5;
let mut b = box 10;
b.clone_from(&a);
assert_eq!(*b, 5);
}
#[test]
fn test_extern_fn_clone() {
trait Empty {}
impl Empty for int {}
fn test_fn_a() -> f64 { 1.0 }
fn test_fn_b<T: Empty>(x: T) -> T { x }
fn test_fn_c(_: int, _: f64, _: ~[int], _: int, _: int, _: int) {}
let _ = test_fn_a.clone();
let _ = test_fn_b::<int>.clone();
let _ = test_fn_c.clone();
}
}

View File

@ -81,32 +81,8 @@ pub trait TotalEq: Eq {
fn assert_receiver_is_total_eq(&self) {}
}
/// A macro which defines an implementation of TotalEq for a given type.
macro_rules! totaleq_impl(
($t:ty) => {
impl TotalEq for $t {}
}
)
totaleq_impl!(bool)
totaleq_impl!(u8)
totaleq_impl!(u16)
totaleq_impl!(u32)
totaleq_impl!(u64)
totaleq_impl!(i8)
totaleq_impl!(i16)
totaleq_impl!(i32)
totaleq_impl!(i64)
totaleq_impl!(int)
totaleq_impl!(uint)
totaleq_impl!(char)
/// An ordering is, e.g, a result of a comparison between two values.
#[deriving(Clone, Eq, Show)]
#[deriving(Clone, Eq)]
pub enum Ordering {
/// An ordering where a compared value is less [than another].
Less = -1,
@ -140,6 +116,7 @@ pub trait TotalOrd: TotalEq + Ord {
}
impl TotalEq for Ordering {}
impl TotalOrd for Ordering {
#[inline]
fn cmp(&self, other: &Ordering) -> Ordering {
@ -152,35 +129,6 @@ impl Ord for Ordering {
fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
}
/// A macro which defines an implementation of TotalOrd for a given type.
macro_rules! totalord_impl(
($t:ty) => {
impl TotalOrd for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
}
}
}
)
totalord_impl!(u8)
totalord_impl!(u16)
totalord_impl!(u32)
totalord_impl!(u64)
totalord_impl!(i8)
totalord_impl!(i16)
totalord_impl!(i32)
totalord_impl!(i64)
totalord_impl!(int)
totalord_impl!(uint)
totalord_impl!(char)
/// Combine orderings, lexically.
///
/// For example for a type `(int, int)`, two comparisons could be done.
@ -241,6 +189,82 @@ pub fn max<T: TotalOrd>(v1: T, v2: T) -> T {
if v1 > v2 { v1 } else { v2 }
}
// Implementation of Eq/TotalEq for some primitive types
#[cfg(not(test))]
mod impls {
use cmp::{Ord, TotalOrd, Eq, TotalEq, Ordering};
use owned::Box;
// & pointers
impl<'a, T: Eq> Eq for &'a T {
#[inline]
fn eq(&self, other: & &'a T) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: & &'a T) -> bool { *(*self) != *(*other) }
}
impl<'a, T: Ord> Ord for &'a T {
#[inline]
fn lt(&self, other: & &'a T) -> bool { *(*self) < *(*other) }
#[inline]
fn le(&self, other: & &'a T) -> bool { *(*self) <= *(*other) }
#[inline]
fn ge(&self, other: & &'a T) -> bool { *(*self) >= *(*other) }
#[inline]
fn gt(&self, other: & &'a T) -> bool { *(*self) > *(*other) }
}
impl<'a, T: TotalOrd> TotalOrd for &'a T {
#[inline]
fn cmp(&self, other: & &'a T) -> Ordering { (**self).cmp(*other) }
}
impl<'a, T: TotalEq> TotalEq for &'a T {}
// @ pointers
impl<T:Eq> Eq for @T {
#[inline]
fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
}
impl<T:Ord> Ord for @T {
#[inline]
fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
#[inline]
fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
#[inline]
fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
#[inline]
fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
}
impl<T: TotalOrd> TotalOrd for @T {
#[inline]
fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
}
impl<T: TotalEq> TotalEq for @T {}
// box pointers
impl<T:Eq> Eq for Box<T> {
#[inline]
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) }
}
impl<T:Ord> Ord for Box<T> {
#[inline]
fn lt(&self, other: &Box<T>) -> bool { *(*self) < *(*other) }
#[inline]
fn le(&self, other: &Box<T>) -> bool { *(*self) <= *(*other) }
#[inline]
fn ge(&self, other: &Box<T>) -> bool { *(*self) >= *(*other) }
#[inline]
fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) }
}
impl<T: TotalOrd> TotalOrd for Box<T> {
#[inline]
fn cmp(&self, other: &Box<T>) -> Ordering { (**self).cmp(*other) }
}
impl<T: TotalEq> TotalEq for Box<T> {}
}
#[cfg(test)]
mod test {
use super::lexical_ordering;

55
src/libcore/failure.rs Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Failure support for libcore
#![allow(dead_code)]
#[cfg(not(test))]
use str::raw::c_str_to_static_slice;
// FIXME: Once std::fmt is in libcore, all of these functions should delegate
// to a common failure function with this signature:
//
// extern {
// fn rust_unwind(f: &fmt::Arguments, file: &str, line: uint) -> !;
// }
//
// Each of these functions can create a temporary fmt::Arguments
// structure to pass to this function.
#[cold] #[inline(never)] // this is the slow path, always
#[lang="fail_"]
#[cfg(not(test))]
fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
unsafe {
let expr = c_str_to_static_slice(expr as *i8);
let file = c_str_to_static_slice(file as *i8);
begin_unwind(expr, file, line)
}
}
#[cold]
#[lang="fail_bounds_check"]
#[cfg(not(test))]
fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
#[allow(ctypes)]
extern { fn rust_fail_bounds_check(file: *u8, line: uint,
index: uint, len: uint,) -> !; }
unsafe { rust_fail_bounds_check(file, line, index, len) }
}
#[cold]
pub fn begin_unwind(msg: &str, file: &'static str, line: uint) -> ! {
#[allow(ctypes)]
extern { fn rust_begin_unwind(msg: &str, file: &'static str,
line: uint) -> !; }
unsafe { rust_begin_unwind(msg, file, line) }
}

View File

@ -30,11 +30,15 @@ use std::unstable::finally::Finally;
```
*/
#![experimental]
use ops::Drop;
#[cfg(test)] use task::failing;
/// A trait for executing a destructor unconditionally after a block of code,
/// regardless of whether the blocked fails.
pub trait Finally<T> {
/// Executes this object, unconditionally running `dtor` after this block of
/// code has run.
fn finally(&mut self, dtor: ||) -> T;
}
@ -111,49 +115,55 @@ impl<'a,A> Drop for Finallyalizer<'a,A> {
}
}
#[test]
fn test_success() {
let mut i = 0;
try_finally(
&mut i, (),
|i, ()| {
*i = 10;
},
|i| {
assert!(!failing());
assert_eq!(*i, 10);
*i = 20;
});
assert_eq!(i, 20);
}
#[cfg(test)]
mod test {
use super::{try_finally, Finally};
use realstd::task::failing;
#[test]
#[should_fail]
fn test_fail() {
let mut i = 0;
try_finally(
&mut i, (),
|i, ()| {
*i = 10;
fail!();
},
|i| {
assert!(failing());
assert_eq!(*i, 10);
})
}
#[test]
fn test_success() {
let mut i = 0;
try_finally(
&mut i, (),
|i, ()| {
*i = 10;
},
|i| {
assert!(!failing());
assert_eq!(*i, 10);
*i = 20;
});
assert_eq!(i, 20);
}
#[test]
fn test_retval() {
let mut closure: || -> int = || 10;
let i = closure.finally(|| { });
assert_eq!(i, 10);
}
#[test]
#[should_fail]
fn test_fail() {
let mut i = 0;
try_finally(
&mut i, (),
|i, ()| {
*i = 10;
fail!();
},
|i| {
assert!(failing());
assert_eq!(*i, 10);
})
}
#[test]
fn test_compact() {
fn do_some_fallible_work() {}
fn but_always_run_this_function() { }
let mut f = do_some_fallible_work;
f.finally(but_always_run_this_function);
#[test]
fn test_retval() {
let mut closure: || -> int = || 10;
let i = closure.finally(|| { });
assert_eq!(i, 10);
}
#[test]
fn test_compact() {
fn do_some_fallible_work() {}
fn but_always_run_this_function() { }
let mut f = do_some_fallible_work;
f.finally(but_always_run_this_function);
}
}

View File

@ -41,11 +41,12 @@ A quick refresher on memory ordering:
*/
#![experimental]
#![allow(missing_doc)]
// This is needed to prevent duplicate lang item definitions.
#[cfg(test)]
pub use realstd::intrinsics::{TyDesc, Opaque, TyVisitor, TypeId};
pub use realcore::intrinsics::{TyDesc, Opaque, TyVisitor, TypeId};
pub type GlueFn = extern "Rust" fn(*i8);
@ -470,7 +471,7 @@ extern "rust-intrinsic" {
/// `TypeId` represents a globally unique identifier for a type
#[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and
// middle/lang_items.rs
#[deriving(Eq, Hash, Show, TotalEq)]
#[deriving(Eq, TotalEq)]
#[cfg(not(test))]
pub struct TypeId {
t: u64,
@ -482,4 +483,5 @@ impl TypeId {
pub fn of<T: 'static>() -> TypeId {
unsafe { type_id::<T>() }
}
pub fn hash(&self) -> u64 { self.t }
}

View File

@ -969,7 +969,7 @@ impl<A: TotalOrd, T: Iterator<A>> OrdIterator<A> for T {
}
/// `MinMaxResult` is an enum returned by `min_max`. See `OrdIterator::min_max` for more detail.
#[deriving(Clone, Eq, Show)]
#[deriving(Clone, Eq)]
pub enum MinMaxResult<T> {
/// Empty iterator
NoElements,
@ -1090,7 +1090,7 @@ impl<A, T: Clone + RandomAccessIterator<A>> RandomAccessIterator<A> for Cycle<T>
pub struct Chain<T, U> {
a: T,
b: U,
flag: bool
flag: bool,
}
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
@ -2329,13 +2329,13 @@ pub mod order {
#[cfg(test)]
mod tests {
use super::*;
use prelude::*;
use realstd::prelude::*;
use realstd::iter::*;
use realstd::num;
use cmp;
use owned::Box;
use uint;
use num;
#[test]
fn test_counter_from_iter() {

114
src/libcore/lib.rs Normal file
View File

@ -0,0 +1,114 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! The Rust core library
#![crate_id = "core#0.11-pre"]
#![license = "MIT/ASL2"]
#![crate_type = "rlib"]
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://static.rust-lang.org/doc/master")]
#![no_std]
#![feature(globs, macro_rules, managed_boxes, phase)]
#![deny(missing_doc)]
#[cfg(test)] extern crate realcore = "core";
#[cfg(test)] extern crate libc;
#[cfg(test)] extern crate native;
#[phase(syntax, link)] #[cfg(test)] extern crate realstd = "std";
#[phase(syntax, link)] #[cfg(test)] extern crate log;
#[cfg(test)] pub use cmp = realcore::cmp;
#[cfg(test)] pub use kinds = realcore::kinds;
#[cfg(test)] pub use ops = realcore::ops;
#[cfg(test)] pub use owned = realcore::owned;
#[cfg(test)] pub use ty = realcore::ty;
#[cfg(not(test))]
mod macros;
#[path = "num/float_macros.rs"] mod float_macros;
#[path = "num/int_macros.rs"] mod int_macros;
#[path = "num/uint_macros.rs"] mod uint_macros;
#[path = "num/int.rs"] pub mod int;
#[path = "num/i8.rs"] pub mod i8;
#[path = "num/i16.rs"] pub mod i16;
#[path = "num/i32.rs"] pub mod i32;
#[path = "num/i64.rs"] pub mod i64;
#[path = "num/uint.rs"] pub mod uint;
#[path = "num/u8.rs"] pub mod u8;
#[path = "num/u16.rs"] pub mod u16;
#[path = "num/u32.rs"] pub mod u32;
#[path = "num/u64.rs"] pub mod u64;
#[path = "num/f32.rs"] pub mod f32;
#[path = "num/f64.rs"] pub mod f64;
pub mod num;
/* The libcore prelude, not as all-encompassing as the libstd prelude */
pub mod prelude;
/* Core modules for ownership management */
pub mod cast;
pub mod intrinsics;
pub mod mem;
pub mod ptr;
/* Core language traits */
#[cfg(not(test))] pub mod kinds;
#[cfg(not(test))] pub mod ops;
#[cfg(not(test))] pub mod ty;
#[cfg(not(test))] pub mod cmp;
#[cfg(not(test))] pub mod owned;
pub mod clone;
pub mod default;
pub mod container;
/* Core types and methods on primitives */
mod unicode;
mod unit;
pub mod any;
pub mod bool;
pub mod cell;
pub mod char;
pub mod finally;
pub mod iter;
pub mod option;
pub mod raw;
pub mod result;
pub mod slice;
pub mod str;
pub mod tuple;
mod failure;
// FIXME: this module should not exist. Once owned allocations are no longer a
// language type, this module can move outside to the owned allocation
// crate.
mod should_not_exist;
mod std {
pub use clone;
pub use cmp;
#[cfg(test)] pub use realstd::fmt; // needed for fail!()
#[cfg(test)] pub use realstd::rt; // needed for fail!()
#[cfg(test)] pub use realstd::option; // needed for assert!()
#[cfg(test)] pub use realstd::os; // needed for tests
}

38
src/libcore/macros.rs Normal file
View File

@ -0,0 +1,38 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![macro_escape]
/// Entry point of failure, for details, see std::macros
#[macro_export]
macro_rules! fail(
() => (
fail!("explicit failure")
);
($msg:expr) => (
::failure::begin_unwind($msg, file!(), line!())
);
)
/// Runtime assertion, for details see std::macros
#[macro_export]
macro_rules! assert(
($cond:expr) => (
if !$cond {
fail!(concat!("assertion failed: ", stringify!($cond)))
}
);
)
/// Runtime assertion, disableable at compile time
#[macro_export]
macro_rules! debug_assert(
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
)

View File

@ -13,8 +13,6 @@
//! This module contains functions for querying the size and alignment of
//! types, initializing and manipulating memory.
#![allow(missing_doc)] // FIXME
use cast;
use ptr;
use intrinsics;
@ -295,7 +293,7 @@ pub fn drop<T>(_x: T) { }
mod tests {
use mem::*;
use option::{Some,None};
use str::StrSlice;
use realstd::str::StrAllocating;
#[test]
fn size_of_basic() {

227
src/libcore/num/f32.rs Normal file
View File

@ -0,0 +1,227 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for 32-bits floats (`f32` type)
use default::Default;
use intrinsics;
use num::{Zero, One, Bounded, Signed, Num, Primitive};
#[cfg(not(test))] use cmp::{Eq, Ord};
#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg};
pub static RADIX: uint = 2u;
pub static MANTISSA_DIGITS: uint = 24u;
pub static DIGITS: uint = 6u;
pub static EPSILON: f32 = 1.19209290e-07_f32;
/// Smallest finite f32 value
pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
/// Smallest positive, normalized f32 value
pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
/// Largest finite f32 value
pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
pub static MIN_EXP: int = -125;
pub static MAX_EXP: int = 128;
pub static MIN_10_EXP: int = -37;
pub static MAX_10_EXP: int = 38;
pub static NAN: f32 = 0.0_f32/0.0_f32;
pub static INFINITY: f32 = 1.0_f32/0.0_f32;
pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
/// Various useful constants.
pub mod consts {
// FIXME: replace with mathematical constants from cmath.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.
/// Archimedes' constant
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
/// pi * 2.0
pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32;
/// pi/2.0
pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
/// pi/3.0
pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
/// pi/4.0
pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
/// pi/6.0
pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
/// pi/8.0
pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
/// 1.0/pi
pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
/// 2.0/pi
pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
/// 2.0/sqrt(pi)
pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
/// sqrt(2.0)
pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
/// 1.0/sqrt(2.0)
pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
/// Euler's number
pub static E: f32 = 2.71828182845904523536028747135266250_f32;
/// log2(e)
pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
/// log10(e)
pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
/// ln(2.0)
pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32;
/// ln(10.0)
pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
}
#[cfg(not(test))]
impl Ord for f32 {
#[inline]
fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
}
#[cfg(not(test))]
impl Eq for f32 {
#[inline]
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
}
impl Num for f32 {}
impl Default for f32 {
#[inline]
fn default() -> f32 { 0.0 }
}
impl Primitive for f32 {}
impl Zero for f32 {
#[inline]
fn zero() -> f32 { 0.0 }
/// Returns true if the number is equal to either `0.0` or `-0.0`
#[inline]
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
}
impl One for f32 {
#[inline]
fn one() -> f32 { 1.0 }
}
#[cfg(not(test))]
impl Add<f32,f32> for f32 {
#[inline]
fn add(&self, other: &f32) -> f32 { *self + *other }
}
#[cfg(not(test))]
impl Sub<f32,f32> for f32 {
#[inline]
fn sub(&self, other: &f32) -> f32 { *self - *other }
}
#[cfg(not(test))]
impl Mul<f32,f32> for f32 {
#[inline]
fn mul(&self, other: &f32) -> f32 { *self * *other }
}
#[cfg(not(test))]
impl Div<f32,f32> for f32 {
#[inline]
fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(not(test))]
impl Rem<f32,f32> for f32 {
#[inline]
fn rem(&self, other: &f32) -> f32 {
extern { fn fmodf(a: f32, b: f32) -> f32; }
unsafe { fmodf(*self, *other) }
}
}
#[cfg(not(test))]
impl Neg<f32> for f32 {
#[inline]
fn neg(&self) -> f32 { -*self }
}
impl Signed for f32 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f32 {
unsafe { intrinsics::fabsf32(*self) }
}
/// The positive difference of two numbers. Returns `0.0` if the number is
/// less than or equal to `other`, otherwise the difference between`self`
/// and `other` is returned.
#[inline]
fn abs_sub(&self, other: &f32) -> f32 {
extern { fn fdimf(a: f32, b: f32) -> f32; }
unsafe { fdimf(*self, *other) }
}
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f32 {
if self != self { NAN } else {
unsafe { intrinsics::copysignf32(1.0, *self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Bounded for f32 {
// NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
#[inline]
fn min_value() -> f32 { -MAX_VALUE }
#[inline]
fn max_value() -> f32 { MAX_VALUE }
}

227
src/libcore/num/f64.rs Normal file
View File

@ -0,0 +1,227 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for 64-bits floats (`f64` type)
use default::Default;
use intrinsics;
use num::{Zero, One, Bounded, Signed, Num, Primitive};
#[cfg(not(test))] use cmp::{Eq, Ord};
#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg};
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective
// members of `Bounded` and `Float`.
pub static RADIX: uint = 2u;
pub static MANTISSA_DIGITS: uint = 53u;
pub static DIGITS: uint = 15u;
pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
/// Smallest finite f64 value
pub static MIN_VALUE: f64 = -1.7976931348623157e+308_f64;
/// Smallest positive, normalized f64 value
pub static MIN_POS_VALUE: f64 = 2.2250738585072014e-308_f64;
/// Largest finite f64 value
pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;
pub static MIN_EXP: int = -1021;
pub static MAX_EXP: int = 1024;
pub static MIN_10_EXP: int = -307;
pub static MAX_10_EXP: int = 308;
pub static NAN: f64 = 0.0_f64/0.0_f64;
pub static INFINITY: f64 = 1.0_f64/0.0_f64;
pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
/// Various useful constants.
pub mod consts {
// FIXME: replace with mathematical constants from cmath.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.
/// Archimedes' constant
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
/// pi * 2.0
pub static PI_2: f64 = 6.28318530717958647692528676655900576_f64;
/// pi/2.0
pub static FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
/// pi/3.0
pub static FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
/// pi/4.0
pub static FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
/// pi/6.0
pub static FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
/// pi/8.0
pub static FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
/// 1.0/pi
pub static FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
/// 2.0/pi
pub static FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
/// 2.0/sqrt(pi)
pub static FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64;
/// sqrt(2.0)
pub static SQRT2: f64 = 1.41421356237309504880168872420969808_f64;
/// 1.0/sqrt(2.0)
pub static FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64;
/// Euler's number
pub static E: f64 = 2.71828182845904523536028747135266250_f64;
/// log2(e)
pub static LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
/// log10(e)
pub static LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
/// ln(2.0)
pub static LN_2: f64 = 0.693147180559945309417232121458176568_f64;
/// ln(10.0)
pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
}
#[cfg(not(test))]
impl Ord for f64 {
#[inline]
fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
}
#[cfg(not(test))]
impl Eq for f64 {
#[inline]
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
}
impl Default for f64 {
#[inline]
fn default() -> f64 { 0.0 }
}
impl Primitive for f64 {}
impl Num for f64 {}
impl Zero for f64 {
#[inline]
fn zero() -> f64 { 0.0 }
/// Returns true if the number is equal to either `0.0` or `-0.0`
#[inline]
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
}
impl One for f64 {
#[inline]
fn one() -> f64 { 1.0 }
}
#[cfg(not(test))]
impl Add<f64,f64> for f64 {
#[inline]
fn add(&self, other: &f64) -> f64 { *self + *other }
}
#[cfg(not(test))]
impl Sub<f64,f64> for f64 {
#[inline]
fn sub(&self, other: &f64) -> f64 { *self - *other }
}
#[cfg(not(test))]
impl Mul<f64,f64> for f64 {
#[inline]
fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(not(test))]
impl Div<f64,f64> for f64 {
#[inline]
fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(not(test))]
impl Rem<f64,f64> for f64 {
#[inline]
fn rem(&self, other: &f64) -> f64 {
extern { fn fmod(a: f64, b: f64) -> f64; }
unsafe { fmod(*self, *other) }
}
}
#[cfg(not(test))]
impl Neg<f64> for f64 {
#[inline]
fn neg(&self) -> f64 { -*self }
}
impl Signed for f64 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f64 {
unsafe { intrinsics::fabsf64(*self) }
}
/// The positive difference of two numbers. Returns `0.0` if the number is less than or
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
#[inline]
fn abs_sub(&self, other: &f64) -> f64 {
extern { fn fdim(a: f64, b: f64) -> f64; }
unsafe { fdim(*self, *other) }
}
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f64 {
if self != self { NAN } else {
unsafe { intrinsics::copysignf64(1.0, *self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Bounded for f64 {
// NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
#[inline]
fn min_value() -> f64 { -MAX_VALUE }
#[inline]
fn max_value() -> f64 { MAX_VALUE }
}

View File

@ -0,0 +1,20 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![macro_escape]
#![doc(hidden)]
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);
})
)

72
src/libcore/num/i16.rs Normal file
View File

@ -0,0 +1,72 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for signed 16-bits integers (`i16` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
use option::{Option, Some, None};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
int_module!(i16, 16)
impl Bitwise for i16 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self as u16) as i16 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self as u16) as i16 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self as u16) as i16 } }
}
impl CheckedAdd for i16 {
#[inline]
fn checked_add(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i16 {
#[inline]
fn checked_sub(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i16 {
#[inline]
fn checked_mul(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

72
src/libcore/num/i32.rs Normal file
View File

@ -0,0 +1,72 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for signed 32-bits integers (`i32` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
use option::{Option, Some, None};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
int_module!(i32, 32)
impl Bitwise for i32 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self as u32) as i32 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self as u32) as i32 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self as u32) as i32 } }
}
impl CheckedAdd for i32 {
#[inline]
fn checked_add(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i32 {
#[inline]
fn checked_sub(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i32 {
#[inline]
fn checked_mul(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

71
src/libcore/num/i64.rs Normal file
View File

@ -0,0 +1,71 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for signed 64-bits integers (`i64` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
use option::{Option, Some, None};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
int_module!(i64, 64)
impl Bitwise for i64 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self as u64) as i64 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self as u64) as i64 } }
/// Counts the number of trailing zeros.
#[inline]
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self as u64) as i64 } }
}
impl CheckedAdd for i64 {
#[inline]
fn checked_add(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i64 {
#[inline]
fn checked_sub(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i64 {
#[inline]
fn checked_mul(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

72
src/libcore/num/i8.rs Normal file
View File

@ -0,0 +1,72 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for signed 8-bits integers (`i8` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
use option::{Option, Some, None};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
int_module!(i8, 8)
impl Bitwise for i8 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self as u8) as i8 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self as u8) as i8 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self as u8) as i8 } }
}
impl CheckedAdd for i8 {
#[inline]
fn checked_add(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i8 {
#[inline]
fn checked_sub(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i8 {
#[inline]
fn checked_mul(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

127
src/libcore/num/int.rs Normal file
View File

@ -0,0 +1,127 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for architecture-sized signed integers (`int` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int};
use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul};
use option::{Option, Some, None};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
#[cfg(target_word_size = "32")] int_module!(int, 32)
#[cfg(target_word_size = "64")] int_module!(int, 64)
#[cfg(target_word_size = "32")]
impl Bitwise for int {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> int { (*self as i32).count_ones() as int }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
}
#[cfg(target_word_size = "64")]
impl Bitwise for int {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> int { (*self as i64).count_ones() as int }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
}
#[cfg(target_word_size = "32")]
impl CheckedAdd for int {
#[inline]
fn checked_add(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedAdd for int {
#[inline]
fn checked_add(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedSub for int {
#[inline]
fn checked_sub(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedSub for int {
#[inline]
fn checked_sub(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedMul for int {
#[inline]
fn checked_mul(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedMul for int {
#[inline]
fn checked_mul(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}

View File

@ -0,0 +1,338 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![macro_escape]
#![doc(hidden)]
macro_rules! int_module (($T:ty, $bits:expr) => (
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
pub static BITS : uint = $bits;
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
pub static BYTES : uint = ($bits / 8);
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::min_value` function.
pub static MIN: $T = (-1 as $T) << (BITS - 1);
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::max_value` function.
pub static MAX: $T = !MIN;
#[cfg(not(test))]
impl Ord for $T {
#[inline]
fn lt(&self, other: &$T) -> bool { *self < *other }
}
#[cfg(not(test))]
impl TotalEq for $T {}
#[cfg(not(test))]
impl Eq for $T {
#[inline]
fn eq(&self, other: &$T) -> bool { *self == *other }
}
#[cfg(not(test))]
impl TotalOrd for $T {
#[inline]
fn cmp(&self, other: &$T) -> Ordering {
if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
}
}
impl Num for $T {}
impl Zero for $T {
#[inline]
fn zero() -> $T { 0 }
#[inline]
fn is_zero(&self) -> bool { *self == 0 }
}
impl One for $T {
#[inline]
fn one() -> $T { 1 }
}
#[cfg(not(test))]
impl Add<$T,$T> for $T {
#[inline]
fn add(&self, other: &$T) -> $T { *self + *other }
}
#[cfg(not(test))]
impl Sub<$T,$T> for $T {
#[inline]
fn sub(&self, other: &$T) -> $T { *self - *other }
}
#[cfg(not(test))]
impl Mul<$T,$T> for $T {
#[inline]
fn mul(&self, other: &$T) -> $T { *self * *other }
}
#[cfg(not(test))]
impl Div<$T,$T> for $T {
/// Integer division, truncated towards 0.
///
/// # Examples
///
/// ~~~
/// assert!( 8 / 3 == 2);
/// assert!( 8 / -3 == -2);
/// assert!(-8 / 3 == -2);
/// assert!(-8 / -3 == 2);
///
/// assert!( 1 / 2 == 0);
/// assert!( 1 / -2 == 0);
/// assert!(-1 / 2 == 0);
/// assert!(-1 / -2 == 0);
/// ~~~
#[inline]
fn div(&self, other: &$T) -> $T { *self / *other }
}
#[cfg(not(test))]
impl Rem<$T,$T> for $T {
/// Returns the integer remainder after division, satisfying:
///
/// ~~~
/// # let n = 1;
/// # let d = 2;
/// assert!((n / d) * d + (n % d) == n)
/// ~~~
///
/// # Examples
///
/// ~~~
/// assert!( 8 % 3 == 2);
/// assert!( 8 % -3 == 2);
/// assert!(-8 % 3 == -2);
/// assert!(-8 % -3 == -2);
///
/// assert!( 1 % 2 == 1);
/// assert!( 1 % -2 == 1);
/// assert!(-1 % 2 == -1);
/// assert!(-1 % -2 == -1);
/// ~~~
#[inline]
fn rem(&self, other: &$T) -> $T { *self % *other }
}
#[cfg(not(test))]
impl Neg<$T> for $T {
#[inline]
fn neg(&self) -> $T { -*self }
}
impl Signed for $T {
/// Computes the absolute value
#[inline]
fn abs(&self) -> $T {
if self.is_negative() { -*self } else { *self }
}
///
/// The positive difference of two numbers. Returns `0` if the number is less than or
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
///
#[inline]
fn abs_sub(&self, other: &$T) -> $T {
if *self <= *other { 0 } else { *self - *other }
}
///
/// # Returns
///
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
///
#[inline]
fn signum(&self) -> $T {
match *self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
/// Returns true if the number is positive
#[inline]
fn is_positive(&self) -> bool { *self > 0 }
/// Returns true if the number is negative
#[inline]
fn is_negative(&self) -> bool { *self < 0 }
}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
fn bitor(&self, other: &$T) -> $T { *self | *other }
}
#[cfg(not(test))]
impl BitAnd<$T,$T> for $T {
#[inline]
fn bitand(&self, other: &$T) -> $T { *self & *other }
}
#[cfg(not(test))]
impl BitXor<$T,$T> for $T {
#[inline]
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
}
#[cfg(not(test))]
impl Shl<$T,$T> for $T {
#[inline]
fn shl(&self, other: &$T) -> $T { *self << *other }
}
#[cfg(not(test))]
impl Shr<$T,$T> for $T {
#[inline]
fn shr(&self, other: &$T) -> $T { *self >> *other }
}
#[cfg(not(test))]
impl Not<$T> for $T {
#[inline]
fn not(&self) -> $T { !*self }
}
impl Bounded for $T {
#[inline]
fn min_value() -> $T { MIN }
#[inline]
fn max_value() -> $T { MAX }
}
impl CheckedDiv for $T {
#[inline]
fn checked_div(&self, v: &$T) -> Option<$T> {
if *v == 0 || (*self == MIN && *v == -1) {
None
} else {
Some(self / *v)
}
}
}
impl Default for $T {
#[inline]
fn default() -> $T { 0 }
}
impl Int for $T {}
impl Primitive for $T {}
#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use int;
use num;
use num::Bitwise;
use num::CheckedDiv;
#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert!(MIN + MAX + 1 == 0);
}
#[test]
fn test_num() {
num::test_num(10 as $T, 2 as $T);
}
#[test]
pub fn test_abs() {
assert!((1 as $T).abs() == 1 as $T);
assert!((0 as $T).abs() == 0 as $T);
assert!((-1 as $T).abs() == 1 as $T);
}
#[test]
fn test_abs_sub() {
assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
}
#[test]
fn test_signum() {
assert!((1 as $T).signum() == 1 as $T);
assert!((0 as $T).signum() == 0 as $T);
assert!((-0 as $T).signum() == 0 as $T);
assert!((-1 as $T).signum() == -1 as $T);
}
#[test]
fn test_is_positive() {
assert!((1 as $T).is_positive());
assert!(!(0 as $T).is_positive());
assert!(!(-0 as $T).is_positive());
assert!(!(-1 as $T).is_positive());
}
#[test]
fn test_is_negative() {
assert!(!(1 as $T).is_negative());
assert!(!(0 as $T).is_negative());
assert!(!(-0 as $T).is_negative());
assert!((-1 as $T).is_negative());
}
#[test]
fn test_bitwise() {
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
}
#[test]
fn test_count_ones() {
assert!((0b0101100 as $T).count_ones() == 3);
assert!((0b0100001 as $T).count_ones() == 2);
assert!((0b1111001 as $T).count_ones() == 5);
}
#[test]
fn test_count_zeros() {
assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
}
#[test]
fn test_signed_checked_div() {
assert!(10i.checked_div(&2) == Some(5));
assert!(5i.checked_div(&0) == None);
assert!(int::MIN.checked_div(&-1) == None);
}
}
))

876
src/libcore/num/mod.rs Normal file
View File

@ -0,0 +1,876 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Numeric traits and functions for generic mathematics
//!
//! These are implemented for the primitive numeric types in `std::{u8, u16,
//! u32, u64, uint, i8, i16, i32, i64, int, f32, f64, float}`.
#![allow(missing_doc)]
use clone::Clone;
use cmp::{Eq, Ord};
use kinds::Copy;
use mem::size_of;
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use option::{Option, Some, None};
/// The base trait for numeric types
pub trait Num: Eq + Zero + One
+ Neg<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}
/// Simultaneous division and remainder
#[inline]
pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
(x / y, x % y)
}
/// Defines an additive identity element for `Self`.
///
/// # Deriving
///
/// This trait can be automatically be derived using `#[deriving(Zero)]`
/// attribute. If you choose to use this, make sure that the laws outlined in
/// the documentation for `Zero::zero` still hold.
pub trait Zero: Add<Self, Self> {
/// Returns the additive identity element of `Self`, `0`.
///
/// # Laws
///
/// ~~~notrust
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ~~~
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
// FIXME (#5527): This should be an associated constant
fn zero() -> Self;
/// Returns `true` if `self` is equal to the additive identity.
fn is_zero(&self) -> bool;
}
/// Returns the additive identity, `0`.
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
/// Defines a multiplicative identity element for `Self`.
pub trait One: Mul<Self, Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
///
/// # Laws
///
/// ~~~notrust
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ~~~
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
// FIXME (#5527): This should be an associated constant
fn one() -> Self;
}
/// Returns the multiplicative identity, `1`.
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
/// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> {
/// Computes the absolute value.
///
/// For float, f32, and f64, `NaN` will be returned if the number is `NaN`.
fn abs(&self) -> Self;
/// The positive difference of two numbers.
///
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
/// between `self` and `other` is returned.
fn abs_sub(&self, other: &Self) -> Self;
/// Returns the sign of the number.
///
/// For `float`, `f32`, `f64`:
/// * `1.0` if the number is positive, `+0.0` or `INFINITY`
/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// * `NaN` if the number is `NaN`
///
/// For `int`:
/// * `0` if the number is zero
/// * `1` if the number is positive
/// * `-1` if the number is negative
fn signum(&self) -> Self;
/// Returns true if the number is positive and false if the number is zero or negative.
fn is_positive(&self) -> bool;
/// Returns true if the number is negative and false if the number is zero or positive.
fn is_negative(&self) -> bool;
}
/// Computes the absolute value.
///
/// For float, f32, and f64, `NaN` will be returned if the number is `NaN`
#[inline(always)]
pub fn abs<T: Signed>(value: T) -> T {
value.abs()
}
/// The positive difference of two numbers.
///
/// Returns `zero` if the number is less than or equal to `other`,
/// otherwise the difference between `self` and `other` is returned.
#[inline(always)]
pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
x.abs_sub(&y)
}
/// Returns the sign of the number.
///
/// For float, f32, f64:
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is `NAN`
///
/// For int:
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
/// A trait for values which cannot be negative
pub trait Unsigned: Num {}
/// Raises a value to the power of exp, using exponentiation by squaring.
///
/// # Example
///
/// ```rust
/// use std::num;
///
/// assert_eq!(num::pow(2, 4), 16);
/// ```
#[inline]
pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
if exp == 1 { base }
else {
let mut acc = one::<T>();
while exp > 0 {
if (exp & 1) == 1 {
acc = acc * base;
}
base = base * base;
exp = exp >> 1;
}
acc
}
}
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent
fn min_value() -> Self;
/// returns the largest finite number this type can represent
fn max_value() -> Self;
}
/// Numbers with a fixed binary representation.
pub trait Bitwise: Bounded
+ Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<Self,Self>
+ Shr<Self,Self> {
/// Returns the number of ones in the binary representation of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b01001100u8;
/// assert_eq!(n.count_ones(), 3);
/// ```
fn count_ones(&self) -> Self;
/// Returns the number of zeros in the binary representation of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b01001100u8;
/// assert_eq!(n.count_zeros(), 5);
/// ```
#[inline]
fn count_zeros(&self) -> Self {
(!*self).count_ones()
}
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.leading_zeros(), 10);
/// ```
fn leading_zeros(&self) -> Self;
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.trailing_zeros(), 3);
/// ```
fn trailing_zeros(&self) -> Self;
}
/// Specifies the available operations common to all of Rust's core numeric primitives.
/// These may not always make sense from a purely mathematical point of view, but
/// may be useful for systems programming.
pub trait Primitive: Copy
+ Clone
+ Num
+ NumCast
+ Ord
+ Bounded {}
/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Primitive
+ Bitwise
+ CheckedAdd
+ CheckedSub
+ CheckedMul
+ CheckedDiv {}
/// Returns the smallest power of 2 greater than or equal to `n`.
#[inline]
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
let mut tmp: T = n - one();
let mut shift: T = one();
while shift <= halfbits {
tmp = tmp | (tmp >> shift);
shift = shift << one();
}
tmp + one()
}
// Returns `true` iff `n == 2^k` for some k.
#[inline]
pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
(n - one()) & n == zero()
}
/// Returns the smallest power of 2 greater than or equal to `n`. If the next
/// power of two is greater than the type's maximum value, `None` is returned,
/// otherwise the power of 2 is wrapped in `Some`.
#[inline]
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
let mut tmp: T = n - one();
let mut shift: T = one();
while shift <= halfbits {
tmp = tmp | (tmp >> shift);
shift = shift << one();
}
tmp.checked_add(&one())
}
/// A generic trait for converting a value to a number.
pub trait ToPrimitive {
/// Converts the value of `self` to an `int`.
#[inline]
fn to_int(&self) -> Option<int> {
self.to_i64().and_then(|x| x.to_int())
}
/// Converts the value of `self` to an `i8`.
#[inline]
fn to_i8(&self) -> Option<i8> {
self.to_i64().and_then(|x| x.to_i8())
}
/// Converts the value of `self` to an `i16`.
#[inline]
fn to_i16(&self) -> Option<i16> {
self.to_i64().and_then(|x| x.to_i16())
}
/// Converts the value of `self` to an `i32`.
#[inline]
fn to_i32(&self) -> Option<i32> {
self.to_i64().and_then(|x| x.to_i32())
}
/// Converts the value of `self` to an `i64`.
fn to_i64(&self) -> Option<i64>;
/// Converts the value of `self` to an `uint`.
#[inline]
fn to_uint(&self) -> Option<uint> {
self.to_u64().and_then(|x| x.to_uint())
}
/// Converts the value of `self` to an `u8`.
#[inline]
fn to_u8(&self) -> Option<u8> {
self.to_u64().and_then(|x| x.to_u8())
}
/// Converts the value of `self` to an `u16`.
#[inline]
fn to_u16(&self) -> Option<u16> {
self.to_u64().and_then(|x| x.to_u16())
}
/// Converts the value of `self` to an `u32`.
#[inline]
fn to_u32(&self) -> Option<u32> {
self.to_u64().and_then(|x| x.to_u32())
}
/// Converts the value of `self` to an `u64`.
#[inline]
fn to_u64(&self) -> Option<u64>;
/// Converts the value of `self` to an `f32`.
#[inline]
fn to_f32(&self) -> Option<f32> {
self.to_f64().and_then(|x| x.to_f32())
}
/// Converts the value of `self` to an `f64`.
#[inline]
fn to_f64(&self) -> Option<f64> {
self.to_i64().and_then(|x| x.to_f64())
}
}
macro_rules! impl_to_primitive_int_to_int(
($SrcT:ty, $DstT:ty) => (
{
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as i64;
let min_value: $DstT = Bounded::min_value();
let max_value: $DstT = Bounded::max_value();
if min_value as i64 <= n && n <= max_value as i64 {
Some(*self as $DstT)
} else {
None
}
}
}
)
)
macro_rules! impl_to_primitive_int_to_uint(
($SrcT:ty, $DstT:ty) => (
{
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
if zero <= *self && *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_int(
($T:ty) => (
impl ToPrimitive for $T {
#[inline]
fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int) }
#[inline]
fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8) }
#[inline]
fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16) }
#[inline]
fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32) }
#[inline]
fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64) }
#[inline]
fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint) }
#[inline]
fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8) }
#[inline]
fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16) }
#[inline]
fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32) }
#[inline]
fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64) }
#[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_int!(int)
impl_to_primitive_int!(i8)
impl_to_primitive_int!(i16)
impl_to_primitive_int!(i32)
impl_to_primitive_int!(i64)
macro_rules! impl_to_primitive_uint_to_int(
($DstT:ty) => (
{
let max_value: $DstT = Bounded::max_value();
if *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_uint_to_uint(
($SrcT:ty, $DstT:ty) => (
{
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
if zero <= *self && *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
}
)
)
macro_rules! impl_to_primitive_uint(
($T:ty) => (
impl ToPrimitive for $T {
#[inline]
fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int) }
#[inline]
fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8) }
#[inline]
fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16) }
#[inline]
fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32) }
#[inline]
fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64) }
#[inline]
fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint) }
#[inline]
fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8) }
#[inline]
fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16) }
#[inline]
fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32) }
#[inline]
fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64) }
#[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_uint!(uint)
impl_to_primitive_uint!(u8)
impl_to_primitive_uint!(u16)
impl_to_primitive_uint!(u32)
impl_to_primitive_uint!(u64)
macro_rules! impl_to_primitive_float_to_float(
($SrcT:ty, $DstT:ty) => (
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as f64;
let max_value: $SrcT = Bounded::max_value();
if -max_value as f64 <= n && n <= max_value as f64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_float(
($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_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32) }
#[inline]
fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64) }
}
)
)
impl_to_primitive_float!(f32)
impl_to_primitive_float!(f64)
/// 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.
#[inline]
fn from_int(n: int) -> Option<Self> {
FromPrimitive::from_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// Convert an `i64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
fn from_i64(n: i64) -> Option<Self>;
/// Convert an `uint` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn from_uint(n: uint) -> Option<Self> {
FromPrimitive::from_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// Convert an `u64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
fn from_u64(n: u64) -> Option<Self>;
/// 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_f64(n as f64)
}
/// 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_i64(n as i64)
}
}
/// 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, $to_ty:expr) => (
impl FromPrimitive for $T {
#[inline] fn from_int(n: int) -> Option<$T> { $to_ty }
#[inline] fn from_i8(n: i8) -> Option<$T> { $to_ty }
#[inline] fn from_i16(n: i16) -> Option<$T> { $to_ty }
#[inline] fn from_i32(n: i32) -> Option<$T> { $to_ty }
#[inline] fn from_i64(n: i64) -> Option<$T> { $to_ty }
#[inline] fn from_uint(n: uint) -> Option<$T> { $to_ty }
#[inline] fn from_u8(n: u8) -> Option<$T> { $to_ty }
#[inline] fn from_u16(n: u16) -> Option<$T> { $to_ty }
#[inline] fn from_u32(n: u32) -> Option<$T> { $to_ty }
#[inline] fn from_u64(n: u64) -> Option<$T> { $to_ty }
#[inline] fn from_f32(n: f32) -> Option<$T> { $to_ty }
#[inline] fn from_f64(n: f64) -> Option<$T> { $to_ty }
}
)
)
impl_from_primitive!(int, n.to_int())
impl_from_primitive!(i8, n.to_i8())
impl_from_primitive!(i16, n.to_i16())
impl_from_primitive!(i32, n.to_i32())
impl_from_primitive!(i64, n.to_i64())
impl_from_primitive!(uint, n.to_uint())
impl_from_primitive!(u8, n.to_u8())
impl_from_primitive!(u16, n.to_u16())
impl_from_primitive!(u32, n.to_u32())
impl_from_primitive!(u64, n.to_u64())
impl_from_primitive!(f32, n.to_f32())
impl_from_primitive!(f64, n.to_f64())
/// Cast from one machine scalar to another.
///
/// # Example
///
/// ```
/// use std::num;
///
/// let twenty: f32 = num::cast(0x14).unwrap();
/// assert_eq!(twenty, 20f32);
/// ```
///
#[inline]
pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
NumCast::from(n)
}
/// An interface for casting between machine scalars.
pub trait NumCast: ToPrimitive {
/// Creates a number from another value that can be converted into a primitive via the
/// `ToPrimitive` trait.
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: ToPrimitive>(n: N) -> Option<$T> {
// `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment
n.$conv()
}
}
)
)
impl_num_cast!(u8, to_u8)
impl_num_cast!(u16, to_u16)
impl_num_cast!(u32, to_u32)
impl_num_cast!(u64, to_u64)
impl_num_cast!(uint, to_uint)
impl_num_cast!(i8, to_i8)
impl_num_cast!(i16, to_i16)
impl_num_cast!(i32, to_i32)
impl_num_cast!(i64, to_i64)
impl_num_cast!(int, to_int)
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 + Ord + 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.
fn checked_add(&self, v: &Self) -> Option<Self>;
}
/// Performs subtraction that returns `None` instead of wrapping around on underflow.
pub trait CheckedSub: Sub<Self, Self> {
/// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
fn checked_sub(&self, v: &Self) -> Option<Self>;
}
/// Performs multiplication that returns `None` instead of wrapping around on underflow or
/// overflow.
pub trait CheckedMul: Mul<Self, Self> {
/// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
/// happens, `None` is returned.
fn checked_mul(&self, v: &Self) -> Option<Self>;
}
/// Performs division that returns `None` instead of wrapping around on underflow or overflow.
pub trait CheckedDiv: Div<Self, Self> {
/// Divides two numbers, checking for underflow or overflow. If underflow or overflow happens,
/// `None` is returned.
fn checked_div(&self, v: &Self) -> Option<Self>;
}
/// Helper function for testing numeric operations
#[cfg(test)]
pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) {
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);
assert_eq!(ten.mul(&two), ten * two);
assert_eq!(ten.div(&two), ten / two);
assert_eq!(ten.rem(&two), ten % two);
}

56
src/libcore/num/u16.rs Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for unsigned 16-bits integers (`u16` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use option::{Some, None, Option};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
uint_module!(u16, i16, 16)
impl CheckedAdd for u16 {
#[inline]
fn checked_add(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u16 {
#[inline]
fn checked_sub(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u16 {
#[inline]
fn checked_mul(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

56
src/libcore/num/u32.rs Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for unsigned 32-bits integers (`u32` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use option::{Some, None, Option};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
uint_module!(u32, i32, 32)
impl CheckedAdd for u32 {
#[inline]
fn checked_add(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u32 {
#[inline]
fn checked_sub(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u32 {
#[inline]
fn checked_mul(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

56
src/libcore/num/u64.rs Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for unsigned 64-bits integer (`u64` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use option::{Some, None, Option};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
uint_module!(u64, i64, 64)
impl CheckedAdd for u64 {
#[inline]
fn checked_add(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u64 {
#[inline]
fn checked_sub(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u64 {
#[inline]
fn checked_mul(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

56
src/libcore/num/u8.rs Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for unsigned 8-bits integers (`u8` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use option::{Some, None, Option};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
uint_module!(u8, i8, 8)
impl CheckedAdd for u8 {
#[inline]
fn checked_add(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u8 {
#[inline]
fn checked_sub(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u8 {
#[inline]
fn checked_mul(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}

92
src/libcore/num/uint.rs Normal file
View File

@ -0,0 +1,92 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations and constants for architecture-sized unsigned integers (`uint` type)
use default::Default;
use intrinsics;
use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use option::{Some, None, Option};
#[cfg(not(test))]
use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
#[cfg(not(test))]
use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
#[cfg(not(test))]
use ops::{Shl, Shr, Not};
uint_module!(uint, int, ::int::BITS)
#[cfg(target_word_size = "32")]
impl CheckedAdd for uint {
#[inline]
fn checked_add(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_add_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedAdd for uint {
#[inline]
fn checked_add(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_add_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedSub for uint {
#[inline]
fn checked_sub(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_sub_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedSub for uint {
#[inline]
fn checked_sub(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_sub_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedMul for uint {
#[inline]
fn checked_mul(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_mul_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedMul for uint {
#[inline]
fn checked_mul(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_mul_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}

View File

@ -0,0 +1,235 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![macro_escape]
#![doc(hidden)]
macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
pub static BITS : uint = $bits;
pub static BYTES : uint = ($bits / 8);
pub static MIN: $T = 0 as $T;
pub static MAX: $T = 0 as $T - 1 as $T;
#[cfg(not(test))]
impl Ord for $T {
#[inline]
fn lt(&self, other: &$T) -> bool { *self < *other }
}
#[cfg(not(test))]
impl TotalEq for $T {}
#[cfg(not(test))]
impl Eq for $T {
#[inline]
fn eq(&self, other: &$T) -> bool { *self == *other }
}
#[cfg(not(test))]
impl TotalOrd for $T {
#[inline]
fn cmp(&self, other: &$T) -> Ordering {
if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
}
}
impl Num for $T {}
impl Zero for $T {
#[inline]
fn zero() -> $T { 0 }
#[inline]
fn is_zero(&self) -> bool { *self == 0 }
}
impl One for $T {
#[inline]
fn one() -> $T { 1 }
}
#[cfg(not(test))]
impl Add<$T,$T> for $T {
#[inline]
fn add(&self, other: &$T) -> $T { *self + *other }
}
#[cfg(not(test))]
impl Sub<$T,$T> for $T {
#[inline]
fn sub(&self, other: &$T) -> $T { *self - *other }
}
#[cfg(not(test))]
impl Mul<$T,$T> for $T {
#[inline]
fn mul(&self, other: &$T) -> $T { *self * *other }
}
#[cfg(not(test))]
impl Div<$T,$T> for $T {
#[inline]
fn div(&self, other: &$T) -> $T { *self / *other }
}
#[cfg(not(test))]
impl Rem<$T,$T> for $T {
#[inline]
fn rem(&self, other: &$T) -> $T { *self % *other }
}
#[cfg(not(test))]
impl Neg<$T> for $T {
#[inline]
fn neg(&self) -> $T { -(*self as $T_SIGNED) as $T }
}
impl Unsigned for $T {}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
fn bitor(&self, other: &$T) -> $T { *self | *other }
}
#[cfg(not(test))]
impl BitAnd<$T,$T> for $T {
#[inline]
fn bitand(&self, other: &$T) -> $T { *self & *other }
}
#[cfg(not(test))]
impl BitXor<$T,$T> for $T {
#[inline]
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
}
#[cfg(not(test))]
impl Shl<$T,$T> for $T {
#[inline]
fn shl(&self, other: &$T) -> $T { *self << *other }
}
#[cfg(not(test))]
impl Shr<$T,$T> for $T {
#[inline]
fn shr(&self, other: &$T) -> $T { *self >> *other }
}
#[cfg(not(test))]
impl Not<$T> for $T {
#[inline]
fn not(&self) -> $T { !*self }
}
impl Bounded for $T {
#[inline]
fn min_value() -> $T { MIN }
#[inline]
fn max_value() -> $T { MAX }
}
impl Bitwise for $T {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> $T {
(*self as $T_SIGNED).count_ones() as $T
}
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> $T {
(*self as $T_SIGNED).leading_zeros() as $T
}
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> $T {
(*self as $T_SIGNED).trailing_zeros() as $T
}
}
impl CheckedDiv for $T {
#[inline]
fn checked_div(&self, v: &$T) -> Option<$T> {
if *v == 0 {
None
} else {
Some(self / *v)
}
}
}
impl Int for $T {}
impl Primitive for $T {}
impl Default for $T {
#[inline]
fn default() -> $T { 0 }
}
#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use num;
use num::CheckedDiv;
use num::Bitwise;
#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert!(MIN + MAX + 1 == 0);
}
#[test]
fn test_num() {
num::test_num(10 as $T, 2 as $T);
}
#[test]
fn test_bitwise() {
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
}
#[test]
fn test_count_ones() {
assert!((0b0101100 as $T).count_ones() == 3);
assert!((0b0100001 as $T).count_ones() == 2);
assert!((0b1111001 as $T).count_ones() == 5);
}
#[test]
fn test_count_zeros() {
assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
}
#[test]
fn test_unsigned_checked_div() {
assert!(10u.checked_div(&2) == Some(5));
assert!(5u.checked_div(&0) == None);
}
}
))

866
src/libcore/option.rs Normal file
View File

@ -0,0 +1,866 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Optional values
//!
//! Type `Option` represents an optional value: every `Option`
//! is either `Some` and contains a value, or `None`, and
//! does not. `Option` types are very common in Rust code, as
//! they have a number of uses:
//!
//! * Initial values
//! * Return values for functions that are not defined
//! over their entire input range (partial functions)
//! * Return value for otherwise reporting simple errors, where `None` is
//! returned on error
//! * Optional struct fields
//! * Struct fields that can be loaned or "taken"
//! * Optional function arguments
//! * Nullable pointers
//! * Swapping things out of difficult situations
//!
//! Options are commonly paired with pattern matching to query the presence
//! of a value and take action, always accounting for the `None` case.
//!
//! ```
//! # // FIXME This is not the greatest first example
//! // cow_says contains the word "moo"
//! let cow_says = Some("moo");
//! // dog_says does not contain a value
//! let dog_says: Option<&str> = None;
//!
//! // Pattern match to retrieve the value
//! match (cow_says, dog_says) {
//! (Some(cow_words), Some(dog_words)) => {
//! println!("Cow says {} and dog says {}!", cow_words, dog_words);
//! }
//! (Some(cow_words), None) => println!("Cow says {}", cow_words),
//! (None, Some(dog_words)) => println!("Dog says {}", dog_words),
//! (None, None) => println!("Cow and dog are suspiciously silent")
//! }
//! ```
//!
//
// FIXME: Show how `Option` is used in practice, with lots of methods
//
//! # Options and pointers ("nullable" pointers)
//!
//! Rust's pointer types must always point to a valid location; there are
//! no "null" pointers. Instead, Rust has *optional* pointers, like
//! the optional owned box, `Option<Box<T>>`.
//!
//! The following example uses `Option` to create an optional box of
//! `int`. Notice that in order to use the inner `int` value first the
//! `check_optional` function needs to use pattern matching to
//! determine whether the box has a value (i.e. it is `Some(...)`) or
//! not (`None`).
//!
//! ```
//! let optional: Option<Box<int>> = None;
//! check_optional(&optional);
//!
//! let optional: Option<Box<int>> = Some(box 9000);
//! check_optional(&optional);
//!
//! fn check_optional(optional: &Option<Box<int>>) {
//! match *optional {
//! Some(ref p) => println!("have value {}", p),
//! None => println!("have no value")
//! }
//! }
//! ```
//!
//! This usage of `Option` to create safe nullable pointers is so
//! common that Rust does special optimizations to make the
//! representation of `Option<Box<T>>` a single pointer. Optional pointers
//! in Rust are stored as efficiently as any other pointer type.
//!
//! # Examples
//!
//! Basic pattern matching on `Option`:
//!
//! ```
//! let msg = Some("howdy");
//!
//! // Take a reference to the contained string
//! match msg {
//! Some(ref m) => println!("{}", *m),
//! None => ()
//! }
//!
//! // Remove the contained string, destroying the Option
//! let unwrapped_msg = match msg {
//! Some(m) => m,
//! None => "default message"
//! };
//! ```
//!
//! Initialize a result to `None` before a loop:
//!
//! ```
//! enum Kingdom { Plant(uint, &'static str), Animal(uint, &'static str) }
//!
//! // A list of data to search through.
//! let all_the_big_things = [
//! Plant(250, "redwood"),
//! Plant(230, "noble fir"),
//! Plant(229, "sugar pine"),
//! Animal(25, "blue whale"),
//! Animal(19, "fin whale"),
//! Animal(15, "north pacific right whale"),
//! ];
//!
//! // We're going to search for the name of the biggest animal,
//! // but to start with we've just got `None`.
//! let mut name_of_biggest_animal = None;
//! let mut size_of_biggest_animal = 0;
//! for big_thing in all_the_big_things.iter() {
//! match *big_thing {
//! Animal(size, name) if size > size_of_biggest_animal => {
//! // Now we've found the name of some big animal
//! size_of_biggest_animal = size;
//! name_of_biggest_animal = Some(name);
//! }
//! Animal(..) | Plant(..) => ()
//! }
//! }
//!
//! match name_of_biggest_animal {
//! Some(name) => println!("the biggest animal is {}", name),
//! None => println!("there are no animals :(")
//! }
//! ```
use cmp::{Eq, TotalEq, TotalOrd};
use default::Default;
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
use mem;
use slice;
/// The `Option`
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
pub enum Option<T> {
/// No value
None,
/// Some value `T`
Some(T)
}
/////////////////////////////////////////////////////////////////////////////
// Type implementation
/////////////////////////////////////////////////////////////////////////////
impl<T> Option<T> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////
/// Returns `true` if the option is a `Some` value
#[inline]
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false
}
}
/// Returns `true` if the option is a `None` value
#[inline]
pub fn is_none(&self) -> bool {
!self.is_some()
}
/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
/// Convert from `Option<T>` to `Option<&T>`
///
/// # Example
///
/// Convert an `Option<~str>` into an `Option<int>`, preserving the original.
/// The `map` method takes the `self` argument by value, consuming the original,
/// so this technique uses `as_ref` to first take an `Option` to a reference
/// to the value inside the original.
///
/// ```
/// let num_as_str: Option<~str> = Some("10".to_owned());
/// // First, cast `Option<~str>` to `Option<&~str>` with `as_ref`,
/// // then consume *that* with `map`, leaving `num_as_str` on the stack.
/// let num_as_int: Option<uint> = num_as_str.as_ref().map(|n| n.len());
/// println!("still can print num_as_str: {}", num_as_str);
/// ```
#[inline]
pub fn as_ref<'r>(&'r self) -> Option<&'r T> {
match *self { Some(ref x) => Some(x), None => None }
}
/// Convert from `Option<T>` to `Option<&mut T>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
match *self { Some(ref mut x) => Some(x), None => None }
}
/// Convert from `Option<T>` to `&[T]` (without copying)
#[inline]
pub fn as_slice<'r>(&'r self) -> &'r [T] {
match *self {
Some(ref x) => slice::ref_slice(x),
None => &[]
}
}
/// Convert from `Option<T>` to `&mut [T]` (without copying)
#[inline]
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
match *self {
Some(ref mut x) => slice::mut_ref_slice(x),
None => &mut []
}
}
/////////////////////////////////////////////////////////////////////////
// Getting to contained values
/////////////////////////////////////////////////////////////////////////
/// Moves a value out of an option type and returns it, consuming the `Option`.
///
/// # Failure
///
/// Fails if the self value equals `None`.
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged.
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn unwrap(self) -> T {
match self {
Some(val) => val,
None => fail!("called `Option::unwrap()` on a `None` value"),
}
}
/// Returns the contained value or a default.
#[inline]
pub fn unwrap_or(self, def: T) -> T {
match self {
Some(x) => x,
None => def
}
}
/// Returns the contained value or computes it from a closure.
#[inline]
pub fn unwrap_or_else(self, f: || -> T) -> T {
match self {
Some(x) => x,
None => f()
}
}
/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////
/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
///
/// # Example
///
/// Convert an `Option<~str>` into an `Option<uint>`, consuming the original:
///
/// ```
/// let num_as_str: Option<~str> = Some("10".to_owned());
/// // `Option::map` takes self *by value*, consuming `num_as_str`
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
/// ```
#[inline]
pub fn map<U>(self, f: |T| -> U) -> Option<U> {
match self { Some(x) => Some(f(x)), None => None }
}
/// Applies a function to the contained value or returns a default.
#[inline]
pub fn map_or<U>(self, def: U, f: |T| -> U) -> U {
match self { None => def, Some(t) => f(t) }
}
/// Applies a function to the contained value or does nothing.
/// Returns true if the contained value was mutated.
pub fn mutate(&mut self, f: |T| -> T) -> bool {
if self.is_some() {
*self = Some(f(self.take_unwrap()));
true
} else { false }
}
/// Applies a function to the contained value or sets it to a default.
/// Returns true if the contained value was mutated, or false if set to the default.
pub fn mutate_or_set(&mut self, def: T, f: |T| -> T) -> bool {
if self.is_some() {
*self = Some(f(self.take_unwrap()));
true
} else {
*self = Some(def);
false
}
}
/////////////////////////////////////////////////////////////////////////
// Iterator constructors
/////////////////////////////////////////////////////////////////////////
/// Returns an iterator over the possibly contained value.
#[inline]
pub fn iter<'r>(&'r self) -> Item<&'r T> {
Item{opt: self.as_ref()}
}
/// Returns a mutable iterator over the possibly contained value.
#[inline]
pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> {
Item{opt: self.as_mut()}
}
/// Returns a consuming iterator over the possibly contained value.
#[inline]
pub fn move_iter(self) -> Item<T> {
Item{opt: self}
}
/////////////////////////////////////////////////////////////////////////
// Boolean operations on the values, eager and lazy
/////////////////////////////////////////////////////////////////////////
/// Returns `None` if the option is `None`, otherwise returns `optb`.
#[inline]
pub fn and<U>(self, optb: Option<U>) -> Option<U> {
match self {
Some(_) => optb,
None => None,
}
}
/// Returns `None` if the option is `None`, otherwise calls `f` with the
/// wrapped value and returns the result.
#[inline]
pub fn and_then<U>(self, f: |T| -> Option<U>) -> Option<U> {
match self {
Some(x) => f(x),
None => None,
}
}
/// Returns the option if it contains a value, otherwise returns `optb`.
#[inline]
pub fn or(self, optb: Option<T>) -> Option<T> {
match self {
Some(_) => self,
None => optb
}
}
/// Returns the option if it contains a value, otherwise calls `f` and
/// returns the result.
#[inline]
pub fn or_else(self, f: || -> Option<T>) -> Option<T> {
match self {
Some(_) => self,
None => f()
}
}
/////////////////////////////////////////////////////////////////////////
// Misc
/////////////////////////////////////////////////////////////////////////
/// Takes the value out of the option, leaving a `None` in its place.
#[inline]
pub fn take(&mut self) -> Option<T> {
mem::replace(self, None)
}
/// Filters an optional value using a given function.
#[inline(always)]
pub fn filtered(self, f: |t: &T| -> bool) -> Option<T> {
match self {
Some(x) => if f(&x) { Some(x) } else { None },
None => None
}
}
/// Applies a function zero or more times until the result is `None`.
#[inline]
pub fn while_some(self, f: |v: T| -> Option<T>) {
let mut opt = self;
loop {
match opt {
Some(x) => opt = f(x),
None => break
}
}
}
/////////////////////////////////////////////////////////////////////////
// Common special cases
/////////////////////////////////////////////////////////////////////////
/// The option dance. Moves a value out of an option type and returns it,
/// replacing the original with `None`.
///
/// # Failure
///
/// Fails if the value equals `None`.
#[inline]
pub fn take_unwrap(&mut self) -> T {
match self.take() {
Some(x) => x,
None => fail!("called `Option::take_unwrap()` on a `None` value")
}
}
/// Gets an immutable reference to the value inside an option.
///
/// # Failure
///
/// Fails if the value equals `None`
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged
/// (calling `get` on `None` is akin to dereferencing a null pointer).
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn get_ref<'a>(&'a self) -> &'a T {
match *self {
Some(ref x) => x,
None => fail!("called `Option::get_ref()` on a `None` value"),
}
}
/// Gets a mutable reference to the value inside an option.
///
/// # Failure
///
/// Fails if the value equals `None`
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged
/// (calling `get` on `None` is akin to dereferencing a null pointer).
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
match *self {
Some(ref mut x) => x,
None => fail!("called `Option::get_mut_ref()` on a `None` value"),
}
}
}
impl<T: Default> Option<T> {
/// Returns the contained value or a default
///
/// Consumes the `self` argument then, if `Some`, returns the contained
/// value, otherwise if `None`, returns the default value for that
/// type.
///
/// # Example
///
/// Convert a string to an integer, turning poorly-formed strings
/// into 0 (the default value for integers). `from_str` converts
/// a string to any other type that implements `FromStr`, returning
/// `None` on error.
///
/// ```
/// let good_year_from_input = "1909";
/// let bad_year_from_input = "190blarg";
/// let good_year = from_str(good_year_from_input).unwrap_or_default();
/// let bad_year = from_str(bad_year_from_input).unwrap_or_default();
///
/// assert_eq!(1909, good_year);
/// assert_eq!(0, bad_year);
/// ```
#[inline]
pub fn unwrap_or_default(self) -> T {
match self {
Some(x) => x,
None => Default::default()
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Trait implementations
/////////////////////////////////////////////////////////////////////////////
impl<T> Default for Option<T> {
#[inline]
fn default() -> Option<T> { None }
}
/////////////////////////////////////////////////////////////////////////////
// The Option Iterator
/////////////////////////////////////////////////////////////////////////////
/// An `Option` iterator that yields either one or zero elements
///
/// The `Item` iterator is returned by the `iter`, `mut_iter` and `move_iter`
/// methods on `Option`.
#[deriving(Clone)]
pub struct Item<A> {
opt: Option<A>
}
impl<A> Iterator<A> for Item<A> {
#[inline]
fn next(&mut self) -> Option<A> {
self.opt.take()
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
match self.opt {
Some(_) => (1, Some(1)),
None => (0, Some(0)),
}
}
}
impl<A> DoubleEndedIterator<A> for Item<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
self.opt.take()
}
}
impl<A> ExactSize<A> for Item<A> {}
/////////////////////////////////////////////////////////////////////////////
// Free functions
/////////////////////////////////////////////////////////////////////////////
/// Takes each element in the `Iterator`: if it is `None`, no further
/// elements are taken, and the `None` is returned. Should no `None` occur, a
/// vector containing the values of each `Option` is returned.
///
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Option<uint> {
/// if x == uint::MAX { return None; }
/// else { return Some(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Some(~[2u, 3, 4]));
#[inline]
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
let mut iter = iter.scan(false, |state, x| {
match x {
Some(x) => Some(x),
None => {
*state = true;
None
}
}
});
let v: V = FromIterator::from_iter(iter.by_ref());
if iter.state {
None
} else {
Some(v)
}
}
/////////////////////////////////////////////////////////////////////////////
// Tests
/////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use realstd::option::collect;
use realstd::prelude::*;
use realstd::iter::range;
use str::StrSlice;
use kinds::marker;
use slice::ImmutableVector;
#[test]
fn test_get_ptr() {
unsafe {
let x = box 0;
let addr_x: *int = ::cast::transmute(&*x);
let opt = Some(x);
let y = opt.unwrap();
let addr_y: *int = ::cast::transmute(&*y);
assert_eq!(addr_x, addr_y);
}
}
#[test]
fn test_get_str() {
let x = "test".to_owned();
let addr_x = x.as_ptr();
let opt = Some(x);
let y = opt.unwrap();
let addr_y = y.as_ptr();
assert_eq!(addr_x, addr_y);
}
#[test]
fn test_get_resource() {
use realstd::rc::Rc;
use cell::RefCell;
struct R {
i: Rc<RefCell<int>>,
}
#[unsafe_destructor]
impl ::ops::Drop for R {
fn drop(&mut self) {
let ii = &*self.i;
let i = ii.borrow().clone();
*ii.borrow_mut() = i + 1;
}
}
fn R(i: Rc<RefCell<int>>) -> R {
R {
i: i
}
}
let i = Rc::new(RefCell::new(0));
{
let x = R(i.clone());
let opt = Some(x);
let _y = opt.unwrap();
}
assert_eq!(*i.borrow(), 1);
}
#[test]
fn test_option_dance() {
let x = Some(());
let mut y = Some(5);
let mut y2 = 0;
for _x in x.iter() {
y2 = y.take_unwrap();
}
assert_eq!(y2, 5);
assert!(y.is_none());
}
#[test] #[should_fail]
fn test_option_too_much_dance() {
let mut y = Some(marker::NoCopy);
let _y2 = y.take_unwrap();
let _y3 = y.take_unwrap();
}
#[test]
fn test_and() {
let x: Option<int> = Some(1);
assert_eq!(x.and(Some(2)), Some(2));
assert_eq!(x.and(None::<int>), None);
let x: Option<int> = None;
assert_eq!(x.and(Some(2)), None);
assert_eq!(x.and(None::<int>), None);
}
#[test]
fn test_and_then() {
let x: Option<int> = Some(1);
assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
assert_eq!(x.and_then(|_| None::<int>), None);
let x: Option<int> = None;
assert_eq!(x.and_then(|x| Some(x + 1)), None);
assert_eq!(x.and_then(|_| None::<int>), None);
}
#[test]
fn test_or() {
let x: Option<int> = Some(1);
assert_eq!(x.or(Some(2)), Some(1));
assert_eq!(x.or(None), Some(1));
let x: Option<int> = None;
assert_eq!(x.or(Some(2)), Some(2));
assert_eq!(x.or(None), None);
}
#[test]
fn test_or_else() {
let x: Option<int> = Some(1);
assert_eq!(x.or_else(|| Some(2)), Some(1));
assert_eq!(x.or_else(|| None), Some(1));
let x: Option<int> = None;
assert_eq!(x.or_else(|| Some(2)), Some(2));
assert_eq!(x.or_else(|| None), None);
}
#[test]
fn test_option_while_some() {
let mut i = 0;
Some(10).while_some(|j| {
i += 1;
if j > 0 {
Some(j-1)
} else {
None
}
});
assert_eq!(i, 11);
}
#[test]
fn test_unwrap() {
assert_eq!(Some(1).unwrap(), 1);
assert_eq!(Some("hello".to_owned()).unwrap(), "hello".to_owned());
}
#[test]
#[should_fail]
fn test_unwrap_fail1() {
let x: Option<int> = None;
x.unwrap();
}
#[test]
#[should_fail]
fn test_unwrap_fail2() {
let x: Option<~str> = None;
x.unwrap();
}
#[test]
fn test_unwrap_or() {
let x: Option<int> = Some(1);
assert_eq!(x.unwrap_or(2), 1);
let x: Option<int> = None;
assert_eq!(x.unwrap_or(2), 2);
}
#[test]
fn test_unwrap_or_else() {
let x: Option<int> = Some(1);
assert_eq!(x.unwrap_or_else(|| 2), 1);
let x: Option<int> = None;
assert_eq!(x.unwrap_or_else(|| 2), 2);
}
#[test]
fn test_filtered() {
let some_stuff = Some(42);
let modified_stuff = some_stuff.filtered(|&x| {x < 10});
assert_eq!(some_stuff.unwrap(), 42);
assert!(modified_stuff.is_none());
}
#[test]
fn test_iter() {
let val = 5;
let x = Some(val);
let mut it = x.iter();
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(&val));
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
}
#[test]
fn test_mut_iter() {
let val = 5;
let new_val = 11;
let mut x = Some(val);
{
let mut it = x.mut_iter();
assert_eq!(it.size_hint(), (1, Some(1)));
match it.next() {
Some(interior) => {
assert_eq!(*interior, val);
*interior = new_val;
}
None => assert!(false),
}
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
}
assert_eq!(x, Some(new_val));
}
#[test]
fn test_ord() {
let small = Some(1.0);
let big = Some(5.0);
let nan = Some(0.0/0.0);
assert!(!(nan < big));
assert!(!(nan > big));
assert!(small < big);
assert!(None < big);
assert!(big > None);
}
#[test]
fn test_mutate() {
let mut x = Some(3i);
assert!(x.mutate(|i| i+1));
assert_eq!(x, Some(4i));
assert!(x.mutate_or_set(0, |i| i+1));
assert_eq!(x, Some(5i));
x = None;
assert!(!x.mutate(|i| i+1));
assert_eq!(x, None);
assert!(!x.mutate_or_set(0i, |i| i+1));
assert_eq!(x, Some(0i));
}
#[test]
fn test_collect() {
let v: Option<~[int]> = collect(range(0, 0)
.map(|_| Some(0)));
assert_eq!(v, Some(box []));
let v: Option<~[int]> = collect(range(0, 3)
.map(|x| Some(x)));
assert_eq!(v, Some(box [0, 1, 2]));
let v: Option<~[int]> = collect(range(0, 3)
.map(|x| if x > 1 { None } else { Some(x) }));
assert_eq!(v, None);
// test that it does not take more elements than it needs
let mut functions = [|| Some(()), || None, || fail!()];
let v: Option<~[()]> = collect(functions.mut_iter().map(|f| (*f)()));
assert_eq!(v, None);
}
}

View File

@ -10,7 +10,10 @@
//! Operations on unique pointer types
#[cfg(not(test))] use cmp::*;
// FIXME: this module should not exist in libcore. It must currently because the
// Box implementation is quite ad-hoc in the compiler. Once there is
// proper support in the compiler this type will be able to be defined in
// its own module.
/// A value that represents the global exchange heap. This is the default
/// place that the `box` keyword allocates into when no place is supplied.
@ -33,32 +36,3 @@ pub struct Box<T>(*T);
#[cfg(test)]
pub struct Box<T>(*T);
#[cfg(not(test))]
impl<T:Eq> Eq for Box<T> {
#[inline]
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for Box<T> {
#[inline]
fn lt(&self, other: &Box<T>) -> bool { *(*self) < *(*other) }
#[inline]
fn le(&self, other: &Box<T>) -> bool { *(*self) <= *(*other) }
#[inline]
fn ge(&self, other: &Box<T>) -> bool { *(*self) >= *(*other) }
#[inline]
fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for Box<T> {
#[inline]
fn cmp(&self, other: &Box<T>) -> Ordering { (**self).cmp(*other) }
}
#[cfg(not(test))]
impl<T: TotalEq> TotalEq for Box<T> {}

47
src/libcore/prelude.rs Normal file
View File

@ -0,0 +1,47 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! The core prelude
//!
//! For more information, see std::prelude.
// Reexported core operators
pub use kinds::{Copy, Send, Sized, Share};
pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not};
pub use ops::{BitAnd, BitOr, BitXor};
pub use ops::{Drop, Deref, DerefMut};
pub use ops::{Shl, Shr, Index};
pub use option::{Option, Some, None};
pub use result::{Result, Ok, Err};
// Reexported functions
pub use iter::range;
pub use mem::drop;
// Reexported types and traits
pub use char::Char;
pub use clone::Clone;
pub use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Signed, Unsigned};
pub use num::{Primitive, Int, ToPrimitive, FromPrimitive};
pub use ptr::RawPtr;
pub use str::{Str, StrSlice};
pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};
pub use slice::{ImmutableEqVector, ImmutableTotalOrdVector};
pub use slice::{MutableVector};
pub use slice::{Vector, ImmutableVector};

View File

@ -92,14 +92,12 @@
use cast;
use clone::Clone;
#[cfg(not(test))]
use cmp::Equiv;
use intrinsics;
use iter::{range, Iterator};
use mem;
use option::{Option, Some, None};
use intrinsics;
use option::{Some, None, Option};
#[cfg(not(test))] use cmp::{Eq, TotalEq, Ord};
#[cfg(not(test))] use cmp::{Eq, TotalEq, Ord, Equiv};
/// Return the offset of the first null pointer in `buf`.
#[inline]
@ -377,7 +375,9 @@ impl<T> RawPtr<T> for *mut T {
fn to_uint(&self) -> uint { *self as uint }
#[inline]
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T }
unsafe fn offset(self, count: int) -> *mut T {
intrinsics::offset(self as *T, count) as *mut T
}
#[inline]
unsafe fn to_option(&self) -> Option<&T> {
@ -444,10 +444,6 @@ mod externfnpointers {
let other_: *() = unsafe { cast::transmute(*other) };
self_ == other_
}
#[inline]
fn ne(&self, other: &extern "C" fn() -> _R) -> bool {
!self.eq(other)
}
}
macro_rules! fnptreq(
($($p:ident),*) => {
@ -458,10 +454,6 @@ mod externfnpointers {
let other_: *() = unsafe { cast::transmute(*other) };
self_ == other_
}
#[inline]
fn ne(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
!self.eq(other)
}
}
}
)
@ -476,52 +468,24 @@ mod externfnpointers {
#[cfg(not(test))]
impl<T> Ord for *T {
#[inline]
fn lt(&self, other: &*T) -> bool {
*self < *other
}
#[inline]
fn le(&self, other: &*T) -> bool {
*self <= *other
}
#[inline]
fn ge(&self, other: &*T) -> bool {
*self >= *other
}
#[inline]
fn gt(&self, other: &*T) -> bool {
*self > *other
}
fn lt(&self, other: &*T) -> bool { *self < *other }
}
#[cfg(not(test))]
impl<T> Ord for *mut T {
#[inline]
fn lt(&self, other: &*mut T) -> bool {
*self < *other
}
#[inline]
fn le(&self, other: &*mut T) -> bool {
*self <= *other
}
#[inline]
fn ge(&self, other: &*mut T) -> bool {
*self >= *other
}
#[inline]
fn gt(&self, other: &*mut T) -> bool {
*self > *other
}
fn lt(&self, other: &*mut T) -> bool { *self < *other }
}
#[cfg(test)]
pub mod ptr_tests {
use super::*;
use prelude::*;
use realstd::prelude::*;
use c_str::ToCStr;
use realstd::c_str::ToCStr;
use cast;
use libc;
use str;
use realstd::str;
use slice::{ImmutableVector, MutableVector};
#[test]

View File

@ -9,6 +9,7 @@
// except according to those terms.
#![allow(missing_doc)]
#![experimental]
//! Contains struct definitions for the layout of compiler built-in types.
//!

752
src/libcore/result.rs Normal file
View File

@ -0,0 +1,752 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Error handling with the `Result` type
//!
//! `Result<T>` is the type used for returning and propagating
//! errors. It is an enum with the variants, `Ok(T)`, representing
//! success and containing a value, and `Err(E)`, representing error
//! and containing an error value.
//!
//! ~~~
//! enum Result<T, E> {
//! Ok(T),
//! Err(E)
//! }
//! ~~~
//!
//! Functions return `Result` whenever errors are expected and
//! recoverable. In the `std` crate `Result` is most prominently used
//! for [I/O](../io/index.html).
//!
//! A simple function returning `Result` might be
//! defined and used like so:
//!
//! ~~~
//! #[deriving(Show)]
//! enum Version { Version1, Version2 }
//!
//! fn parse_version(header: &[u8]) -> Result<Version, &'static str> {
//! if header.len() < 1 {
//! return Err("invalid header length");
//! }
//! match header[0] {
//! 1 => Ok(Version1),
//! 2 => Ok(Version2),
//! _ => Err("invalid version")
//! }
//! }
//!
//! let version = parse_version(&[1, 2, 3, 4]);
//! match version {
//! Ok(v) => {
//! println!("working with version: {}", v);
//! }
//! Err(e) => {
//! println!("error parsing header: {}", e);
//! }
//! }
//! ~~~
//!
//! Pattern matching on `Result`s is clear and straightforward for
//! simple cases, but `Result` comes with some convenience methods
//! that make working it more succinct.
//!
//! ~~~
//! let good_result: Result<int, int> = Ok(10);
//! let bad_result: Result<int, int> = Err(10);
//!
//! // The `is_ok` and `is_err` methods do what they say.
//! assert!(good_result.is_ok() && !good_result.is_err());
//! assert!(bad_result.is_err() && !bad_result.is_ok());
//!
//! // `map` consumes the `Result` and produces another.
//! let good_result: Result<int, int> = good_result.map(|i| i + 1);
//! let bad_result: Result<int, int> = bad_result.map(|i| i - 1);
//!
//! // Use `and_then` to continue the computation.
//! let good_result: Result<bool, int> = good_result.and_then(|i| Ok(i == 11));
//!
//! // Use `or_else` to handle the error.
//! let bad_result: Result<int, int> = bad_result.or_else(|i| Ok(11));
//!
//! // Consume the result and return the contents with `unwrap`.
//! let final_awesome_result = good_result.ok().unwrap();
//! ~~~
//!
//! # Results must be used
//!
//! A common problem with using return values to indicate errors is
//! that it is easy to ignore the return value, thus failing to handle
//! the error. Result is annotated with the #[must_use] attribute,
//! which will cause the compiler to issue a warning when a Result
//! value is ignored. This makes `Result` especially useful with
//! functions that may encounter errors but don't otherwise return a
//! useful value.
//!
//! Consider the `write_line` method defined for I/O types
//! by the [`Writer`](../io/trait.Writer.html) trait:
//!
//! ~~~
//! use std::io::IoError;
//!
//! trait Writer {
//! fn write_line(&mut self, s: &str) -> Result<(), IoError>;
//! }
//! ~~~
//!
//! *Note: The actual definition of `Writer` uses `IoResult`, which
//! is just a synonym for `Result<T, IoError>`.*
//!
//! This method doesn`t produce a value, but the write may
//! fail. It's crucial to handle the error case, and *not* write
//! something like this:
//!
//! ~~~ignore
//! use std::io::{File, Open, Write};
//!
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
//! // If `write_line` errors, then we'll never know, because the return
//! // value is ignored.
//! file.write_line("important message");
//! drop(file);
//! ~~~
//!
//! If you *do* write that in Rust, the compiler will by give you a
//! warning (by default, controlled by the `unused_must_use` lint).
//!
//! You might instead, if you don't want to handle the error, simply
//! fail, by converting to an `Option` with `ok`, then asserting
//! success with `expect`. This will fail if the write fails, proving
//! a marginally useful message indicating why:
//!
//! ~~~no_run
//! use std::io::{File, Open, Write};
//!
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
//! file.write_line("important message").ok().expect("failed to write message");
//! drop(file);
//! ~~~
//!
//! You might also simply assert success:
//!
//! ~~~no_run
//! # use std::io::{File, Open, Write};
//!
//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
//! assert!(file.write_line("important message").is_ok());
//! # drop(file);
//! ~~~
//!
//! Or propagate the error up the call stack with `try!`:
//!
//! ~~~
//! # use std::io::{File, Open, Write, IoError};
//! fn write_message() -> Result<(), IoError> {
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
//! try!(file.write_line("important message"));
//! drop(file);
//! return Ok(());
//! }
//! ~~~
//!
//! # The `try!` macro
//!
//! When writing code that calls many functions that return the
//! `Result` type, the error handling can be tedious. The `try!`
//! macro hides some of the boilerplate of propagating errors up the
//! call stack.
//!
//! It replaces this:
//!
//! ~~~
//! use std::io::{File, Open, Write, IoError};
//!
//! struct Info { name: ~str, age: int, rating: int }
//!
//! fn write_info(info: &Info) -> Result<(), IoError> {
//! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write);
//! // Early return on error
//! match file.write_line(format!("name: {}", info.name)) {
//! Ok(_) => (),
//! Err(e) => return Err(e)
//! }
//! match file.write_line(format!("age: {}", info.age)) {
//! Ok(_) => (),
//! Err(e) => return Err(e)
//! }
//! return file.write_line(format!("rating: {}", info.rating));
//! }
//! ~~~
//!
//! With this:
//!
//! ~~~
//! use std::io::{File, Open, Write, IoError};
//!
//! struct Info { name: ~str, age: int, rating: int }
//!
//! fn write_info(info: &Info) -> Result<(), IoError> {
//! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write);
//! // Early return on error
//! try!(file.write_line(format!("name: {}", info.name)));
//! try!(file.write_line(format!("age: {}", info.age)));
//! try!(file.write_line(format!("rating: {}", info.rating)));
//! return Ok(());
//! }
//! ~~~
//!
//! *It's much nicer!*
//!
//! Wrapping an expression in `try!` will result in the unwrapped
//! success (`Ok`) value, unless the result is `Err`, in which case
//! `Err` is returned early from the enclosing function. Its simple definition
//! makes it clear:
//!
//! ~~~
//! # #![feature(macro_rules)]
//! macro_rules! try(
//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
//! )
//! # fn main() { }
//! ~~~
//!
//! `try!` is imported by the prelude, and is available everywhere.
//!
//! # `Result` and `Option`
//!
//! The `Result` and [`Option`](../option/index.html) types are
//! similar and complementary: they are often employed to indicate a
//! lack of a return value; and they are trivially converted between
//! each other, so `Result`s are often handled by first converting to
//! `Option` with the [`ok`](enum.Result.html#method.ok) and
//! [`err`](enum.Result.html#method.ok) methods.
//!
//! Whereas `Option` only indicates the lack of a value, `Result` is
//! specifically for error reporting, and carries with it an error
//! value. Sometimes `Option` is used for indicating errors, but this
//! is only for simple cases and is generally discouraged. Even when
//! there is no useful error value to return, prefer `Result<T, ()>`.
//!
//! Converting to an `Option` with `ok()` to handle an error:
//!
//! ~~~
//! use std::io::Timer;
//! let mut t = Timer::new().ok().expect("failed to create timer!");
//! ~~~
//!
//! # `Result` vs. `fail!`
//!
//! `Result` is for recoverable errors; `fail!` is for unrecoverable
//! errors. Callers should always be able to avoid failure if they
//! take the proper precautions, for example, calling `is_some()`
//! on an `Option` type before calling `unwrap`.
//!
//! The suitability of `fail!` as an error handling mechanism is
//! limited by Rust's lack of any way to "catch" and resume execution
//! from a thrown exception. Therefore using failure for error
//! handling requires encapsulating fallable code in a task. Calling
//! the `fail!` macro, or invoking `fail!` indirectly should be
//! avoided as an error reporting strategy. Failure is only for
//! unrecoverable errors and a failing task is typically the sign of
//! a bug.
//!
//! A module that instead returns `Results` is alerting the caller
//! that failure is possible, and providing precise control over how
//! it is handled.
//!
//! Furthermore, failure may not be recoverable at all, depending on
//! the context. The caller of `fail!` should assume that execution
//! will not resume after failure, that failure is catastrophic.
use clone::Clone;
use cmp::Eq;
use iter::{Iterator, FromIterator};
use option::{None, Option, Some};
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
///
/// See the [`std::result`](index.html) module documentation for details.
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
#[must_use]
pub enum Result<T, E> {
/// Contains the success value
Ok(T),
/// Contains the error value
Err(E)
}
/////////////////////////////////////////////////////////////////////////////
// Type implementation
/////////////////////////////////////////////////////////////////////////////
impl<T, E> Result<T, E> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////
/// Returns true if the result is `Ok`
///
/// # Example
///
/// ~~~
/// use std::io::{File, Open, Write};
///
/// # fn do_not_run_example() { // creates a file
/// let mut file = File::open_mode(&Path::new("secret.txt"), Open, Write);
/// assert!(file.write_line("it's cold in here").is_ok());
/// # }
/// ~~~
#[inline]
pub fn is_ok(&self) -> bool {
match *self {
Ok(_) => true,
Err(_) => false
}
}
/// Returns true if the result is `Err`
///
/// # Example
///
/// ~~~
/// use std::io::{File, Open, Read};
///
/// // When opening with `Read` access, if the file does not exist
/// // then `open_mode` returns an error.
/// let bogus = File::open_mode(&Path::new("not_a_file.txt"), Open, Read);
/// assert!(bogus.is_err());
/// ~~~
#[inline]
pub fn is_err(&self) -> bool {
!self.is_ok()
}
/////////////////////////////////////////////////////////////////////////
// Adapter for each variant
/////////////////////////////////////////////////////////////////////////
/// Convert from `Result<T, E>` to `Option<T>`
///
/// Converts `self` into an `Option<T>`, consuming `self`,
/// and discarding the error, if any.
///
/// To convert to an `Option` without discarding the error value,
/// use `as_ref` to first convert the `Result<T, E>` into a
/// `Result<&T, &E>`.
///
/// # Examples
///
/// ~~~{.should_fail}
/// use std::io::{File, IoResult};
///
/// let bdays: IoResult<File> = File::open(&Path::new("important_birthdays.txt"));
/// let bdays: File = bdays.ok().expect("unable to open birthday file");
/// ~~~
#[inline]
pub fn ok(self) -> Option<T> {
match self {
Ok(x) => Some(x),
Err(_) => None,
}
}
/// Convert from `Result<T, E>` to `Option<E>`
///
/// Converts `self` into an `Option<T>`, consuming `self`,
/// and discarding the value, if any.
#[inline]
pub fn err(self) -> Option<E> {
match self {
Ok(_) => None,
Err(x) => Some(x),
}
}
/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
/// Convert from `Result<T, E>` to `Result<&T, &E>`
///
/// Produces a new `Result`, containing a reference
/// into the original, leaving the original in place.
#[inline]
pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> {
match *self {
Ok(ref x) => Ok(x),
Err(ref x) => Err(x),
}
}
/// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
match *self {
Ok(ref mut x) => Ok(x),
Err(ref mut x) => Err(x),
}
}
/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////
/// Maps a `Result<T, E>` to `Result<U, E>` by applying a function to an
/// contained `Ok` value, leaving an `Err` value untouched.
///
/// This function can be used to compose the results of two functions.
///
/// # Examples
///
/// Sum the lines of a buffer by mapping strings to numbers,
/// ignoring I/O and parse errors:
///
/// ~~~
/// use std::io::{BufReader, IoResult};
///
/// let buffer = "1\n2\n3\n4\n";
/// let mut reader = BufReader::new(buffer.as_bytes());
///
/// let mut sum = 0;
///
/// while !reader.eof() {
/// let line: IoResult<~str> = reader.read_line();
/// // Convert the string line to a number using `map` and `from_str`
/// let val: IoResult<int> = line.map(|line| {
/// from_str::<int>(line).unwrap_or(0)
/// });
/// // Add the value if there were no errors, otherwise add 0
/// sum += val.ok().unwrap_or(0);
/// }
/// ~~~
#[inline]
pub fn map<U>(self, op: |T| -> U) -> Result<U,E> {
match self {
Ok(t) => Ok(op(t)),
Err(e) => Err(e)
}
}
/// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to an
/// contained `Err` value, leaving an `Ok` value untouched.
///
/// This function can be used to pass through a successful result while handling
/// an error.
#[inline]
pub fn map_err<F>(self, op: |E| -> F) -> Result<T,F> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(op(e))
}
}
////////////////////////////////////////////////////////////////////////
// Boolean operations on the values, eager and lazy
/////////////////////////////////////////////////////////////////////////
/// Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.
#[inline]
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E> {
match self {
Ok(_) => res,
Err(e) => Err(e),
}
}
/// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
///
/// This function can be used for control flow based on result values
#[inline]
pub fn and_then<U>(self, op: |T| -> Result<U, E>) -> Result<U, E> {
match self {
Ok(t) => op(t),
Err(e) => Err(e),
}
}
/// Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
#[inline]
pub fn or(self, res: Result<T, E>) -> Result<T, E> {
match self {
Ok(_) => self,
Err(_) => res,
}
}
/// Calls `op` if the result is `Err`, otherwise returns the `Ok` value of `self`.
///
/// This function can be used for control flow based on result values
#[inline]
pub fn or_else<F>(self, op: |E| -> Result<T, F>) -> Result<T, F> {
match self {
Ok(t) => Ok(t),
Err(e) => op(e),
}
}
/// Unwraps a result, yielding the content of an `Ok`.
/// Else it returns `optb`.
#[inline]
pub fn unwrap_or(self, optb: T) -> T {
match self {
Ok(t) => t,
Err(_) => optb
}
}
/// Unwraps a result, yielding the content of an `Ok`.
/// If the value is an `Err` then it calls `op` with its value.
#[inline]
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
match self {
Ok(t) => t,
Err(e) => op(e)
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Free functions
/////////////////////////////////////////////////////////////////////////////
/// Takes each element in the `Iterator`: if it is an `Err`, no further
/// elements are taken, and the `Err` is returned. Should no `Err` occur, a
/// vector containing the values of each `Result` is returned.
///
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
/// if x == uint::MAX { return Err("overflow"); }
/// else { return Ok(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Ok(~[2u, 3, 4]));
#[inline]
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
let mut iter = iter.scan(None, |state, x| {
match x {
Ok(x) => Some(x),
Err(err) => {
*state = Some(err);
None
}
}
});
let v: V = FromIterator::from_iter(iter.by_ref());
match iter.state {
Some(err) => Err(err),
None => Ok(v),
}
}
/// Perform a fold operation over the result values from an iterator.
///
/// If an `Err` is encountered, it is immediately returned.
/// Otherwise, the folded value is returned.
#[inline]
pub fn fold<T,
V,
E,
Iter: Iterator<Result<T, E>>>(
mut iterator: Iter,
mut init: V,
f: |V, T| -> V)
-> Result<V, E> {
for t in iterator {
match t {
Ok(v) => init = f(init, v),
Err(u) => return Err(u)
}
}
Ok(init)
}
/// Perform a trivial fold operation over the result values
/// from an iterator.
///
/// If an `Err` is encountered, it is immediately returned.
/// Otherwise, a simple `Ok(())` is returned.
#[inline]
pub fn fold_<T,E,Iter:Iterator<Result<T,E>>>(iterator: Iter) -> Result<(),E> {
fold(iterator, (), |_, _| ())
}
/////////////////////////////////////////////////////////////////////////////
// Tests
/////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use realstd::result::{collect, fold, fold_};
use realstd::prelude::*;
use realstd::iter::range;
pub fn op1() -> Result<int, ~str> { Ok(666) }
pub fn op2() -> Result<int, ~str> { Err("sadface".to_owned()) }
#[test]
pub fn test_and() {
assert_eq!(op1().and(Ok(667)).unwrap(), 667);
assert_eq!(op1().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "bad".to_owned());
assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface".to_owned());
assert_eq!(op2().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "sadface".to_owned());
}
#[test]
pub fn test_and_then() {
assert_eq!(op1().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap(), 667);
assert_eq!(op1().and_then(|_| Err::<int, ~str>("bad".to_owned())).unwrap_err(),
"bad".to_owned());
assert_eq!(op2().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap_err(),
"sadface".to_owned());
assert_eq!(op2().and_then(|_| Err::<int, ~str>("bad".to_owned())).unwrap_err(),
"sadface".to_owned());
}
#[test]
pub fn test_or() {
assert_eq!(op1().or(Ok(667)).unwrap(), 666);
assert_eq!(op1().or(Err("bad".to_owned())).unwrap(), 666);
assert_eq!(op2().or(Ok(667)).unwrap(), 667);
assert_eq!(op2().or(Err("bad".to_owned())).unwrap_err(), "bad".to_owned());
}
#[test]
pub fn test_or_else() {
assert_eq!(op1().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 666);
assert_eq!(op1().or_else(|e| Err::<int, ~str>(e + "!")).unwrap(), 666);
assert_eq!(op2().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 667);
assert_eq!(op2().or_else(|e| Err::<int, ~str>(e + "!")).unwrap_err(),
"sadface!".to_owned());
}
#[test]
pub fn test_impl_map() {
assert_eq!(Ok::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Ok("ab".to_owned()));
assert_eq!(Err::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Err("a".to_owned()));
}
#[test]
pub fn test_impl_map_err() {
assert_eq!(Ok::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Ok("a".to_owned()));
assert_eq!(Err::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Err("ab".to_owned()));
}
#[test]
fn test_collect() {
let v: Result<~[int], ()> = collect(range(0, 0).map(|_| Ok::<int, ()>(0)));
assert_eq!(v, Ok(box []));
let v: Result<~[int], ()> = collect(range(0, 3).map(|x| Ok::<int, ()>(x)));
assert_eq!(v, Ok(box [0, 1, 2]));
let v: Result<~[int], int> = collect(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(x) }));
assert_eq!(v, Err(2));
// test that it does not take more elements than it needs
let mut functions = [|| Ok(()), || Err(1), || fail!()];
let v: Result<~[()], int> = collect(functions.mut_iter().map(|f| (*f)()));
assert_eq!(v, Err(1));
}
#[test]
fn test_fold() {
assert_eq!(fold_(range(0, 0)
.map(|_| Ok::<(), ()>(()))),
Ok(()));
assert_eq!(fold(range(0, 3)
.map(|x| Ok::<int, ()>(x)),
0, |a, b| a + b),
Ok(3));
assert_eq!(fold_(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(()) })),
Err(2));
// test that it does not take more elements than it needs
let mut functions = [|| Ok(()), || Err(1), || fail!()];
assert_eq!(fold_(functions.mut_iter()
.map(|f| (*f)())),
Err(1));
}
#[test]
pub fn test_to_str() {
let ok: Result<int, ~str> = Ok(100);
let err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(ok.to_str(), "Ok(100)".to_owned());
assert_eq!(err.to_str(), "Err(Err)".to_owned());
}
#[test]
pub fn test_fmt_default() {
let ok: Result<int, ~str> = Ok(100);
let err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(format!("{}", ok), "Ok(100)".to_owned());
assert_eq!(format!("{}", err), "Err(Err)".to_owned());
}
#[test]
pub fn test_unwrap_or() {
let ok: Result<int, ~str> = Ok(100);
let ok_err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(ok.unwrap_or(50), 100);
assert_eq!(ok_err.unwrap_or(50), 50);
}
#[test]
pub fn test_unwrap_or_else() {
fn handler(msg: ~str) -> int {
if msg == "I got this.".to_owned() {
50
} else {
fail!("BadBad")
}
}
let ok: Result<int, ~str> = Ok(100);
let ok_err: Result<int, ~str> = Err("I got this.".to_owned());
assert_eq!(ok.unwrap_or_handle(handler), 100);
assert_eq!(ok_err.unwrap_or_handle(handler), 50);
}
#[test]
#[should_fail]
pub fn test_unwrap_or_else_failure() {
fn handler(msg: ~str) -> int {
if msg == "I got this.".to_owned() {
50
} else {
fail!("BadBad")
}
}
let bad_err: Result<int, ~str> = Err("Unrecoverable mess.".to_owned());
let _ : int = bad_err.unwrap_or_handle(handler);
}
}

View File

@ -0,0 +1,202 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use cast;
use char::Char;
use clone::Clone;
use container::Container;
use default::Default;
use intrinsics;
use iter::{Iterator, FromIterator};
use mem;
use num::{CheckedMul, CheckedAdd};
use option::{Some, None};
use ptr::RawPtr;
use ptr;
use raw::Vec;
use slice::ImmutableVector;
use str::StrSlice;
#[cfg(not(test))] use ops::Add;
#[cfg(not(test))] use slice::Vector;
#[allow(ctypes)]
extern {
fn malloc(size: uint) -> *u8;
fn free(ptr: *u8);
}
unsafe fn alloc(cap: uint) -> *mut Vec<()> {
let cap = cap.checked_add(&mem::size_of::<Vec<()>>()).unwrap();
let ret = malloc(cap) as *mut Vec<()>;
if ret.is_null() {
intrinsics::abort();
}
(*ret).fill = 0;
(*ret).alloc = cap;
ret
}
// Strings
impl Default for ~str {
fn default() -> ~str {
unsafe {
// Get some memory
let ptr = alloc(0);
// Initialize the memory
(*ptr).fill = 0;
(*ptr).alloc = 0;
cast::transmute(ptr)
}
}
}
impl Clone for ~str {
fn clone(&self) -> ~str {
// Don't use the clone() implementation above because it'll start
// requiring the eh_personality lang item (no fun)
unsafe {
let bytes = self.as_bytes().as_ptr();
let len = self.len();
let ptr = alloc(len) as *mut Vec<u8>;
ptr::copy_nonoverlapping_memory(&mut (*ptr).data, bytes, len);
(*ptr).fill = len;
(*ptr).alloc = len;
cast::transmute(ptr)
}
}
}
impl FromIterator<char> for ~str {
#[inline]
fn from_iter<T: Iterator<char>>(mut iterator: T) -> ~str {
let (lower, _) = iterator.size_hint();
let mut cap = if lower == 0 {16} else {lower};
let mut len = 0;
let mut tmp = [0u8, ..4];
unsafe {
let mut ptr = alloc(cap) as *mut Vec<u8>;
let mut ret = cast::transmute(ptr);
for ch in iterator {
let amt = ch.encode_utf8(tmp);
if len + amt > cap {
cap = cap.checked_mul(&2).unwrap();
if cap < len + amt {
cap = len + amt;
}
let ptr2 = alloc(cap) as *mut Vec<u8>;
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
&(*ptr).data,
len);
free(ptr as *u8);
cast::forget(ret);
ret = cast::transmute(ptr2);
ptr = ptr2;
}
let base = &mut (*ptr).data as *mut u8;
for byte in tmp.slice_to(amt).iter() {
*base.offset(len as int) = *byte;
len += 1;
}
(*ptr).fill = len;
}
ret
}
}
}
#[cfg(not(test))]
impl<'a> Add<&'a str,~str> for &'a str {
#[inline]
fn add(&self, rhs: & &'a str) -> ~str {
let amt = self.len().checked_add(&rhs.len()).unwrap();
unsafe {
let ptr = alloc(amt) as *mut Vec<u8>;
let base = &mut (*ptr).data as *mut _;
ptr::copy_nonoverlapping_memory(base,
self.as_bytes().as_ptr(),
self.len());
let base = base.offset(self.len() as int);
ptr::copy_nonoverlapping_memory(base,
rhs.as_bytes().as_ptr(),
rhs.len());
(*ptr).fill = amt;
(*ptr).alloc = amt;
cast::transmute(ptr)
}
}
}
// Arrays
impl<A: Clone> Clone for ~[A] {
#[inline]
fn clone(&self) -> ~[A] {
self.iter().map(|a| a.clone()).collect()
}
}
impl<A> FromIterator<A> for ~[A] {
fn from_iter<T: Iterator<A>>(mut iterator: T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let cap = if lower == 0 {16} else {lower};
let mut cap = cap.checked_mul(&mem::size_of::<A>()).unwrap();
let mut len = 0;
unsafe {
let mut ptr = alloc(cap) as *mut Vec<A>;
let mut ret = cast::transmute(ptr);
for elt in iterator {
if len * mem::size_of::<A>() >= cap {
cap = cap.checked_mul(&2).unwrap();
let ptr2 = alloc(cap) as *mut Vec<A>;
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
&(*ptr).data,
len);
free(ptr as *u8);
cast::forget(ret);
ret = cast::transmute(ptr2);
ptr = ptr2;
}
let base = &mut (*ptr).data as *mut A;
intrinsics::move_val_init(&mut *base.offset(len as int), elt);
len += 1;
(*ptr).fill = len * mem::nonzero_size_of::<A>();
}
ret
}
}
}
#[cfg(not(test))]
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let first = self.iter().map(|t| t.clone());
first.chain(rhs.as_slice().iter().map(|t| t.clone())).collect()
}
}
#[cfg(not(test))]
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
self.as_slice() + rhs.as_slice()
}
}

1483
src/libcore/slice.rs Normal file

File diff suppressed because it is too large Load Diff

1861
src/libcore/str.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -15,8 +15,6 @@
use clone::Clone;
#[cfg(not(test))] use cmp::*;
#[cfg(not(test))] use default::Default;
use fmt;
use result::{Ok, Err};
// macro for implementing n-ary tuple functions and operations
macro_rules! tuple_impls {
@ -112,12 +110,6 @@ macro_rules! tuple_impls {
($({ let x: $T = Default::default(); x},)+)
}
}
impl<$($T: fmt::Show),+> fmt::Show for ($($T,)+) {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write_tuple!(f.buf, $(self.$refN()),+)
}
}
)+
}
}
@ -144,18 +136,6 @@ macro_rules! lexical_cmp {
($a:expr, $b:expr) => { ($a).cmp($b) };
}
macro_rules! write_tuple {
($buf:expr, $x:expr) => (
write!($buf, "({},)", *$x)
);
($buf:expr, $hd:expr, $($tl:expr),+) => ({
try!(write!($buf, "("));
try!(write!($buf, "{}", *$hd));
$(try!(write!($buf, ", {}", *$tl));)+
write!($buf, ")")
});
}
tuple_impls! {
Tuple1 {
(val0, ref0, mut0) -> A { (a) => a }
@ -266,7 +246,7 @@ mod tests {
use super::*;
use clone::Clone;
use cmp::*;
use str::StrSlice;
use realstd::str::StrAllocating;
#[test]
fn test_clone() {

5009
src/libcore/unicode.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,6 @@
use default::Default;
#[cfg(not(test))]
use cmp::{Eq, Equal, Ord, Ordering, TotalEq, TotalOrd};
use fmt;
#[cfg(not(test))]
impl Eq for () {
@ -44,9 +43,3 @@ impl Default for () {
#[inline]
fn default() -> () { () }
}
impl fmt::Show for () {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("()")
}
}

View File

@ -1398,7 +1398,6 @@ impl<T: Iterator<char>> Parser<T> {
'n' => res.push_char('\n'),
'r' => res.push_char('\r'),
't' => res.push_char('\t'),
//<<<<<<< HEAD
'u' => match try!(self.decode_hex_escape()) {
0xDC00 .. 0xDFFF => return self.error(LoneLeadingSurrogateInHexEscape),

View File

@ -981,7 +981,6 @@ mod test {
use native;
use os;
use owned::Box;
use super::*;
pub fn stress_factor() -> uint {
@ -1516,7 +1515,6 @@ mod test {
mod sync_tests {
use prelude::*;
use os;
use owned::Box;
pub fn stress_factor() -> uint {
match os::getenv("RUST_TEST_STRESS") {

View File

@ -485,20 +485,25 @@ will look like `"\\{"`.
use any;
use cast;
use cell::Cell;
use char::Char;
use cmp;
use container::Container;
use io::MemWriter;
use io;
use iter;
use iter::{Iterator, range};
use kinds::Copy;
use num::Signed;
use option::{Option,Some,None};
use option::{Option, Some, None};
use owned::Box;
use repr;
use result::{Ok, Err};
use str::StrSlice;
use result::{Ok, Err, ResultUnwrap};
use str::{StrSlice, StrAllocating, UTF16Item, ScalarValue, LoneSurrogate};
use str;
use slice::{Vector, ImmutableVector};
use slice;
use intrinsics::TypeId;
pub use self::num::radix;
pub use self::num::Radix;
@ -1241,5 +1246,144 @@ impl<T> Show for *mut T {
fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
}
macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
macro_rules! tuple (
() => ();
( $($name:ident,)+ ) => (
impl<$($name:Show),*> Show for ($($name,)*) {
#[allow(uppercase_variables, dead_assignment)]
fn fmt(&self, f: &mut Formatter) -> Result {
try!(write!(f.buf, "("));
let ($(ref $name,)*) = *self;
let mut n = 0;
$(
if n > 0 {
try!(write!(f.buf, ", "));
}
try!(write!(f.buf, "{}", *$name));
n += 1;
)*
if n == 1 {
try!(write!(f.buf, ","));
}
write!(f.buf, ")")
}
}
peel!($($name,)*)
)
)
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
impl Show for Box<any::Any> {
fn fmt(&self, f: &mut Formatter) -> Result { f.pad("Box<Any>") }
}
impl<'a> Show for &'a any::Any {
fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
}
impl<T: Show> Show for Option<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
Some(ref t) => write!(f.buf, "Some({})", *t),
None => write!(f.buf, "None"),
}
}
}
impl<T: Show, U: Show> Show for ::result::Result<T, U> {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
Ok(ref t) => write!(f.buf, "Ok({})", *t),
Err(ref t) => write!(f.buf, "Err({})", *t),
}
}
}
impl<'a, T: Show> Show for &'a [T] {
fn fmt(&self, f: &mut Formatter) -> Result {
if f.flags & (1 << (parse::FlagAlternate as uint)) == 0 {
try!(write!(f.buf, "["));
}
let mut is_first = true;
for x in self.iter() {
if is_first {
is_first = false;
} else {
try!(write!(f.buf, ", "));
}
try!(write!(f.buf, "{}", *x))
}
if f.flags & (1 << (parse::FlagAlternate as uint)) == 0 {
try!(write!(f.buf, "]"));
}
Ok(())
}
}
impl<'a, T: Show> Show for &'a mut [T] {
fn fmt(&self, f: &mut Formatter) -> Result {
secret_show(&self.as_slice(), f)
}
}
impl<T: Show> Show for ~[T] {
fn fmt(&self, f: &mut Formatter) -> Result {
secret_show(&self.as_slice(), f)
}
}
impl Show for () {
fn fmt(&self, f: &mut Formatter) -> Result {
f.pad("()")
}
}
impl Show for TypeId {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f.buf, "TypeId \\{ {} \\}", self.hash())
}
}
impl<T: Show> Show for iter::MinMaxResult<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
iter::NoElements =>
write!(f.buf, "NoElements"),
iter::OneElement(ref t) =>
write!(f.buf, "OneElement({})", *t),
iter::MinMax(ref t1, ref t2) =>
write!(f.buf, "MinMax({}, {})", *t1, *t2),
}
}
}
impl Show for cmp::Ordering {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
cmp::Less => write!(f.buf, "Less"),
cmp::Greater => write!(f.buf, "Greater"),
cmp::Equal => write!(f.buf, "Equal"),
}
}
}
impl<T: Copy + Show> Show for Cell<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f.buf, r"Cell \{ value: {} \}", self.get())
}
}
impl Show for UTF16Item {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
ScalarValue(c) => write!(f.buf, "ScalarValue({})", c),
LoneSurrogate(u) => write!(f.buf, "LoneSurrogate({})", u),
}
}
}
// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
// it's a lot easier than creating all of the rt::Piece structures here.

View File

@ -194,7 +194,7 @@ mod tests {
use fmt::radix;
use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
use super::{GenericRadix, Radix};
use str::StrSlice;
use str::StrAllocating;
#[test]
fn test_radix_base() {
@ -400,6 +400,7 @@ mod bench {
use super::test::Bencher;
use fmt::radix;
use rand::{XorShiftRng, Rng};
use realstd::result::ResultUnwrap;
#[bench]
fn format_bin(b: &mut Bencher) {
@ -436,6 +437,7 @@ mod bench {
use super::test::Bencher;
use fmt::radix;
use rand::{XorShiftRng, Rng};
use realstd::result::ResultUnwrap;
#[bench]
fn format_bin(b: &mut Bencher) {

View File

@ -10,7 +10,7 @@
//! The `FromStr` trait for types that can be created from strings
use option::Option;
use option::{Option, Some, None};
/// A trait to abstract the idea of creating a new instance of a type from a
/// string.
@ -24,3 +24,37 @@ pub trait FromStr {
pub fn from_str<A: FromStr>(s: &str) -> Option<A> {
FromStr::from_str(s)
}
impl FromStr for bool {
/// Parse a `bool` from a string.
///
/// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
///
/// # Examples
///
/// ```rust
/// assert_eq!(from_str::<bool>("true"), Some(true));
/// assert_eq!(from_str::<bool>("false"), Some(false));
/// assert_eq!(from_str::<bool>("not even a boolean"), None);
/// ```
#[inline]
fn from_str(s: &str) -> Option<bool> {
match s {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use prelude::*;
#[test]
fn test_bool_from_str() {
assert_eq!(from_str::<bool>("true"), Some(true));
assert_eq!(from_str::<bool>("false"), Some(false));
assert_eq!(from_str::<bool>("not even a boolean"), None);
}
}

View File

@ -20,7 +20,6 @@ collector is task-local so `Gc<T>` is not sendable.
use kinds::marker;
use clone::Clone;
use managed;
/// Immutable garbage-collected pointer type
#[lang="gc"]
@ -55,7 +54,7 @@ impl<T: 'static> Gc<T> {
/// Determine if two garbage-collected boxes point to the same object
#[inline]
pub fn ptr_eq(&self, other: &Gc<T>) -> bool {
managed::ptr_eq(self.ptr, other.ptr)
self.borrow() as *T == other.borrow() as *T
}
}

View File

@ -64,13 +64,15 @@
#![allow(unused_must_use)]
use container::Container;
use intrinsics::TypeId;
use io::Writer;
use iter::Iterator;
use option::{Option, Some, None};
use owned::Box;
use rc::Rc;
use str::{Str, StrSlice};
use result::{Result, Ok, Err};
use slice::{Vector, ImmutableVector};
use str::{Str, StrSlice};
use vec::Vec;
/// Reexport the `sip::hash` function as our default hasher.
@ -284,6 +286,23 @@ impl<S: Writer, T> Hash<S> for *mut T {
}
}
impl<S: Writer> Hash<S> for TypeId {
#[inline]
fn hash(&self, state: &mut S) {
self.hash().hash(state)
}
}
impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
#[inline]
fn hash(&self, state: &mut S) {
match *self {
Ok(ref t) => { 1u.hash(state); t.hash(state); }
Err(ref t) => { 2u.hash(state); t.hash(state); }
}
}
}
//////////////////////////////////////////////////////////////////////////////
#[cfg(test)]

View File

@ -362,7 +362,7 @@ mod tests {
use prelude::*;
use num::ToStrRadix;
use option::{Some, None};
use str::{Str,StrSlice};
use str::Str;
use strbuf::StrBuf;
use slice::{Vector, ImmutableVector};
use self::test::Bencher;

View File

@ -16,7 +16,7 @@ use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
use iter::ExactSize;
use ops::Drop;
use option::{Some, None, Option};
use result::{Ok, Err};
use result::{Ok, Err, ResultUnwrap};
use slice::{ImmutableVector, MutableVector};
use slice;
use vec::Vec;

View File

@ -1335,7 +1335,7 @@ mod test {
use rand::{StdRng, Rng};
let mut bytes = [0, ..1024];
StdRng::new().unwrap().fill_bytes(bytes);
StdRng::new().ok().unwrap().fill_bytes(bytes);
let tmpdir = tmpdir();

View File

@ -230,7 +230,7 @@ use option::{Option, Some, None};
use owned::Box;
use path::Path;
use result::{Ok, Err, Result};
use str::StrSlice;
use str::{StrSlice, StrAllocating};
use str;
use uint;
use unstable::finally::try_finally;

View File

@ -358,6 +358,8 @@ mod test {
})
pub fn socket_name(addr: SocketAddr) {
use result::ResultUnwrap;
let server = UdpSocket::bind(addr);
assert!(server.is_ok());

View File

@ -428,7 +428,6 @@ impl Drop for Process {
mod tests {
use io::process::{ProcessConfig, Process};
use prelude::*;
use str::StrSlice;
// FIXME(#10380) these tests should not all be ignored on android.

View File

@ -36,7 +36,7 @@ use mem::replace;
use option::{Option, Some, None};
use owned::Box;
use prelude::drop;
use result::{Ok, Err};
use result::{Ok, Err, ResultUnwrap};
use rt;
use rt::local::Local;
use rt::rtio::{DontClose, IoFactory, LocalIo, RtioFileStream, RtioTTY};

View File

@ -18,7 +18,7 @@ use ops::Drop;
use option::{Option, None, Some};
use os;
use path::{Path, GenericPath};
use result::{Ok, Err};
use result::{Ok, Err, ResultUnwrap};
use sync::atomics;
/// A wrapper for a path to temporary directory implementing automatic

View File

@ -21,6 +21,7 @@ use comm::Receiver;
use io::IoResult;
use kinds::Send;
use owned::Box;
use option::Expect;
use rt::rtio::{IoFactory, LocalIo, RtioTimer};
/// A synchronous timer object

View File

@ -122,8 +122,8 @@
// Make and rand accessible for benchmarking/testcases
#[cfg(test)] extern crate rand;
// we wrap some libc stuff
extern crate libc;
extern crate core;
// Make std testable by not duplicating lang items. See #2912
#[cfg(test)] extern crate realstd = "std";
@ -133,6 +133,27 @@ extern crate libc;
#[cfg(test)] pub use ty = realstd::ty;
#[cfg(test)] pub use owned = realstd::owned;
#[cfg(not(test))] pub use cmp = core::cmp;
#[cfg(not(test))] pub use kinds = core::kinds;
#[cfg(not(test))] pub use ops = core::ops;
#[cfg(not(test))] pub use owned = core::owned;
#[cfg(not(test))] pub use ty = core::ty;
pub use core::any;
pub use core::bool;
pub use core::cast;
pub use core::cell;
pub use core::char;
pub use core::clone;
pub use core::container;
pub use core::default;
pub use core::intrinsics;
pub use core::iter;
pub use core::mem;
pub use core::ptr;
pub use core::raw;
pub use core::tuple;
// Run tests with libgreen instead of libnative.
//
// FIXME: This egregiously hacks around starting the test runner in a different
@ -176,11 +197,6 @@ pub mod prelude;
#[path = "num/f32.rs"] pub mod f32;
#[path = "num/f64.rs"] pub mod f64;
pub mod unit;
pub mod bool;
pub mod char;
pub mod tuple;
pub mod slice;
pub mod vec;
pub mod str;
@ -188,40 +204,20 @@ pub mod strbuf;
pub mod ascii;
pub mod ptr;
mod managed;
mod reference;
pub mod rc;
pub mod gc;
/* Core language traits */
#[cfg(not(test))] pub mod kinds;
#[cfg(not(test))] pub mod ops;
#[cfg(not(test))] pub mod cmp;
#[cfg(not(test))] pub mod ty;
#[cfg(not(test))] pub mod owned;
/* Common traits */
pub mod from_str;
pub mod num;
pub mod iter;
pub mod to_str;
pub mod clone;
pub mod hash;
pub mod container;
pub mod default;
pub mod any;
/* Common data structures */
pub mod option;
pub mod result;
pub mod cell;
pub mod option;
/* Tasks and communication */
@ -238,11 +234,8 @@ pub mod c_vec;
pub mod os;
pub mod io;
pub mod path;
pub mod cast;
pub mod fmt;
pub mod cleanup;
pub mod mem;
/* Unsupported interfaces */
@ -254,10 +247,6 @@ pub mod reflect;
// Private APIs
#[unstable]
pub mod unstable;
#[experimental]
pub mod intrinsics;
#[experimental]
pub mod raw;
/* For internal use, not exported */

View File

@ -357,7 +357,6 @@ mod tests {
use super::*;
use owned::Box;
use task;
use str::StrSlice;
#[test]
fn test_tls_multitask() {

View File

@ -1,58 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Operations on managed box types
#[cfg(not(test))] use cmp::*;
/// Determine if two shared boxes point to the same object
#[inline]
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
&*a as *T == &*b as *T
}
#[cfg(not(test))]
impl<T:Eq> Eq for @T {
#[inline]
fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for @T {
#[inline]
fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
#[inline]
fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
#[inline]
fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
#[inline]
fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for @T {
#[inline]
fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
}
#[cfg(not(test))]
impl<T: TotalEq> TotalEq for @T {}
#[test]
fn test() {
let x = @3;
let y = @3;
assert!((ptr_eq::<int>(x, x)));
assert!((ptr_eq::<int>(y, y)));
assert!((!ptr_eq::<int>(x, y)));
assert!((!ptr_eq::<int>(y, x)));
}

View File

@ -16,14 +16,18 @@
use prelude::*;
use cast;
use default::Default;
use from_str::FromStr;
use libc::{c_int};
use libc::c_int;
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, Bounded, strconv};
use num::strconv;
use num;
use intrinsics;
pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
pub use core::f32::consts;
#[allow(dead_code)]
mod cmath {
use libc::{c_float, c_int};
@ -65,214 +69,6 @@ mod cmath {
}
}
pub static RADIX: uint = 2u;
pub static MANTISSA_DIGITS: uint = 24u;
pub static DIGITS: uint = 6u;
pub static EPSILON: f32 = 1.19209290e-07_f32;
/// Smallest finite f32 value
pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
/// Smallest positive, normalized f32 value
pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
/// Largest finite f32 value
pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
pub static MIN_EXP: int = -125;
pub static MAX_EXP: int = 128;
pub static MIN_10_EXP: int = -37;
pub static MAX_10_EXP: int = 38;
pub static NAN: f32 = 0.0_f32/0.0_f32;
pub static INFINITY: f32 = 1.0_f32/0.0_f32;
pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
/// Various useful constants.
pub mod consts {
// FIXME: replace with mathematical constants from cmath.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.
/// Archimedes' constant
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
/// pi * 2.0
pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32;
/// pi/2.0
pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
/// pi/3.0
pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
/// pi/4.0
pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
/// pi/6.0
pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
/// pi/8.0
pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
/// 1.0/pi
pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
/// 2.0/pi
pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
/// 2.0/sqrt(pi)
pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
/// sqrt(2.0)
pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
/// 1.0/sqrt(2.0)
pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
/// Euler's number
pub static E: f32 = 2.71828182845904523536028747135266250_f32;
/// log2(e)
pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
/// log10(e)
pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
/// ln(2.0)
pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32;
/// ln(10.0)
pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
}
impl Num for f32 {}
#[cfg(not(test))]
impl Eq for f32 {
#[inline]
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
}
#[cfg(not(test))]
impl Ord for f32 {
#[inline]
fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
}
impl Default for f32 {
#[inline]
fn default() -> f32 { 0.0 }
}
impl Zero for f32 {
#[inline]
fn zero() -> f32 { 0.0 }
/// Returns true if the number is equal to either `0.0` or `-0.0`
#[inline]
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
}
impl One for f32 {
#[inline]
fn one() -> f32 { 1.0 }
}
#[cfg(not(test))]
impl Add<f32,f32> for f32 {
#[inline]
fn add(&self, other: &f32) -> f32 { *self + *other }
}
#[cfg(not(test))]
impl Sub<f32,f32> for f32 {
#[inline]
fn sub(&self, other: &f32) -> f32 { *self - *other }
}
#[cfg(not(test))]
impl Mul<f32,f32> for f32 {
#[inline]
fn mul(&self, other: &f32) -> f32 { *self * *other }
}
#[cfg(not(test))]
impl Div<f32,f32> for f32 {
#[inline]
fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(not(test))]
impl Rem<f32,f32> for f32 {
#[inline]
fn rem(&self, other: &f32) -> f32 {
unsafe { cmath::fmodf(*self, *other) }
}
}
#[cfg(not(test))]
impl Neg<f32> for f32 {
#[inline]
fn neg(&self) -> f32 { -*self }
}
impl Signed for f32 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f32 {
unsafe { intrinsics::fabsf32(*self) }
}
/// The positive difference of two numbers. Returns `0.0` if the number is
/// less than or equal to `other`, otherwise the difference between`self`
/// and `other` is returned.
#[inline]
fn abs_sub(&self, other: &f32) -> f32 {
unsafe { cmath::fdimf(*self, *other) }
}
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f32 {
if self.is_nan() { NAN } else {
unsafe { intrinsics::copysignf32(1.0, *self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Bounded for f32 {
// NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
#[inline]
fn min_value() -> f32 { -MAX_VALUE }
#[inline]
fn max_value() -> f32 { MAX_VALUE }
}
impl Primitive for f32 {}
impl Float for f32 {
#[inline]
fn nan() -> f32 { NAN }

View File

@ -15,14 +15,18 @@
use prelude::*;
use cast;
use default::Default;
use from_str::FromStr;
use libc::{c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, Bounded, strconv};
use num::{strconv};
use num;
use intrinsics;
pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
pub use core::f64::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
pub use core::f64::consts;
#[allow(dead_code)]
mod cmath {
use libc::{c_double, c_int};
@ -73,214 +77,6 @@ mod cmath {
}
}
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective
// members of `Bounded` and `Float`.
pub static RADIX: uint = 2u;
pub static MANTISSA_DIGITS: uint = 53u;
pub static DIGITS: uint = 15u;
pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
/// Smallest finite f64 value
pub static MIN_VALUE: f64 = -1.7976931348623157e+308_f64;
/// Smallest positive, normalized f64 value
pub static MIN_POS_VALUE: f64 = 2.2250738585072014e-308_f64;
/// Largest finite f64 value
pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;
pub static MIN_EXP: int = -1021;
pub static MAX_EXP: int = 1024;
pub static MIN_10_EXP: int = -307;
pub static MAX_10_EXP: int = 308;
pub static NAN: f64 = 0.0_f64/0.0_f64;
pub static INFINITY: f64 = 1.0_f64/0.0_f64;
pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
/// Various useful constants.
pub mod consts {
// FIXME: replace with mathematical constants from cmath.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.
/// Archimedes' constant
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
/// pi * 2.0
pub static PI_2: f64 = 6.28318530717958647692528676655900576_f64;
/// pi/2.0
pub static FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
/// pi/3.0
pub static FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
/// pi/4.0
pub static FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
/// pi/6.0
pub static FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
/// pi/8.0
pub static FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
/// 1.0/pi
pub static FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
/// 2.0/pi
pub static FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
/// 2.0/sqrt(pi)
pub static FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64;
/// sqrt(2.0)
pub static SQRT2: f64 = 1.41421356237309504880168872420969808_f64;
/// 1.0/sqrt(2.0)
pub static FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64;
/// Euler's number
pub static E: f64 = 2.71828182845904523536028747135266250_f64;
/// log2(e)
pub static LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
/// log10(e)
pub static LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
/// ln(2.0)
pub static LN_2: f64 = 0.693147180559945309417232121458176568_f64;
/// ln(10.0)
pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
}
impl Num for f64 {}
#[cfg(not(test))]
impl Eq for f64 {
#[inline]
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
}
#[cfg(not(test))]
impl Ord for f64 {
#[inline]
fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
}
impl Default for f64 {
#[inline]
fn default() -> f64 { 0.0 }
}
impl Zero for f64 {
#[inline]
fn zero() -> f64 { 0.0 }
/// Returns true if the number is equal to either `0.0` or `-0.0`
#[inline]
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
}
impl One for f64 {
#[inline]
fn one() -> f64 { 1.0 }
}
#[cfg(not(test))]
impl Add<f64,f64> for f64 {
#[inline]
fn add(&self, other: &f64) -> f64 { *self + *other }
}
#[cfg(not(test))]
impl Sub<f64,f64> for f64 {
#[inline]
fn sub(&self, other: &f64) -> f64 { *self - *other }
}
#[cfg(not(test))]
impl Mul<f64,f64> for f64 {
#[inline]
fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(not(test))]
impl Div<f64,f64> for f64 {
#[inline]
fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(not(test))]
impl Rem<f64,f64> for f64 {
#[inline]
fn rem(&self, other: &f64) -> f64 {
unsafe { cmath::fmod(*self, *other) }
}
}
#[cfg(not(test))]
impl Neg<f64> for f64 {
#[inline]
fn neg(&self) -> f64 { -*self }
}
impl Signed for f64 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f64 {
unsafe { intrinsics::fabsf64(*self) }
}
/// The positive difference of two numbers. Returns `0.0` if the number is less than or
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
#[inline]
fn abs_sub(&self, other: &f64) -> f64 {
unsafe { cmath::fdim(*self, *other) }
}
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f64 {
if self.is_nan() { NAN } else {
unsafe { intrinsics::copysignf64(1.0, *self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Bounded for f64 {
// NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
#[inline]
fn min_value() -> f64 { -MAX_VALUE }
#[inline]
fn max_value() -> f64 { MAX_VALUE }
}
impl Primitive for f64 {}
impl Float for f64 {
#[inline]
fn nan() -> f64 { NAN }

View File

@ -10,63 +10,14 @@
//! Operations and constants for signed 16-bits integers (`i16` type)
#![allow(non_uppercase_statics)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
int_module!(i16, 16)
pub use core::i16::{BITS, BYTES, MIN, MAX};
impl Bitwise for i16 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self as u16) as i16 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self as u16) as i16 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self as u16) as i16 } }
}
impl CheckedAdd for i16 {
#[inline]
fn checked_add(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i16 {
#[inline]
fn checked_sub(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i16 {
#[inline]
fn checked_mul(&self, v: &i16) -> Option<i16> {
unsafe {
let (x, y) = intrinsics::i16_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
int_module!(i16)

View File

@ -10,63 +10,14 @@
//! Operations and constants for signed 32-bits integers (`i32` type)
#![allow(non_uppercase_statics)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
int_module!(i32, 32)
pub use core::i32::{BITS, BYTES, MIN, MAX};
impl Bitwise for i32 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self as u32) as i32 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self as u32) as i32 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self as u32) as i32 } }
}
impl CheckedAdd for i32 {
#[inline]
fn checked_add(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i32 {
#[inline]
fn checked_sub(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i32 {
#[inline]
fn checked_mul(&self, v: &i32) -> Option<i32> {
unsafe {
let (x, y) = intrinsics::i32_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
int_module!(i32)

View File

@ -10,64 +10,14 @@
//! Operations and constants for signed 64-bits integers (`i64` type)
#![allow(non_uppercase_statics)]
use prelude::*;
use default::Default;
use from_str::FromStr;
#[cfg(target_word_size = "64")]
use num::CheckedMul;
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
int_module!(i64, 64)
pub use core::i64::{BITS, BYTES, MIN, MAX};
impl Bitwise for i64 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self as u64) as i64 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self as u64) as i64 } }
/// Counts the number of trailing zeros.
#[inline]
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self as u64) as i64 } }
}
impl CheckedAdd for i64 {
#[inline]
fn checked_add(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i64 {
#[inline]
fn checked_sub(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i64 {
#[inline]
fn checked_mul(&self, v: &i64) -> Option<i64> {
unsafe {
let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
int_module!(i64)

View File

@ -10,63 +10,14 @@
//! Operations and constants for signed 8-bits integers (`i8` type)
#![allow(non_uppercase_statics)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
int_module!(i8, 8)
pub use core::i8::{BITS, BYTES, MIN, MAX};
impl Bitwise for i8 {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self as u8) as i8 } }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self as u8) as i8 } }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self as u8) as i8 } }
}
impl CheckedAdd for i8 {
#[inline]
fn checked_add(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for i8 {
#[inline]
fn checked_sub(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for i8 {
#[inline]
fn checked_mul(&self, v: &i8) -> Option<i8> {
unsafe {
let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
int_module!(i8)

View File

@ -10,118 +10,14 @@
//! Operations and constants for architecture-sized signed integers (`int` type)
#![allow(non_uppercase_statics)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
#[cfg(target_word_size = "32")] int_module!(int, 32)
#[cfg(target_word_size = "64")] int_module!(int, 64)
pub use core::int::{BITS, BYTES, MIN, MAX};
#[cfg(target_word_size = "32")]
impl Bitwise for int {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> int { (*self as i32).count_ones() as int }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
}
#[cfg(target_word_size = "64")]
impl Bitwise for int {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> int { (*self as i64).count_ones() as int }
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
}
#[cfg(target_word_size = "32")]
impl CheckedAdd for int {
#[inline]
fn checked_add(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedAdd for int {
#[inline]
fn checked_add(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedSub for int {
#[inline]
fn checked_sub(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedSub for int {
#[inline]
fn checked_sub(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedMul for int {
#[inline]
fn checked_mul(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
if y { None } else { Some(x as int) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedMul for int {
#[inline]
fn checked_mul(&self, v: &int) -> Option<int> {
unsafe {
let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
if y { None } else { Some(x as int) }
}
}
}
int_module!(int)

View File

@ -11,226 +11,7 @@
#![macro_escape]
#![doc(hidden)]
macro_rules! int_module (($T:ty, $bits:expr) => (
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
pub static BITS : uint = $bits;
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
pub static BYTES : uint = ($bits / 8);
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::min_value` function.
pub static MIN: $T = (-1 as $T) << (BITS - 1);
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::max_value` function.
pub static MAX: $T = !MIN;
impl CheckedDiv for $T {
#[inline]
fn checked_div(&self, v: &$T) -> Option<$T> {
if *v == 0 || (*self == MIN && *v == -1) {
None
} else {
Some(self / *v)
}
}
}
impl Num for $T {}
#[cfg(not(test))]
impl Ord for $T {
#[inline]
fn lt(&self, other: &$T) -> bool { return (*self) < (*other); }
}
#[cfg(not(test))]
impl Eq for $T {
#[inline]
fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
}
impl Default for $T {
#[inline]
fn default() -> $T { 0 }
}
impl Zero for $T {
#[inline]
fn zero() -> $T { 0 }
#[inline]
fn is_zero(&self) -> bool { *self == 0 }
}
impl One for $T {
#[inline]
fn one() -> $T { 1 }
}
#[cfg(not(test))]
impl Add<$T,$T> for $T {
#[inline]
fn add(&self, other: &$T) -> $T { *self + *other }
}
#[cfg(not(test))]
impl Sub<$T,$T> for $T {
#[inline]
fn sub(&self, other: &$T) -> $T { *self - *other }
}
#[cfg(not(test))]
impl Mul<$T,$T> for $T {
#[inline]
fn mul(&self, other: &$T) -> $T { *self * *other }
}
#[cfg(not(test))]
impl Div<$T,$T> for $T {
/// Integer division, truncated towards 0.
///
/// # Examples
///
/// ~~~
/// assert!( 8 / 3 == 2);
/// assert!( 8 / -3 == -2);
/// assert!(-8 / 3 == -2);
/// assert!(-8 / -3 == 2);
///
/// assert!( 1 / 2 == 0);
/// assert!( 1 / -2 == 0);
/// assert!(-1 / 2 == 0);
/// assert!(-1 / -2 == 0);
/// ~~~
#[inline]
fn div(&self, other: &$T) -> $T { *self / *other }
}
#[cfg(not(test))]
impl Rem<$T,$T> for $T {
/// Returns the integer remainder after division, satisfying:
///
/// ~~~
/// # let n = 1;
/// # let d = 2;
/// assert!((n / d) * d + (n % d) == n)
/// ~~~
///
/// # Examples
///
/// ~~~
/// assert!( 8 % 3 == 2);
/// assert!( 8 % -3 == 2);
/// assert!(-8 % 3 == -2);
/// assert!(-8 % -3 == -2);
///
/// assert!( 1 % 2 == 1);
/// assert!( 1 % -2 == 1);
/// assert!(-1 % 2 == -1);
/// assert!(-1 % -2 == -1);
/// ~~~
#[inline]
fn rem(&self, other: &$T) -> $T { *self % *other }
}
#[cfg(not(test))]
impl Neg<$T> for $T {
#[inline]
fn neg(&self) -> $T { -*self }
}
impl Signed for $T {
/// Computes the absolute value
#[inline]
fn abs(&self) -> $T {
if self.is_negative() { -*self } else { *self }
}
///
/// The positive difference of two numbers. Returns `0` if the number is less than or
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
///
#[inline]
fn abs_sub(&self, other: &$T) -> $T {
if *self <= *other { 0 } else { *self - *other }
}
///
/// # Returns
///
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
///
#[inline]
fn signum(&self) -> $T {
match *self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
/// Returns true if the number is positive
#[inline]
fn is_positive(&self) -> bool { *self > 0 }
/// Returns true if the number is negative
#[inline]
fn is_negative(&self) -> bool { *self < 0 }
}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
fn bitor(&self, other: &$T) -> $T { *self | *other }
}
#[cfg(not(test))]
impl BitAnd<$T,$T> for $T {
#[inline]
fn bitand(&self, other: &$T) -> $T { *self & *other }
}
#[cfg(not(test))]
impl BitXor<$T,$T> for $T {
#[inline]
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
}
#[cfg(not(test))]
impl Shl<$T,$T> for $T {
#[inline]
fn shl(&self, other: &$T) -> $T { *self << *other }
}
#[cfg(not(test))]
impl Shr<$T,$T> for $T {
#[inline]
fn shr(&self, other: &$T) -> $T { *self >> *other }
}
#[cfg(not(test))]
impl Not<$T> for $T {
#[inline]
fn not(&self) -> $T { !*self }
}
impl Bounded for $T {
#[inline]
fn min_value() -> $T { MIN }
#[inline]
fn max_value() -> $T { MAX }
}
impl Int for $T {}
impl Primitive for $T {}
macro_rules! int_module (($T:ty) => (
// String conversion functions and impl str -> num
@ -296,7 +77,7 @@ impl ToStrRadix for $T {
/// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
let mut buf = Vec::new();
let mut buf = ::vec::Vec::new();
strconv::int_to_str_bytes_common(*self, radix, strconv::SignNeg, |i| {
buf.push(i);
});
@ -311,89 +92,10 @@ mod tests {
use prelude::*;
use super::*;
use int;
use i32;
use num;
use num::Bitwise;
use num::CheckedDiv;
use num::ToStrRadix;
use str::StrSlice;
#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert_eq!(MIN + MAX + 1, 0);
}
#[test]
fn test_num() {
num::test_num(10 as $T, 2 as $T);
}
#[test]
pub fn test_abs() {
assert_eq!((1 as $T).abs(), 1 as $T);
assert_eq!((0 as $T).abs(), 0 as $T);
assert_eq!((-1 as $T).abs(), 1 as $T);
}
#[test]
fn test_abs_sub() {
assert_eq!((-1 as $T).abs_sub(&(1 as $T)), 0 as $T);
assert_eq!((1 as $T).abs_sub(&(1 as $T)), 0 as $T);
assert_eq!((1 as $T).abs_sub(&(0 as $T)), 1 as $T);
assert_eq!((1 as $T).abs_sub(&(-1 as $T)), 2 as $T);
}
#[test]
fn test_signum() {
assert_eq!((1 as $T).signum(), 1 as $T);
assert_eq!((0 as $T).signum(), 0 as $T);
assert_eq!((-0 as $T).signum(), 0 as $T);
assert_eq!((-1 as $T).signum(), -1 as $T);
}
#[test]
fn test_is_positive() {
assert!((1 as $T).is_positive());
assert!(!(0 as $T).is_positive());
assert!(!(-0 as $T).is_positive());
assert!(!(-1 as $T).is_positive());
}
#[test]
fn test_is_negative() {
assert!(!(1 as $T).is_negative());
assert!(!(0 as $T).is_negative());
assert!(!(-0 as $T).is_negative());
assert!((-1 as $T).is_negative());
}
#[test]
fn test_bitwise() {
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
}
#[test]
fn test_count_ones() {
assert_eq!((0b0101100 as $T).count_ones(), 3);
assert_eq!((0b0100001 as $T).count_ones(), 2);
assert_eq!((0b1111001 as $T).count_ones(), 5);
}
#[test]
fn test_count_zeros() {
assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3);
assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2);
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
}
#[test]
fn test_from_str() {
assert_eq!(from_str::<$T>("0"), Some(0 as $T));
@ -508,13 +210,6 @@ mod tests {
assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
assert!(from_str::<i64>("-9223372036854775809").is_none());
}
#[test]
fn test_signed_checked_div() {
assert_eq!(10i.checked_div(&2), Some(5));
assert_eq!(5i.checked_div(&0), None);
assert_eq!(int::MIN.checked_div(&-1), None);
}
}
))

View File

@ -15,306 +15,24 @@
#![allow(missing_doc)]
use clone::Clone;
use cmp::{Eq, Ord};
use kinds::Copy;
use mem::size_of;
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use option::{Option, Some, None};
use fmt::{Show, Binary, Octal, LowerHex, UpperHex};
use option::{Option};
#[cfg(test)] use fmt::Show;
pub use core::num::{Num, div_rem, Zero, zero, One, one};
pub use core::num::{Signed, abs, abs_sub, signum};
pub use core::num::{Unsigned, pow, Bounded, Bitwise};
pub use core::num::{Primitive, Int, Saturating};
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};
pub use core::num::{checked_next_power_of_two};
pub use core::num::{from_int, from_i8, from_i16, from_i32, from_i64};
pub use core::num::{from_uint, from_u8, from_u16, from_u32, from_u64};
pub use core::num::{from_f32, from_f64};
pub mod strconv;
/// The base trait for numeric types
pub trait Num: Eq + Zero + One
+ Neg<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}
/// Simultaneous division and remainder
#[inline]
pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
(x / y, x % y)
}
/// Defines an additive identity element for `Self`.
///
/// # Deriving
///
/// This trait can be automatically be derived using `#[deriving(Zero)]`
/// attribute. If you choose to use this, make sure that the laws outlined in
/// the documentation for `Zero::zero` still hold.
pub trait Zero: Add<Self, Self> {
/// Returns the additive identity element of `Self`, `0`.
///
/// # Laws
///
/// ~~~notrust
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ~~~
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
// FIXME (#5527): This should be an associated constant
fn zero() -> Self;
/// Returns `true` if `self` is equal to the additive identity.
fn is_zero(&self) -> bool;
}
/// Returns the additive identity, `0`.
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
/// Defines a multiplicative identity element for `Self`.
pub trait One: Mul<Self, Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
///
/// # Laws
///
/// ~~~notrust
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ~~~
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
// FIXME (#5527): This should be an associated constant
fn one() -> Self;
}
/// Returns the multiplicative identity, `1`.
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
/// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> {
/// Computes the absolute value.
///
/// For float, f32, and f64, `NaN` will be returned if the number is `NaN`.
fn abs(&self) -> Self;
/// The positive difference of two numbers.
///
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
/// between `self` and `other` is returned.
fn abs_sub(&self, other: &Self) -> Self;
/// Returns the sign of the number.
///
/// For `float`, `f32`, `f64`:
/// * `1.0` if the number is positive, `+0.0` or `INFINITY`
/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// * `NaN` if the number is `NaN`
///
/// For `int`:
/// * `0` if the number is zero
/// * `1` if the number is positive
/// * `-1` if the number is negative
fn signum(&self) -> Self;
/// Returns true if the number is positive and false if the number is zero or negative.
fn is_positive(&self) -> bool;
/// Returns true if the number is negative and false if the number is zero or positive.
fn is_negative(&self) -> bool;
}
/// Computes the absolute value.
///
/// For float, f32, and f64, `NaN` will be returned if the number is `NaN`
#[inline(always)]
pub fn abs<T: Signed>(value: T) -> T {
value.abs()
}
/// The positive difference of two numbers.
///
/// Returns `zero` if the number is less than or equal to `other`,
/// otherwise the difference between `self` and `other` is returned.
#[inline(always)]
pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
x.abs_sub(&y)
}
/// Returns the sign of the number.
///
/// For float, f32, f64:
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is `NAN`
///
/// For int:
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
/// A trait for values which cannot be negative
pub trait Unsigned: Num {}
/// Raises a value to the power of exp, using exponentiation by squaring.
///
/// # Example
///
/// ```rust
/// use std::num;
///
/// assert_eq!(num::pow(2, 4), 16);
/// ```
#[inline]
pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
if exp == 1 { base }
else {
let mut acc = one::<T>();
while exp > 0 {
if (exp & 1) == 1 {
acc = acc * base;
}
base = base * base;
exp = exp >> 1;
}
acc
}
}
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent
fn min_value() -> Self;
/// returns the largest finite number this type can represent
fn max_value() -> Self;
}
/// Numbers with a fixed binary representation.
pub trait Bitwise: Bounded
+ Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<Self,Self>
+ Shr<Self,Self> {
/// Returns the number of ones in the binary representation of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b01001100u8;
/// assert_eq!(n.count_ones(), 3);
/// ```
fn count_ones(&self) -> Self;
/// Returns the number of zeros in the binary representation of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b01001100u8;
/// assert_eq!(n.count_zeros(), 5);
/// ```
#[inline]
fn count_zeros(&self) -> Self {
(!*self).count_ones()
}
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.leading_zeros(), 10);
/// ```
fn leading_zeros(&self) -> Self;
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.trailing_zeros(), 3);
/// ```
fn trailing_zeros(&self) -> Self;
}
/// Specifies the available operations common to all of Rust's core numeric primitives.
/// These may not always make sense from a purely mathematical point of view, but
/// may be useful for systems programming.
pub trait Primitive: Copy
+ Clone
+ Num
+ NumCast
+ Ord
+ Bounded {}
/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Primitive
+ Bitwise
+ CheckedAdd
+ CheckedSub
+ CheckedMul
+ CheckedDiv
+ Show
+ Binary
+ Octal
+ LowerHex
+ UpperHex {}
/// Returns the smallest power of 2 greater than or equal to `n`.
#[inline]
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
let mut tmp: T = n - one();
let mut shift: T = one();
while shift <= halfbits {
tmp = tmp | (tmp >> shift);
shift = shift << one();
}
tmp + one()
}
// Returns `true` iff `n == 2^k` for some k.
#[inline]
pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
(n - one()) & n == zero()
}
/// Returns the smallest power of 2 greater than or equal to `n`. If the next
/// power of two is greater than the type's maximum value, `None` is returned,
/// otherwise the power of 2 is wrapped in `Some`.
#[inline]
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
let mut tmp: T = n - one();
let mut shift: T = one();
while shift <= halfbits {
tmp = tmp | (tmp >> shift);
shift = shift << one();
}
tmp.checked_add(&one())
}
/// Used for representing the classification of floating point numbers
#[deriving(Eq, Show)]
pub enum FPCategory {
@ -537,494 +255,6 @@ pub trait Float: Signed + Primitive {
fn to_radians(self) -> Self;
}
/// A generic trait for converting a value to a number.
pub trait ToPrimitive {
/// Converts the value of `self` to an `int`.
#[inline]
fn to_int(&self) -> Option<int> {
self.to_i64().and_then(|x| x.to_int())
}
/// Converts the value of `self` to an `i8`.
#[inline]
fn to_i8(&self) -> Option<i8> {
self.to_i64().and_then(|x| x.to_i8())
}
/// Converts the value of `self` to an `i16`.
#[inline]
fn to_i16(&self) -> Option<i16> {
self.to_i64().and_then(|x| x.to_i16())
}
/// Converts the value of `self` to an `i32`.
#[inline]
fn to_i32(&self) -> Option<i32> {
self.to_i64().and_then(|x| x.to_i32())
}
/// Converts the value of `self` to an `i64`.
fn to_i64(&self) -> Option<i64>;
/// Converts the value of `self` to an `uint`.
#[inline]
fn to_uint(&self) -> Option<uint> {
self.to_u64().and_then(|x| x.to_uint())
}
/// Converts the value of `self` to an `u8`.
#[inline]
fn to_u8(&self) -> Option<u8> {
self.to_u64().and_then(|x| x.to_u8())
}
/// Converts the value of `self` to an `u16`.
#[inline]
fn to_u16(&self) -> Option<u16> {
self.to_u64().and_then(|x| x.to_u16())
}
/// Converts the value of `self` to an `u32`.
#[inline]
fn to_u32(&self) -> Option<u32> {
self.to_u64().and_then(|x| x.to_u32())
}
/// Converts the value of `self` to an `u64`.
#[inline]
fn to_u64(&self) -> Option<u64>;
/// Converts the value of `self` to an `f32`.
#[inline]
fn to_f32(&self) -> Option<f32> {
self.to_f64().and_then(|x| x.to_f32())
}
/// Converts the value of `self` to an `f64`.
#[inline]
fn to_f64(&self) -> Option<f64> {
self.to_i64().and_then(|x| x.to_f64())
}
}
macro_rules! impl_to_primitive_int_to_int(
($SrcT:ty, $DstT:ty) => (
{
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as i64;
let min_value: $DstT = Bounded::min_value();
let max_value: $DstT = Bounded::max_value();
if min_value as i64 <= n && n <= max_value as i64 {
Some(*self as $DstT)
} else {
None
}
}
}
)
)
macro_rules! impl_to_primitive_int_to_uint(
($SrcT:ty, $DstT:ty) => (
{
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
if zero <= *self && *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_int(
($T:ty) => (
impl ToPrimitive for $T {
#[inline]
fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int) }
#[inline]
fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8) }
#[inline]
fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16) }
#[inline]
fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32) }
#[inline]
fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64) }
#[inline]
fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint) }
#[inline]
fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8) }
#[inline]
fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16) }
#[inline]
fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32) }
#[inline]
fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64) }
#[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_int!(int)
impl_to_primitive_int!(i8)
impl_to_primitive_int!(i16)
impl_to_primitive_int!(i32)
impl_to_primitive_int!(i64)
macro_rules! impl_to_primitive_uint_to_int(
($DstT:ty) => (
{
let max_value: $DstT = Bounded::max_value();
if *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_uint_to_uint(
($SrcT:ty, $DstT:ty) => (
{
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
if zero <= *self && *self as u64 <= max_value as u64 {
Some(*self as $DstT)
} else {
None
}
}
}
)
)
macro_rules! impl_to_primitive_uint(
($T:ty) => (
impl ToPrimitive for $T {
#[inline]
fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int) }
#[inline]
fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8) }
#[inline]
fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16) }
#[inline]
fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32) }
#[inline]
fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64) }
#[inline]
fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint) }
#[inline]
fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8) }
#[inline]
fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16) }
#[inline]
fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32) }
#[inline]
fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64) }
#[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_uint!(uint)
impl_to_primitive_uint!(u8)
impl_to_primitive_uint!(u16)
impl_to_primitive_uint!(u32)
impl_to_primitive_uint!(u64)
macro_rules! impl_to_primitive_float_to_float(
($SrcT:ty, $DstT:ty) => (
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as f64;
let max_value: $SrcT = Bounded::max_value();
if -max_value as f64 <= n && n <= max_value as f64 {
Some(*self as $DstT)
} else {
None
}
}
)
)
macro_rules! impl_to_primitive_float(
($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_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32) }
#[inline]
fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64) }
}
)
)
impl_to_primitive_float!(f32)
impl_to_primitive_float!(f64)
/// 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.
#[inline]
fn from_int(n: int) -> Option<Self> {
FromPrimitive::from_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// 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_i64(n as i64)
}
/// Convert an `i64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
fn from_i64(n: i64) -> Option<Self>;
/// Convert an `uint` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
#[inline]
fn from_uint(n: uint) -> Option<Self> {
FromPrimitive::from_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// 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_u64(n as u64)
}
/// Convert an `u64` to return an optional value of this type. If the
/// type cannot be represented by this value, the `None` is returned.
fn from_u64(n: u64) -> Option<Self>;
/// 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_f64(n as f64)
}
/// 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_i64(n as i64)
}
}
/// 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, $to_ty:expr) => (
impl FromPrimitive for $T {
#[inline] fn from_int(n: int) -> Option<$T> { $to_ty }
#[inline] fn from_i8(n: i8) -> Option<$T> { $to_ty }
#[inline] fn from_i16(n: i16) -> Option<$T> { $to_ty }
#[inline] fn from_i32(n: i32) -> Option<$T> { $to_ty }
#[inline] fn from_i64(n: i64) -> Option<$T> { $to_ty }
#[inline] fn from_uint(n: uint) -> Option<$T> { $to_ty }
#[inline] fn from_u8(n: u8) -> Option<$T> { $to_ty }
#[inline] fn from_u16(n: u16) -> Option<$T> { $to_ty }
#[inline] fn from_u32(n: u32) -> Option<$T> { $to_ty }
#[inline] fn from_u64(n: u64) -> Option<$T> { $to_ty }
#[inline] fn from_f32(n: f32) -> Option<$T> { $to_ty }
#[inline] fn from_f64(n: f64) -> Option<$T> { $to_ty }
}
)
)
impl_from_primitive!(int, n.to_int())
impl_from_primitive!(i8, n.to_i8())
impl_from_primitive!(i16, n.to_i16())
impl_from_primitive!(i32, n.to_i32())
impl_from_primitive!(i64, n.to_i64())
impl_from_primitive!(uint, n.to_uint())
impl_from_primitive!(u8, n.to_u8())
impl_from_primitive!(u16, n.to_u16())
impl_from_primitive!(u32, n.to_u32())
impl_from_primitive!(u64, n.to_u64())
impl_from_primitive!(f32, n.to_f32())
impl_from_primitive!(f64, n.to_f64())
/// Cast from one machine scalar to another.
///
/// # Example
///
/// ```
/// use std::num;
///
/// let twenty: f32 = num::cast(0x14).unwrap();
/// assert_eq!(twenty, 20f32);
/// ```
///
#[inline]
pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
NumCast::from(n)
}
/// An interface for casting between machine scalars.
pub trait NumCast: ToPrimitive {
/// Creates a number from another value that can be converted into a primitive via the
/// `ToPrimitive` trait.
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: ToPrimitive>(n: N) -> Option<$T> {
// `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment
n.$conv()
}
}
)
)
impl_num_cast!(u8, to_u8)
impl_num_cast!(u16, to_u16)
impl_num_cast!(u32, to_u32)
impl_num_cast!(u64, to_u64)
impl_num_cast!(uint, to_uint)
impl_num_cast!(i8, to_i8)
impl_num_cast!(i16, to_i16)
impl_num_cast!(i32, to_i32)
impl_num_cast!(i64, to_i64)
impl_num_cast!(int, to_int)
impl_num_cast!(f32, to_f32)
impl_num_cast!(f64, to_f64)
/// A generic trait for converting a value to a string with a radix (base)
pub trait ToStrRadix {
fn to_str_radix(&self, radix: uint) -> ~str;
@ -1040,70 +270,6 @@ pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
FromStrRadix::from_str_radix(str, radix)
}
/// 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 + Ord + 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.
fn checked_add(&self, v: &Self) -> Option<Self>;
}
/// Performs subtraction that returns `None` instead of wrapping around on underflow.
pub trait CheckedSub: Sub<Self, Self> {
/// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
fn checked_sub(&self, v: &Self) -> Option<Self>;
}
/// Performs multiplication that returns `None` instead of wrapping around on underflow or
/// overflow.
pub trait CheckedMul: Mul<Self, Self> {
/// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
/// happens, `None` is returned.
fn checked_mul(&self, v: &Self) -> Option<Self>;
}
/// Performs division that returns `None` instead of wrapping around on underflow or overflow.
pub trait CheckedDiv: Div<Self, Self> {
/// Divides two numbers, checking for underflow or overflow. If underflow or overflow happens,
/// `None` is returned.
fn checked_div(&self, v: &Self) -> Option<Self>;
}
/// Helper function for testing numeric operations
#[cfg(test)]
pub fn test_num<T:Num + NumCast + Show>(ten: T, two: T) {

View File

@ -820,6 +820,7 @@ mod bench {
use super::test::Bencher;
use rand::{XorShiftRng, Rng};
use num::ToStrRadix;
use realstd::result::ResultUnwrap;
#[bench]
fn to_str_bin(b: &mut Bencher) {
@ -856,6 +857,7 @@ mod bench {
use super::test::Bencher;
use rand::{XorShiftRng, Rng};
use num::ToStrRadix;
use realstd::result::ResultUnwrap;
#[bench]
fn to_str_bin(b: &mut Bencher) {
@ -892,6 +894,7 @@ mod bench {
use super::test::Bencher;
use rand::{XorShiftRng, Rng};
use f64;
use realstd::result::ResultUnwrap;
#[bench]
fn float_to_str(b: &mut Bencher) {

View File

@ -10,49 +10,14 @@
//! Operations and constants for unsigned 16-bits integers (`u16` type)
#![allow(non_uppercase_statics)]
#![allow(unsigned_negate)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
uint_module!(u16, i16, 16)
pub use core::u16::{BITS, BYTES, MIN, MAX};
impl CheckedAdd for u16 {
#[inline]
fn checked_add(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u16 {
#[inline]
fn checked_sub(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u16 {
#[inline]
fn checked_mul(&self, v: &u16) -> Option<u16> {
unsafe {
let (x, y) = intrinsics::u16_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
uint_module!(u16)

View File

@ -10,49 +10,14 @@
//! Operations and constants for unsigned 32-bits integers (`u32` type)
#![allow(non_uppercase_statics)]
#![allow(unsigned_negate)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
uint_module!(u32, i32, 32)
pub use core::u32::{BITS, BYTES, MIN, MAX};
impl CheckedAdd for u32 {
#[inline]
fn checked_add(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u32 {
#[inline]
fn checked_sub(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u32 {
#[inline]
fn checked_mul(&self, v: &u32) -> Option<u32> {
unsafe {
let (x, y) = intrinsics::u32_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
uint_module!(u32)

View File

@ -10,51 +10,14 @@
//! Operations and constants for unsigned 64-bits integer (`u64` type)
#![allow(non_uppercase_statics)]
#![allow(unsigned_negate)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded};
#[cfg(target_word_size = "64")]
use num::CheckedMul;
use num::{CheckedAdd, CheckedSub};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
uint_module!(u64, i64, 64)
pub use core::u64::{BITS, BYTES, MIN, MAX};
impl CheckedAdd for u64 {
#[inline]
fn checked_add(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u64 {
#[inline]
fn checked_sub(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u64 {
#[inline]
fn checked_mul(&self, v: &u64) -> Option<u64> {
unsafe {
let (x, y) = intrinsics::u64_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
uint_module!(u64)

View File

@ -10,49 +10,14 @@
//! Operations and constants for unsigned 8-bits integers (`u8` type)
#![allow(non_uppercase_statics)]
#![allow(unsigned_negate)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
uint_module!(u8, i8, 8)
pub use core::u8::{BITS, BYTES, MIN, MAX};
impl CheckedAdd for u8 {
#[inline]
fn checked_add(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_add_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedSub for u8 {
#[inline]
fn checked_sub(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_sub_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
impl CheckedMul for u8 {
#[inline]
fn checked_mul(&self, v: &u8) -> Option<u8> {
unsafe {
let (x, y) = intrinsics::u8_mul_with_overflow(*self, *v);
if y { None } else { Some(x) }
}
}
}
uint_module!(u8)

View File

@ -10,85 +10,14 @@
//! Operations and constants for architecture-sized unsigned integers (`uint` type)
#![allow(non_uppercase_statics)]
#![allow(unsigned_negate)]
use prelude::*;
use default::Default;
use from_str::FromStr;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use iter::Iterator;
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
use num::strconv;
use option::Option;
use slice::{ImmutableVector, OwnedVector};
use str;
use intrinsics;
uint_module!(uint, int, ::int::BITS)
pub use core::uint::{BITS, BYTES, MIN, MAX};
#[cfg(target_word_size = "32")]
impl CheckedAdd for uint {
#[inline]
fn checked_add(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_add_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedAdd for uint {
#[inline]
fn checked_add(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_add_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedSub for uint {
#[inline]
fn checked_sub(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_sub_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedSub for uint {
#[inline]
fn checked_sub(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_sub_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "32")]
impl CheckedMul for uint {
#[inline]
fn checked_mul(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u32_mul_with_overflow(*self as u32, *v as u32);
if y { None } else { Some(x as uint) }
}
}
}
#[cfg(target_word_size = "64")]
impl CheckedMul for uint {
#[inline]
fn checked_mul(&self, v: &uint) -> Option<uint> {
unsafe {
let (x, y) = intrinsics::u64_mul_with_overflow(*self as u64, *v as u64);
if y { None } else { Some(x as uint) }
}
}
}
uint_module!(uint)

View File

@ -12,140 +12,7 @@
#![doc(hidden)]
#![allow(unsigned_negate)]
macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
pub static BITS : uint = $bits;
pub static BYTES : uint = ($bits / 8);
pub static MIN: $T = 0 as $T;
pub static MAX: $T = 0 as $T - 1 as $T;
impl CheckedDiv for $T {
#[inline]
fn checked_div(&self, v: &$T) -> Option<$T> {
if *v == 0 {
None
} else {
Some(self / *v)
}
}
}
impl Num for $T {}
#[cfg(not(test))]
impl Ord for $T {
#[inline]
fn lt(&self, other: &$T) -> bool { (*self) < (*other) }
}
#[cfg(not(test))]
impl Eq for $T {
#[inline]
fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
}
impl Default for $T {
#[inline]
fn default() -> $T { 0 }
}
impl Zero for $T {
#[inline]
fn zero() -> $T { 0 }
#[inline]
fn is_zero(&self) -> bool { *self == 0 }
}
impl One for $T {
#[inline]
fn one() -> $T { 1 }
}
#[cfg(not(test))]
impl Add<$T,$T> for $T {
#[inline]
fn add(&self, other: &$T) -> $T { *self + *other }
}
#[cfg(not(test))]
impl Sub<$T,$T> for $T {
#[inline]
fn sub(&self, other: &$T) -> $T { *self - *other }
}
#[cfg(not(test))]
impl Mul<$T,$T> for $T {
#[inline]
fn mul(&self, other: &$T) -> $T { *self * *other }
}
#[cfg(not(test))]
impl Div<$T,$T> for $T {
#[inline]
fn div(&self, other: &$T) -> $T { *self / *other }
}
#[cfg(not(test))]
impl Rem<$T,$T> for $T {
#[inline]
fn rem(&self, other: &$T) -> $T { *self % *other }
}
#[cfg(not(test))]
impl Neg<$T> for $T {
#[inline]
fn neg(&self) -> $T { -*self }
}
impl Unsigned for $T {}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
fn bitor(&self, other: &$T) -> $T { *self | *other }
}
#[cfg(not(test))]
impl BitAnd<$T,$T> for $T {
#[inline]
fn bitand(&self, other: &$T) -> $T { *self & *other }
}
#[cfg(not(test))]
impl BitXor<$T,$T> for $T {
#[inline]
fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
}
#[cfg(not(test))]
impl Shl<$T,$T> for $T {
#[inline]
fn shl(&self, other: &$T) -> $T { *self << *other }
}
#[cfg(not(test))]
impl Shr<$T,$T> for $T {
#[inline]
fn shr(&self, other: &$T) -> $T { *self >> *other }
}
#[cfg(not(test))]
impl Not<$T> for $T {
#[inline]
fn not(&self) -> $T { !*self }
}
impl Bounded for $T {
#[inline]
fn min_value() -> $T { MIN }
#[inline]
fn max_value() -> $T { MAX }
}
impl Int for $T {}
macro_rules! uint_module (($T:ty) => (
// String conversion functions and impl str -> num
@ -211,7 +78,7 @@ impl ToStrRadix for $T {
/// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
let mut buf = Vec::new();
let mut buf = ::vec::Vec::new();
strconv::int_to_str_bytes_common(*self, radix, strconv::SignNone, |i| {
buf.push(i);
});
@ -221,78 +88,15 @@ impl ToStrRadix for $T {
}
}
impl Primitive for $T {}
impl Bitwise for $T {
/// Returns the number of ones in the binary representation of the number.
#[inline]
fn count_ones(&self) -> $T {
(*self as $T_SIGNED).count_ones() as $T
}
/// Returns the number of leading zeros in the in the binary representation
/// of the number.
#[inline]
fn leading_zeros(&self) -> $T {
(*self as $T_SIGNED).leading_zeros() as $T
}
/// Returns the number of trailing zeros in the in the binary representation
/// of the number.
#[inline]
fn trailing_zeros(&self) -> $T {
(*self as $T_SIGNED).trailing_zeros() as $T
}
}
#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use num;
use num::CheckedDiv;
use num::Bitwise;
use num::ToStrRadix;
use str::StrSlice;
use u16;
#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert_eq!(MIN + MAX + 1, 0);
}
#[test]
fn test_num() {
num::test_num(10 as $T, 2 as $T);
}
#[test]
fn test_bitwise() {
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
assert_eq!(MAX - (0b1011 as $T), (0b1011 as $T).not());
}
#[test]
fn test_count_ones() {
assert_eq!((0b0101100 as $T).count_ones(), 3);
assert_eq!((0b0100001 as $T).count_ones(), 2);
assert_eq!((0b1111001 as $T).count_ones(), 5);
}
#[test]
fn test_count_zeros() {
assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3);
assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2);
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
}
#[test]
pub fn test_to_str() {
assert_eq!((0 as $T).to_str_radix(10u), "0".to_owned());
@ -404,12 +208,6 @@ mod tests {
pub fn to_str_radix37() {
100u.to_str_radix(37u);
}
#[test]
fn test_unsigned_checked_div() {
assert_eq!(10u.checked_div(&2), Some(5));
assert_eq!(5u.checked_div(&0), None);
}
}
))

View File

@ -1,4 +1,4 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -139,744 +139,29 @@
//! ```
use any::Any;
use clone::Clone;
use cmp::{Eq, TotalEq, TotalOrd};
use default::Default;
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
use kinds::Send;
use mem;
use slice;
/// The `Option`
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show)]
pub enum Option<T> {
/// No value
None,
/// Some value `T`
Some(T)
}
pub use core::option::{Option, Some, None, Item, collect};
/////////////////////////////////////////////////////////////////////////////
// Type implementation
/////////////////////////////////////////////////////////////////////////////
impl<T> Option<T> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////
/// Returns `true` if the option is a `Some` value
#[inline]
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false
}
}
/// Returns `true` if the option is a `None` value
#[inline]
pub fn is_none(&self) -> bool {
!self.is_some()
}
/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
/// Convert from `Option<T>` to `Option<&T>`
///
/// # Example
///
/// Convert an `Option<~str>` into an `Option<int>`, preserving the original.
/// The `map` method takes the `self` argument by value, consuming the original,
/// so this technique uses `as_ref` to first take an `Option` to a reference
/// to the value inside the original.
///
/// ```
/// let num_as_str: Option<~str> = Some("10".to_owned());
/// // First, cast `Option<~str>` to `Option<&~str>` with `as_ref`,
/// // then consume *that* with `map`, leaving `num_as_str` on the stack.
/// let num_as_int: Option<uint> = num_as_str.as_ref().map(|n| n.len());
/// println!("still can print num_as_str: {}", num_as_str);
/// ```
#[inline]
pub fn as_ref<'r>(&'r self) -> Option<&'r T> {
match *self { Some(ref x) => Some(x), None => None }
}
/// Convert from `Option<T>` to `Option<&mut T>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
match *self { Some(ref mut x) => Some(x), None => None }
}
/// Convert from `Option<T>` to `&[T]` (without copying)
#[inline]
pub fn as_slice<'r>(&'r self) -> &'r [T] {
match *self {
Some(ref x) => slice::ref_slice(x),
None => &[]
}
}
/// Convert from `Option<T>` to `&mut [T]` (without copying)
#[inline]
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
match *self {
Some(ref mut x) => slice::mut_ref_slice(x),
None => &mut []
}
}
/////////////////////////////////////////////////////////////////////////
// Getting to contained values
/////////////////////////////////////////////////////////////////////////
/// Extension trait for the `Option` type to add an `expect` method
// FIXME(#14008) should this trait even exist?
pub trait Expect<T> {
/// Unwraps an option, yielding the content of a `Some`
///
/// # Failure
///
/// Fails if the value is a `None` with a custom failure message provided by `msg`.
/// Fails if the value is a `None` with a custom failure message provided by
/// `msg`.
fn expect<M: Any + Send>(self, m: M) -> T;
}
impl<T> Expect<T> for Option<T> {
#[inline]
pub fn expect<M: Any + Send>(self, msg: M) -> T {
fn expect<M: Any + Send>(self, msg: M) -> T {
match self {
Some(val) => val,
None => fail!(msg),
}
}
/// Moves a value out of an option type and returns it, consuming the `Option`.
///
/// # Failure
///
/// Fails if the self value equals `None`.
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged.
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn unwrap(self) -> T {
match self {
Some(val) => val,
None => fail!("called `Option::unwrap()` on a `None` value"),
}
}
/// Returns the contained value or a default.
#[inline]
pub fn unwrap_or(self, def: T) -> T {
match self {
Some(x) => x,
None => def
}
}
/// Returns the contained value or computes it from a closure.
#[inline]
pub fn unwrap_or_else(self, f: || -> T) -> T {
match self {
Some(x) => x,
None => f()
}
}
/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////
/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
///
/// # Example
///
/// Convert an `Option<~str>` into an `Option<uint>`, consuming the original:
///
/// ```
/// let num_as_str: Option<~str> = Some("10".to_owned());
/// // `Option::map` takes self *by value*, consuming `num_as_str`
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
/// ```
#[inline]
pub fn map<U>(self, f: |T| -> U) -> Option<U> {
match self { Some(x) => Some(f(x)), None => None }
}
/// Applies a function to the contained value or returns a default.
#[inline]
pub fn map_or<U>(self, def: U, f: |T| -> U) -> U {
match self { None => def, Some(t) => f(t) }
}
/// Applies a function to the contained value or does nothing.
/// Returns true if the contained value was mutated.
pub fn mutate(&mut self, f: |T| -> T) -> bool {
if self.is_some() {
*self = Some(f(self.take_unwrap()));
true
} else { false }
}
/// Applies a function to the contained value or sets it to a default.
/// Returns true if the contained value was mutated, or false if set to the default.
pub fn mutate_or_set(&mut self, def: T, f: |T| -> T) -> bool {
if self.is_some() {
*self = Some(f(self.take_unwrap()));
true
} else {
*self = Some(def);
false
}
}
/////////////////////////////////////////////////////////////////////////
// Iterator constructors
/////////////////////////////////////////////////////////////////////////
/// Returns an iterator over the possibly contained value.
#[inline]
pub fn iter<'r>(&'r self) -> Item<&'r T> {
Item{opt: self.as_ref()}
}
/// Returns a mutable iterator over the possibly contained value.
#[inline]
pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> {
Item{opt: self.as_mut()}
}
/// Returns a consuming iterator over the possibly contained value.
#[inline]
pub fn move_iter(self) -> Item<T> {
Item{opt: self}
}
/////////////////////////////////////////////////////////////////////////
// Boolean operations on the values, eager and lazy
/////////////////////////////////////////////////////////////////////////
/// Returns `None` if the option is `None`, otherwise returns `optb`.
#[inline]
pub fn and<U>(self, optb: Option<U>) -> Option<U> {
match self {
Some(_) => optb,
None => None,
}
}
/// Returns `None` if the option is `None`, otherwise calls `f` with the
/// wrapped value and returns the result.
#[inline]
pub fn and_then<U>(self, f: |T| -> Option<U>) -> Option<U> {
match self {
Some(x) => f(x),
None => None,
}
}
/// Returns the option if it contains a value, otherwise returns `optb`.
#[inline]
pub fn or(self, optb: Option<T>) -> Option<T> {
match self {
Some(_) => self,
None => optb
}
}
/// Returns the option if it contains a value, otherwise calls `f` and
/// returns the result.
#[inline]
pub fn or_else(self, f: || -> Option<T>) -> Option<T> {
match self {
Some(_) => self,
None => f()
}
}
/////////////////////////////////////////////////////////////////////////
// Misc
/////////////////////////////////////////////////////////////////////////
/// Takes the value out of the option, leaving a `None` in its place.
#[inline]
pub fn take(&mut self) -> Option<T> {
mem::replace(self, None)
}
/// Filters an optional value using a given function.
#[inline(always)]
pub fn filtered(self, f: |t: &T| -> bool) -> Option<T> {
match self {
Some(x) => if f(&x) { Some(x) } else { None },
None => None
}
}
/// Applies a function zero or more times until the result is `None`.
#[inline]
pub fn while_some(self, f: |v: T| -> Option<T>) {
let mut opt = self;
loop {
match opt {
Some(x) => opt = f(x),
None => break
}
}
}
/////////////////////////////////////////////////////////////////////////
// Common special cases
/////////////////////////////////////////////////////////////////////////
/// The option dance. Moves a value out of an option type and returns it,
/// replacing the original with `None`.
///
/// # Failure
///
/// Fails if the value equals `None`.
#[inline]
pub fn take_unwrap(&mut self) -> T {
match self.take() {
Some(x) => x,
None => fail!("called `Option::take_unwrap()` on a `None` value")
}
}
/// Gets an immutable reference to the value inside an option.
///
/// # Failure
///
/// Fails if the value equals `None`
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged
/// (calling `get` on `None` is akin to dereferencing a null pointer).
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn get_ref<'a>(&'a self) -> &'a T {
match *self {
Some(ref x) => x,
None => fail!("called `Option::get_ref()` on a `None` value"),
}
}
/// Gets a mutable reference to the value inside an option.
///
/// # Failure
///
/// Fails if the value equals `None`
///
/// # Safety note
///
/// In general, because this function may fail, its use is discouraged
/// (calling `get` on `None` is akin to dereferencing a null pointer).
/// Instead, prefer to use pattern matching and handle the `None`
/// case explicitly.
#[inline]
pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
match *self {
Some(ref mut x) => x,
None => fail!("called `Option::get_mut_ref()` on a `None` value"),
}
}
}
impl<T: Default> Option<T> {
/// Returns the contained value or a default
///
/// Consumes the `self` argument then, if `Some`, returns the contained
/// value, otherwise if `None`, returns the default value for that
/// type.
///
/// # Example
///
/// Convert a string to an integer, turning poorly-formed strings
/// into 0 (the default value for integers). `from_str` converts
/// a string to any other type that implements `FromStr`, returning
/// `None` on error.
///
/// ```
/// let good_year_from_input = "1909";
/// let bad_year_from_input = "190blarg";
/// let good_year = from_str(good_year_from_input).unwrap_or_default();
/// let bad_year = from_str(bad_year_from_input).unwrap_or_default();
///
/// assert_eq!(1909, good_year);
/// assert_eq!(0, bad_year);
/// ```
#[inline]
pub fn unwrap_or_default(self) -> T {
match self {
Some(x) => x,
None => Default::default()
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Trait implementations
/////////////////////////////////////////////////////////////////////////////
impl<T> Default for Option<T> {
#[inline]
fn default() -> Option<T> { None }
}
/////////////////////////////////////////////////////////////////////////////
// The Option Iterator
/////////////////////////////////////////////////////////////////////////////
/// An `Option` iterator that yields either one or zero elements
///
/// The `Item` iterator is returned by the `iter`, `mut_iter` and `move_iter`
/// methods on `Option`.
#[deriving(Clone)]
pub struct Item<A> {
opt: Option<A>
}
impl<A> Iterator<A> for Item<A> {
#[inline]
fn next(&mut self) -> Option<A> {
self.opt.take()
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
match self.opt {
Some(_) => (1, Some(1)),
None => (0, Some(0)),
}
}
}
impl<A> DoubleEndedIterator<A> for Item<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
self.opt.take()
}
}
impl<A> ExactSize<A> for Item<A> {}
/////////////////////////////////////////////////////////////////////////////
// Free functions
/////////////////////////////////////////////////////////////////////////////
/// Takes each element in the `Iterator`: if it is `None`, no further
/// elements are taken, and the `None` is returned. Should no `None` occur, a
/// vector containing the values of each `Option` is returned.
///
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Option<uint> {
/// if x == uint::MAX { return None; }
/// else { return Some(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Some(~[2u, 3, 4]));
#[inline]
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
let mut iter = iter.scan(false, |state, x| {
match x {
Some(x) => Some(x),
None => {
*state = true;
None
}
}
});
let v: V = FromIterator::from_iter(iter.by_ref());
if iter.state {
None
} else {
Some(v)
}
}
/////////////////////////////////////////////////////////////////////////////
// Tests
/////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use super::*;
use prelude::*;
use iter::range;
use str::StrSlice;
use kinds::marker;
use slice::ImmutableVector;
#[test]
fn test_get_ptr() {
unsafe {
let x = box 0;
let addr_x: *int = ::cast::transmute(&*x);
let opt = Some(x);
let y = opt.unwrap();
let addr_y: *int = ::cast::transmute(&*y);
assert_eq!(addr_x, addr_y);
}
}
#[test]
fn test_get_str() {
let x = "test".to_owned();
let addr_x = x.as_ptr();
let opt = Some(x);
let y = opt.unwrap();
let addr_y = y.as_ptr();
assert_eq!(addr_x, addr_y);
}
#[test]
fn test_get_resource() {
use rc::Rc;
use cell::RefCell;
struct R {
i: Rc<RefCell<int>>,
}
#[unsafe_destructor]
impl ::ops::Drop for R {
fn drop(&mut self) {
let ii = &*self.i;
let i = ii.borrow().clone();
*ii.borrow_mut() = i + 1;
}
}
fn R(i: Rc<RefCell<int>>) -> R {
R {
i: i
}
}
let i = Rc::new(RefCell::new(0));
{
let x = R(i.clone());
let opt = Some(x);
let _y = opt.unwrap();
}
assert_eq!(*i.borrow(), 1);
}
#[test]
fn test_option_dance() {
let x = Some(());
let mut y = Some(5);
let mut y2 = 0;
for _x in x.iter() {
y2 = y.take_unwrap();
}
assert_eq!(y2, 5);
assert!(y.is_none());
}
#[test] #[should_fail]
fn test_option_too_much_dance() {
let mut y = Some(marker::NoCopy);
let _y2 = y.take_unwrap();
let _y3 = y.take_unwrap();
}
#[test]
fn test_and() {
let x: Option<int> = Some(1);
assert_eq!(x.and(Some(2)), Some(2));
assert_eq!(x.and(None::<int>), None);
let x: Option<int> = None;
assert_eq!(x.and(Some(2)), None);
assert_eq!(x.and(None::<int>), None);
}
#[test]
fn test_and_then() {
let x: Option<int> = Some(1);
assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
assert_eq!(x.and_then(|_| None::<int>), None);
let x: Option<int> = None;
assert_eq!(x.and_then(|x| Some(x + 1)), None);
assert_eq!(x.and_then(|_| None::<int>), None);
}
#[test]
fn test_or() {
let x: Option<int> = Some(1);
assert_eq!(x.or(Some(2)), Some(1));
assert_eq!(x.or(None), Some(1));
let x: Option<int> = None;
assert_eq!(x.or(Some(2)), Some(2));
assert_eq!(x.or(None), None);
}
#[test]
fn test_or_else() {
let x: Option<int> = Some(1);
assert_eq!(x.or_else(|| Some(2)), Some(1));
assert_eq!(x.or_else(|| None), Some(1));
let x: Option<int> = None;
assert_eq!(x.or_else(|| Some(2)), Some(2));
assert_eq!(x.or_else(|| None), None);
}
#[test]
fn test_option_while_some() {
let mut i = 0;
Some(10).while_some(|j| {
i += 1;
if j > 0 {
Some(j-1)
} else {
None
}
});
assert_eq!(i, 11);
}
#[test]
fn test_unwrap() {
assert_eq!(Some(1).unwrap(), 1);
assert_eq!(Some("hello".to_owned()).unwrap(), "hello".to_owned());
}
#[test]
#[should_fail]
fn test_unwrap_fail1() {
let x: Option<int> = None;
x.unwrap();
}
#[test]
#[should_fail]
fn test_unwrap_fail2() {
let x: Option<~str> = None;
x.unwrap();
}
#[test]
fn test_unwrap_or() {
let x: Option<int> = Some(1);
assert_eq!(x.unwrap_or(2), 1);
let x: Option<int> = None;
assert_eq!(x.unwrap_or(2), 2);
}
#[test]
fn test_unwrap_or_else() {
let x: Option<int> = Some(1);
assert_eq!(x.unwrap_or_else(|| 2), 1);
let x: Option<int> = None;
assert_eq!(x.unwrap_or_else(|| 2), 2);
}
#[test]
fn test_filtered() {
let some_stuff = Some(42);
let modified_stuff = some_stuff.filtered(|&x| {x < 10});
assert_eq!(some_stuff.unwrap(), 42);
assert!(modified_stuff.is_none());
}
#[test]
fn test_iter() {
let val = 5;
let x = Some(val);
let mut it = x.iter();
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(&val));
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
}
#[test]
fn test_mut_iter() {
let val = 5;
let new_val = 11;
let mut x = Some(val);
{
let mut it = x.mut_iter();
assert_eq!(it.size_hint(), (1, Some(1)));
match it.next() {
Some(interior) => {
assert_eq!(*interior, val);
*interior = new_val;
}
None => assert!(false),
}
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
}
assert_eq!(x, Some(new_val));
}
#[test]
fn test_ord() {
let small = Some(1.0);
let big = Some(5.0);
let nan = Some(0.0/0.0);
assert!(!(nan < big));
assert!(!(nan > big));
assert!(small < big);
assert!(None < big);
assert!(big > None);
}
#[test]
fn test_mutate() {
let mut x = Some(3i);
assert!(x.mutate(|i| i+1));
assert_eq!(x, Some(4i));
assert!(x.mutate_or_set(0, |i| i+1));
assert_eq!(x, Some(5i));
x = None;
assert!(!x.mutate(|i| i+1));
assert_eq!(x, None);
assert!(!x.mutate_or_set(0i, |i| i+1));
assert_eq!(x, Some(0i));
}
#[test]
fn test_collect() {
let v: Option<~[int]> = collect(range(0, 0)
.map(|_| Some(0)));
assert_eq!(v, Some(box []));
let v: Option<~[int]> = collect(range(0, 3)
.map(|x| Some(x)));
assert_eq!(v, Some(box [0, 1, 2]));
let v: Option<~[int]> = collect(range(0, 3)
.map(|x| if x > 1 { None } else { Some(x) }));
assert_eq!(v, None);
// test that it does not take more elements than it needs
let mut functions = [|| Some(()), || None, || fail!()];
let v: Option<~[()]> = collect(functions.mut_iter().map(|f| (*f)()));
assert_eq!(v, None);
}
}

View File

@ -38,7 +38,7 @@ use ops::Drop;
use result::{Err, Ok, Result};
use ptr;
use str;
use str::{Str, StrSlice};
use str::{Str, StrSlice, StrAllocating};
use fmt;
use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
use path::{Path, GenericPath};
@ -81,6 +81,8 @@ pub fn getcwd() -> Path {
pub fn getcwd() -> Path {
use libc::DWORD;
use libc::GetCurrentDirectoryW;
use option::Expect;
let mut buf = [0 as u16, ..BUF_BYTES];
unsafe {
if libc::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) == 0 as DWORD {
@ -96,11 +98,11 @@ pub mod win32 {
use iter::Iterator;
use libc::types::os::arch::extra::DWORD;
use libc;
use option::{None, Option};
use option::{None, Option, Expect};
use option;
use os::TMPBUF_SZ;
use slice::{MutableVector, ImmutableVector, OwnedVector};
use str::StrSlice;
use str::{StrSlice, StrAllocating};
use str;
use vec::Vec;
@ -182,7 +184,6 @@ pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
#[cfg(windows)]
unsafe fn get_env_pairs() -> Vec<~[u8]> {
use c_str;
use str::StrSlice;
use libc::funcs::extra::kernel32::{
GetEnvironmentStringsA,
@ -830,6 +831,7 @@ fn real_args() -> ~[~str] {
#[cfg(windows)]
fn real_args() -> ~[~str] {
use slice;
use option::Expect;
let mut nArgs: c_int = 0;
let lpArgCount: *mut c_int = &mut nArgs;

View File

@ -21,7 +21,7 @@ use io::Writer;
use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Rev, Iterator, Map};
use option::{Option, Some, None};
use slice::{Vector, OwnedVector, ImmutableVector};
use str::{CharSplits, Str, StrVector, StrSlice};
use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
use strbuf::StrBuf;
use vec::Vec;
@ -684,7 +684,7 @@ impl Path {
}
}
fn normalize_<S: Str>(s: S) -> (Option<PathPrefix>, StrBuf) {
fn normalize_<S: StrAllocating>(s: S) -> (Option<PathPrefix>, StrBuf) {
// make borrowck happy
let (prefix, val) = {
let prefix = parse_prefix(s.as_slice());
@ -842,7 +842,7 @@ impl Path {
}
fn update_normalized<S: Str>(&mut self, s: S) {
let (prefix, path) = Path::normalize_(s);
let (prefix, path) = Path::normalize_(s.as_slice());
self.repr = path;
self.prefix = prefix;
self.update_sepidx();

View File

@ -65,18 +65,21 @@ pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Signed, Unsigned};
pub use num::{Primitive, Int, Float, ToPrimitive, FromPrimitive};
pub use option::Expect;
pub use owned::Box;
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
pub use ptr::RawPtr;
pub use io::{Buffer, Writer, Reader, Seek};
pub use result::{ResultUnwrap, ResultUnwrapErr};
pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
pub use str::{StrAllocating};
pub use to_str::{ToStr, IntoStr};
pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};
pub use slice::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCloneableVector};
pub use slice::{OwnedVector};
pub use slice::{MutableVector, MutableTotalOrdVector};
pub use slice::{MutableVector, MutableTotalOrdVector, MutableVectorAllocating};
pub use slice::{Vector, VectorVector, CloneableVector, ImmutableVector};
pub use strbuf::StrBuf;
pub use vec::Vec;

View File

@ -1,57 +0,0 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Utilities for references
#[cfg(not(test))]
use cmp::{Eq, Ord, Ordering, TotalEq, TotalOrd};
// Equality for region pointers
#[cfg(not(test))]
impl<'a, T: Eq> Eq for &'a T {
#[inline]
fn eq(&self, other: & &'a T) -> bool {
*(*self) == *(*other)
}
#[inline]
fn ne(&self, other: & &'a T) -> bool {
*(*self) != *(*other)
}
}
// Comparison for region pointers
#[cfg(not(test))]
impl<'a, T: Ord> Ord for &'a T {
#[inline]
fn lt(&self, other: & &'a T) -> bool {
*(*self) < *(*other)
}
#[inline]
fn le(&self, other: & &'a T) -> bool {
*(*self) <= *(*other)
}
#[inline]
fn ge(&self, other: & &'a T) -> bool {
*(*self) >= *(*other)
}
#[inline]
fn gt(&self, other: & &'a T) -> bool {
*(*self) > *(*other)
}
}
#[cfg(not(test))]
impl<'a, T: TotalOrd> TotalOrd for &'a T {
#[inline]
fn cmp(&self, other: & &'a T) -> Ordering { (**self).cmp(*other) }
}
#[cfg(not(test))]
impl<'a, T: TotalEq> TotalEq for &'a T {}

View File

@ -25,7 +25,7 @@ use option::{Some, None, Option};
use ptr::RawPtr;
use reflect;
use reflect::{MovePtr, align};
use result::{Ok, Err};
use result::{Ok, Err, ResultUnwrap};
use str::StrSlice;
use to_str::ToStr;
use slice::Vector;
@ -606,6 +606,7 @@ pub fn write_repr<T>(writer: &mut io::Writer, object: &T) -> io::IoResult<()> {
pub fn repr_to_str<T>(t: &T) -> ~str {
use str;
use str::StrAllocating;
use io;
let mut result = io::MemWriter::new();
@ -624,7 +625,6 @@ fn test_repr() {
use io::stdio::println;
use char::is_alphabetic;
use mem::swap;
use owned::Box;
fn exact_test<T>(t: &T, e:&str) {
let mut m = io::MemWriter::new();

View File

@ -266,262 +266,32 @@
//! the context. The caller of `fail!` should assume that execution
//! will not resume after failure, that failure is catastrophic.
use clone::Clone;
use cmp::Eq;
use std::fmt::Show;
use iter::{Iterator, FromIterator};
use option::{None, Option, Some};
use fmt::Show;
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
///
/// See the [`std::result`](index.html) module documentation for details.
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show, Hash)]
#[must_use]
pub enum Result<T, E> {
/// Contains the success value
Ok(T),
pub use core::result::{Result, Ok, Err, collect, fold, fold_};
/// Contains the error value
Err(E)
}
// FIXME: These traits should not exist. Once std::fmt is moved to libcore,
// these can once again become inherent methods on Result.
/////////////////////////////////////////////////////////////////////////////
// Type implementation
/////////////////////////////////////////////////////////////////////////////
impl<T, E> Result<T, E> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////
/// Returns true if the result is `Ok`
///
/// # Example
///
/// ~~~
/// use std::io::{File, Open, Write};
///
/// # fn do_not_run_example() { // creates a file
/// let mut file = File::open_mode(&Path::new("secret.txt"), Open, Write);
/// assert!(file.write_line("it's cold in here").is_ok());
/// # }
/// ~~~
#[inline]
pub fn is_ok(&self) -> bool {
match *self {
Ok(_) => true,
Err(_) => false
}
}
/// Returns true if the result is `Err`
///
/// # Example
///
/// ~~~
/// use std::io::{File, Open, Read};
///
/// // When opening with `Read` access, if the file does not exist
/// // then `open_mode` returns an error.
/// let bogus = File::open_mode(&Path::new("not_a_file.txt"), Open, Read);
/// assert!(bogus.is_err());
/// ~~~
#[inline]
pub fn is_err(&self) -> bool {
!self.is_ok()
}
/////////////////////////////////////////////////////////////////////////
// Adapter for each variant
/////////////////////////////////////////////////////////////////////////
/// Convert from `Result<T, E>` to `Option<T>`
///
/// Converts `self` into an `Option<T>`, consuming `self`,
/// and discarding the error, if any.
///
/// To convert to an `Option` without discarding the error value,
/// use `as_ref` to first convert the `Result<T, E>` into a
/// `Result<&T, &E>`.
///
/// # Examples
///
/// ~~~{.should_fail}
/// use std::io::{File, IoResult};
///
/// let bdays: IoResult<File> = File::open(&Path::new("important_birthdays.txt"));
/// let bdays: File = bdays.ok().expect("unable to open birthday file");
/// ~~~
#[inline]
pub fn ok(self) -> Option<T> {
match self {
Ok(x) => Some(x),
Err(_) => None,
}
}
/// Convert from `Result<T, E>` to `Option<E>`
///
/// Converts `self` into an `Option<T>`, consuming `self`,
/// and discarding the value, if any.
#[inline]
pub fn err(self) -> Option<E> {
match self {
Ok(_) => None,
Err(x) => Some(x),
}
}
/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
/// Convert from `Result<T, E>` to `Result<&T, &E>`
///
/// Produces a new `Result`, containing a reference
/// into the original, leaving the original in place.
#[inline]
pub fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E> {
match *self {
Ok(ref x) => Ok(x),
Err(ref x) => Err(x),
}
}
/// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
match *self {
Ok(ref mut x) => Ok(x),
Err(ref mut x) => Err(x),
}
}
/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////
/// Maps a `Result<T, E>` to `Result<U, E>` by applying a function to an
/// contained `Ok` value, leaving an `Err` value untouched.
///
/// This function can be used to compose the results of two functions.
///
/// # Examples
///
/// Sum the lines of a buffer by mapping strings to numbers,
/// ignoring I/O and parse errors:
///
/// ~~~
/// use std::io::{BufReader, IoResult};
///
/// let buffer = "1\n2\n3\n4\n";
/// let mut reader = BufReader::new(buffer.as_bytes());
///
/// let mut sum = 0;
///
/// while !reader.eof() {
/// let line: IoResult<~str> = reader.read_line();
/// // Convert the string line to a number using `map` and `from_str`
/// let val: IoResult<int> = line.map(|line| {
/// from_str::<int>(line).unwrap_or(0)
/// });
/// // Add the value if there were no errors, otherwise add 0
/// sum += val.ok().unwrap_or(0);
/// }
/// ~~~
#[inline]
pub fn map<U>(self, op: |T| -> U) -> Result<U,E> {
match self {
Ok(t) => Ok(op(t)),
Err(e) => Err(e)
}
}
/// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to an
/// contained `Err` value, leaving an `Ok` value untouched.
///
/// This function can be used to pass through a successful result while handling
/// an error.
#[inline]
pub fn map_err<F>(self, op: |E| -> F) -> Result<T,F> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(op(e))
}
}
////////////////////////////////////////////////////////////////////////
// Boolean operations on the values, eager and lazy
/////////////////////////////////////////////////////////////////////////
/// Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.
#[inline]
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E> {
match self {
Ok(_) => res,
Err(e) => Err(e),
}
}
/// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
///
/// This function can be used for control flow based on result values
#[inline]
pub fn and_then<U>(self, op: |T| -> Result<U, E>) -> Result<U, E> {
match self {
Ok(t) => op(t),
Err(e) => Err(e),
}
}
/// Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
#[inline]
pub fn or(self, res: Result<T, E>) -> Result<T, E> {
match self {
Ok(_) => self,
Err(_) => res,
}
}
/// Calls `op` if the result is `Err`, otherwise returns the `Ok` value of `self`.
///
/// This function can be used for control flow based on result values
#[inline]
pub fn or_else<F>(self, op: |E| -> Result<T, F>) -> Result<T, F> {
match self {
Ok(t) => Ok(t),
Err(e) => op(e),
}
}
/// Unwraps a result, yielding the content of an `Ok`.
/// Else it returns `optb`.
#[inline]
pub fn unwrap_or(self, optb: T) -> T {
match self {
Ok(t) => t,
Err(_) => optb
}
}
/// Unwraps a result, yielding the content of an `Ok`.
/// If the value is an `Err` then it calls `op` with its value.
#[inline]
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
match self {
Ok(t) => t,
Err(e) => op(e)
}
}
}
impl<T, E: Show> Result<T, E> {
/// Temporary trait for unwrapping a result
pub trait ResultUnwrap<T, E> {
/// Unwraps a result, yielding the content of an `Ok`.
///
/// Fails if the value is an `Err`.
fn unwrap(self) -> T;
}
/// Temporary trait for unwrapping the error of a result
pub trait ResultUnwrapErr<T, E> {
/// Unwraps a result, yielding the content of an `Err`.
///
/// Fails if the value is an `Ok`.
fn unwrap_err(self) -> E;
}
impl<T, E: Show> ResultUnwrap<T, E> for Result<T, E> {
#[inline]
pub fn unwrap(self) -> T {
fn unwrap(self) -> T {
match self {
Ok(t) => t,
Err(e) =>
@ -530,12 +300,9 @@ impl<T, E: Show> Result<T, E> {
}
}
impl<T: Show, E> Result<T, E> {
/// Unwraps a result, yielding the content of an `Err`.
///
/// Fails if the value is an `Ok`.
impl<T: Show, E> ResultUnwrapErr<T, E> for Result<T, E> {
#[inline]
pub fn unwrap_err(self) -> E {
fn unwrap_err(self) -> E {
match self {
Ok(t) =>
fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
@ -543,241 +310,3 @@ impl<T: Show, E> Result<T, E> {
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Free functions
/////////////////////////////////////////////////////////////////////////////
/// Takes each element in the `Iterator`: if it is an `Err`, no further
/// elements are taken, and the `Err` is returned. Should no `Err` occur, a
/// vector containing the values of each `Result` is returned.
///
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
/// if x == uint::MAX { return Err("overflow"); }
/// else { return Ok(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Ok(~[2u, 3, 4]));
#[inline]
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
let mut iter = iter.scan(None, |state, x| {
match x {
Ok(x) => Some(x),
Err(err) => {
*state = Some(err);
None
}
}
});
let v: V = FromIterator::from_iter(iter.by_ref());
match iter.state {
Some(err) => Err(err),
None => Ok(v),
}
}
/// Perform a fold operation over the result values from an iterator.
///
/// If an `Err` is encountered, it is immediately returned.
/// Otherwise, the folded value is returned.
#[inline]
pub fn fold<T,
V,
E,
Iter: Iterator<Result<T, E>>>(
mut iterator: Iter,
mut init: V,
f: |V, T| -> V)
-> Result<V, E> {
for t in iterator {
match t {
Ok(v) => init = f(init, v),
Err(u) => return Err(u)
}
}
Ok(init)
}
/// Perform a trivial fold operation over the result values
/// from an iterator.
///
/// If an `Err` is encountered, it is immediately returned.
/// Otherwise, a simple `Ok(())` is returned.
#[inline]
pub fn fold_<T,E,Iter:Iterator<Result<T,E>>>(iterator: Iter) -> Result<(),E> {
fold(iterator, (), |_, _| ())
}
/////////////////////////////////////////////////////////////////////////////
// Tests
/////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use super::*;
use prelude::*;
use str::StrSlice;
use iter::range;
pub fn op1() -> Result<int, ~str> { Ok(666) }
pub fn op2() -> Result<int, ~str> { Err("sadface".to_owned()) }
#[test]
pub fn test_and() {
assert_eq!(op1().and(Ok(667)).unwrap(), 667);
assert_eq!(op1().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "bad".to_owned());
assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface".to_owned());
assert_eq!(op2().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "sadface".to_owned());
}
#[test]
pub fn test_and_then() {
assert_eq!(op1().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap(), 667);
assert_eq!(op1().and_then(|_| Err::<int, ~str>("bad".to_owned())).unwrap_err(),
"bad".to_owned());
assert_eq!(op2().and_then(|i| Ok::<int, ~str>(i + 1)).unwrap_err(),
"sadface".to_owned());
assert_eq!(op2().and_then(|_| Err::<int, ~str>("bad".to_owned())).unwrap_err(),
"sadface".to_owned());
}
#[test]
pub fn test_or() {
assert_eq!(op1().or(Ok(667)).unwrap(), 666);
assert_eq!(op1().or(Err("bad".to_owned())).unwrap(), 666);
assert_eq!(op2().or(Ok(667)).unwrap(), 667);
assert_eq!(op2().or(Err("bad".to_owned())).unwrap_err(), "bad".to_owned());
}
#[test]
pub fn test_or_else() {
assert_eq!(op1().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 666);
assert_eq!(op1().or_else(|e| Err::<int, ~str>(e + "!")).unwrap(), 666);
assert_eq!(op2().or_else(|_| Ok::<int, ~str>(667)).unwrap(), 667);
assert_eq!(op2().or_else(|e| Err::<int, ~str>(e + "!")).unwrap_err(),
"sadface!".to_owned());
}
#[test]
pub fn test_impl_map() {
assert_eq!(Ok::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Ok("ab".to_owned()));
assert_eq!(Err::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Err("a".to_owned()));
}
#[test]
pub fn test_impl_map_err() {
assert_eq!(Ok::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Ok("a".to_owned()));
assert_eq!(Err::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Err("ab".to_owned()));
}
#[test]
fn test_collect() {
let v: Result<~[int], ()> = collect(range(0, 0).map(|_| Ok::<int, ()>(0)));
assert_eq!(v, Ok(box []));
let v: Result<~[int], ()> = collect(range(0, 3).map(|x| Ok::<int, ()>(x)));
assert_eq!(v, Ok(box [0, 1, 2]));
let v: Result<~[int], int> = collect(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(x) }));
assert_eq!(v, Err(2));
// test that it does not take more elements than it needs
let mut functions = [|| Ok(()), || Err(1), || fail!()];
let v: Result<~[()], int> = collect(functions.mut_iter().map(|f| (*f)()));
assert_eq!(v, Err(1));
}
#[test]
fn test_fold() {
assert_eq!(fold_(range(0, 0)
.map(|_| Ok::<(), ()>(()))),
Ok(()));
assert_eq!(fold(range(0, 3)
.map(|x| Ok::<int, ()>(x)),
0, |a, b| a + b),
Ok(3));
assert_eq!(fold_(range(0, 3)
.map(|x| if x > 1 { Err(x) } else { Ok(()) })),
Err(2));
// test that it does not take more elements than it needs
let mut functions = [|| Ok(()), || Err(1), || fail!()];
assert_eq!(fold_(functions.mut_iter()
.map(|f| (*f)())),
Err(1));
}
#[test]
pub fn test_to_str() {
let ok: Result<int, ~str> = Ok(100);
let err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(ok.to_str(), "Ok(100)".to_owned());
assert_eq!(err.to_str(), "Err(Err)".to_owned());
}
#[test]
pub fn test_fmt_default() {
let ok: Result<int, ~str> = Ok(100);
let err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(format!("{}", ok), "Ok(100)".to_owned());
assert_eq!(format!("{}", err), "Err(Err)".to_owned());
}
#[test]
pub fn test_unwrap_or() {
let ok: Result<int, ~str> = Ok(100);
let ok_err: Result<int, ~str> = Err("Err".to_owned());
assert_eq!(ok.unwrap_or(50), 100);
assert_eq!(ok_err.unwrap_or(50), 50);
}
#[test]
pub fn test_unwrap_or_else() {
fn handler(msg: ~str) -> int {
if msg == "I got this.".to_owned() {
50
} else {
fail!("BadBad")
}
}
let ok: Result<int, ~str> = Ok(100);
let ok_err: Result<int, ~str> = Err("I got this.".to_owned());
assert_eq!(ok.unwrap_or_handle(handler), 100);
assert_eq!(ok_err.unwrap_or_handle(handler), 50);
}
#[test]
#[should_fail]
pub fn test_unwrap_or_else_failure() {
fn handler(msg: ~str) -> int {
if msg == "I got this.".to_owned() {
50
} else {
fail!("BadBad")
}
}
let bad_err: Result<int, ~str> = Err("Unrecoverable mess.".to_owned());
let _ : int = bad_err.unwrap_or_handle(handler);
}
}

View File

@ -70,7 +70,6 @@ mod imp {
use owned::Box;
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use mem;
#[cfg(not(test))] use str::StrSlice;
#[cfg(not(test))] use ptr::RawPtr;
static mut global_args_ptr: uint = 0;

View File

@ -11,7 +11,7 @@
//! Runtime environment settings
use from_str::from_str;
use option::{Some, None};
use option::{Some, None, Expect};
use os;
// Note that these are all accessed without any synchronization.

View File

@ -70,7 +70,7 @@ use self::task::{Task, BlockedTask};
pub use self::util::default_sched_threads;
// Export unwinding facilities used by the failure macros
pub use self::unwind::{begin_unwind, begin_unwind_raw, begin_unwind_fmt};
pub use self::unwind::{begin_unwind, begin_unwind_fmt};
pub use self::util::{Stdio, Stdout, Stderr};

View File

@ -436,7 +436,7 @@ mod test {
#[test]
fn rng() {
use rand::{StdRng, Rng};
let mut r = StdRng::new().unwrap();
let mut r = StdRng::new().ok().unwrap();
let _ = r.next_u32();
}

View File

@ -58,7 +58,6 @@
// Currently Rust uses unwind runtime provided by libgcc.
use any::{Any, AnyRefExt};
use c_str::CString;
use cast;
use fmt;
use kinds::Send;
@ -298,42 +297,23 @@ pub mod eabi {
}
#[cold]
#[lang="fail_"]
#[no_mangle]
#[cfg(not(test))]
pub fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
begin_unwind_raw(expr, file, line);
}
#[cold]
#[lang="fail_bounds_check"]
#[cfg(not(test))]
pub fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
use c_str::ToCStr;
pub extern fn rust_fail_bounds_check(file: *u8, line: uint,
index: uint, len: uint) -> ! {
use str::raw::c_str_to_static_slice;
let msg = format!("index out of bounds: the len is {} but the index is {}",
len as uint, index as uint);
msg.with_c_str(|buf| fail_(buf as *u8, file, line))
begin_unwind(msg, unsafe { c_str_to_static_slice(file as *i8) }, line)
}
/// This is the entry point of unwinding for things like lang items and such.
/// The arguments are normally generated by the compiler, and need to
/// have static lifetimes.
#[inline(never)] #[cold] // this is the slow path, please never inline this
pub fn begin_unwind_raw(msg: *u8, file: *u8, line: uint) -> ! {
use libc::c_char;
#[inline]
fn static_char_ptr(p: *u8) -> &'static str {
let s = unsafe { CString::new(p as *c_char, false) };
match s.as_str() {
Some(s) => unsafe { cast::transmute::<&str, &'static str>(s) },
None => rtabort!("message wasn't utf8?")
}
}
let msg = static_char_ptr(msg);
let file = static_char_ptr(file);
begin_unwind(msg, file, line as uint)
// Entry point of failure from the libcore crate
#[no_mangle]
#[cfg(not(test))]
pub extern fn rust_begin_unwind(msg: &str, file: &'static str, line: uint) -> ! {
use str::StrAllocating;
begin_unwind(msg.to_owned(), file, line)
}
/// The entry point for unwinding with a formatted message.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -20,8 +20,8 @@ use iter::{Extendable, FromIterator, Iterator, range};
use option::{None, Option, Some};
use ptr::RawPtr;
use slice::{OwnedVector, Vector};
use str::{OwnedStr, Str, StrSlice, StrAllocating};
use str;
use str::{OwnedStr, Str, StrSlice};
use vec::Vec;
/// A growable string stored as a UTF-8 encoded buffer.
@ -268,7 +268,9 @@ impl Str for StrBuf {
cast::transmute(self.vec.as_slice())
}
}
}
impl StrAllocating for StrBuf {
#[inline]
fn into_owned(self) -> ~str {
let StrBuf {

Some files were not shown because too many files have changed in this diff Show More