core: Convert to rustdoc
This commit is contained in:
parent
a0521971b1
commit
b22556a6f8
|
@ -10,55 +10,36 @@ export from_str, to_str, all_values, to_bit;
|
||||||
#[doc = "The type of boolean logic values"]
|
#[doc = "The type of boolean logic values"]
|
||||||
type t = bool;
|
type t = bool;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Negation/Inverse"]
|
||||||
brief = "Negation/Inverse"
|
|
||||||
)]
|
|
||||||
pure fn not(v: t) -> t { !v }
|
pure fn not(v: t) -> t { !v }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Conjunction"]
|
||||||
brief = "Conjunction"
|
|
||||||
)]
|
|
||||||
pure fn and(a: t, b: t) -> t { a && b }
|
pure fn and(a: t, b: t) -> t { a && b }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Disjunction"]
|
||||||
brief = "Disjunction"
|
|
||||||
)]
|
|
||||||
pure fn or(a: t, b: t) -> t { a || b }
|
pure fn or(a: t, b: t) -> t { a || b }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Exclusive or, i.e. `or(and(a, not(b)), and(not(a), b))`"]
|
||||||
brief = "Exclusive or, i.e. `or(and(a, not(b)), and(not(a), b))`"
|
|
||||||
)]
|
|
||||||
pure fn xor(a: t, b: t) -> t { (a && !b) || (!a && b) }
|
pure fn xor(a: t, b: t) -> t { (a && !b) || (!a && b) }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Implication in the logic, i.e. from `a` follows `b`"]
|
||||||
brief = "Implication in the logic, i.e. from `a` follows `b`"
|
|
||||||
)]
|
|
||||||
pure fn implies(a: t, b: t) -> t { !a || b }
|
pure fn implies(a: t, b: t) -> t { !a || b }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "true if truth values `a` and `b` \
|
true if truth values `a` and `b` are indistinguishable in the logic
|
||||||
are indistinguishable in the logic"
|
"]
|
||||||
)]
|
|
||||||
pure fn eq(a: t, b: t) -> bool { a == b }
|
pure fn eq(a: t, b: t) -> bool { a == b }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "true if truth values `a` and `b` are distinguishable in the logic"]
|
||||||
brief = "true if truth values `a` and `b` are distinguishable in the logic"
|
|
||||||
)]
|
|
||||||
pure fn ne(a: t, b: t) -> bool { a != b }
|
pure fn ne(a: t, b: t) -> bool { a != b }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "true if `v` represents truth in the logic"]
|
||||||
brief = "true if `v` represents truth in the logic"
|
|
||||||
)]
|
|
||||||
pure fn is_true(v: t) -> bool { v }
|
pure fn is_true(v: t) -> bool { v }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "true if `v` represents falsehood in the logic"]
|
||||||
brief = "true if `v` represents falsehood in the logic"
|
|
||||||
)]
|
|
||||||
pure fn is_false(v: t) -> bool { !v }
|
pure fn is_false(v: t) -> bool { !v }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Parse logic value from `s`"]
|
||||||
brief = "Parse logic value from `s`"
|
|
||||||
)]
|
|
||||||
pure fn from_str(s: str) -> option<t> {
|
pure fn from_str(s: str) -> option<t> {
|
||||||
alt check s {
|
alt check s {
|
||||||
"true" { some(true) }
|
"true" { some(true) }
|
||||||
|
@ -67,23 +48,19 @@ pure fn from_str(s: str) -> option<t> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Convert `v` into a string"]
|
||||||
brief = "Convert `v` into a string"
|
|
||||||
)]
|
|
||||||
pure fn to_str(v: t) -> str { if v { "true" } else { "false" } }
|
pure fn to_str(v: t) -> str { if v { "true" } else { "false" } }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Iterates over all truth values by passing them to `blk` \
|
Iterates over all truth values by passing them to `blk` in an unspecified
|
||||||
in an unspecified order"
|
order
|
||||||
)]
|
"]
|
||||||
fn all_values(blk: fn(v: t)) {
|
fn all_values(blk: fn(v: t)) {
|
||||||
blk(true);
|
blk(true);
|
||||||
blk(false);
|
blk(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "converts truth value to an 8 bit byte"]
|
||||||
brief = "converts truth value to an 8 bit byte"
|
|
||||||
)]
|
|
||||||
pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
|
pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
#[doc = "Operations on shared box types"];
|
||||||
|
|
||||||
export ptr_eq;
|
export ptr_eq;
|
||||||
|
|
||||||
#[doc(
|
|
||||||
brief = "Determine if two shared boxes point to the same object"
|
|
||||||
)]
|
|
||||||
pure fn ptr_eq<T>(a: @T, b: @T) -> bool unchecked {
|
pure fn ptr_eq<T>(a: @T, b: @T) -> bool unchecked {
|
||||||
|
#[doc = "Determine if two shared boxes point to the same object"];
|
||||||
ptr::addr_of(*a) == ptr::addr_of(*b)
|
ptr::addr_of(*a) == ptr::addr_of(*b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,27 +45,27 @@ import is_XID_start = unicode::derived_property::XID_Start;
|
||||||
import is_XID_continue = unicode::derived_property::XID_Continue;
|
import is_XID_continue = unicode::derived_property::XID_Continue;
|
||||||
|
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Indicates whether a character is in lower case, defined \
|
Indicates whether a character is in lower case, defined
|
||||||
in terms of the Unicode General Category 'Ll'."
|
in terms of the Unicode General Category 'Ll'
|
||||||
)]
|
"]
|
||||||
pure fn is_lowercase(c: char) -> bool {
|
pure fn is_lowercase(c: char) -> bool {
|
||||||
ret unicode::general_category::Ll(c);
|
ret unicode::general_category::Ll(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Indicates whether a character is in upper case, defined \
|
Indicates whether a character is in upper case, defined
|
||||||
in terms of the Unicode General Category 'Lu'."
|
in terms of the Unicode General Category 'Lu'.
|
||||||
)]
|
"]
|
||||||
pure fn is_uppercase(c: char) -> bool {
|
pure fn is_uppercase(c: char) -> bool {
|
||||||
ret unicode::general_category::Lu(c);
|
ret unicode::general_category::Lu(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Indicates whether a character is whitespace, defined in \
|
Indicates whether a character is whitespace, defined in
|
||||||
terms of the Unicode General Categories 'Zs', 'Zl', 'Zp' \
|
terms of the Unicode General Categories 'Zs', 'Zl', 'Zp'
|
||||||
additional 'Cc'-category control codes in the range [0x09, 0x0d]"
|
additional 'Cc'-category control codes in the range [0x09, 0x0d]
|
||||||
)]
|
"]
|
||||||
pure fn is_whitespace(c: char) -> bool {
|
pure fn is_whitespace(c: char) -> bool {
|
||||||
ret ('\x09' <= c && c <= '\x0d')
|
ret ('\x09' <= c && c <= '\x0d')
|
||||||
|| unicode::general_category::Zs(c)
|
|| unicode::general_category::Zs(c)
|
||||||
|
@ -73,11 +73,11 @@ pure fn is_whitespace(c: char) -> bool {
|
||||||
|| unicode::general_category::Zp(c);
|
|| unicode::general_category::Zp(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Indicates whether a character is alphanumeric, defined \
|
Indicates whether a character is alphanumeric, defined
|
||||||
in terms of the Unicode General Categories 'Nd', \
|
in terms of the Unicode General Categories 'Nd',
|
||||||
'Nl', 'No' and the Derived Core Property 'Alphabetic'."
|
'Nl', 'No' and the Derived Core Property 'Alphabetic'.
|
||||||
)]
|
"]
|
||||||
pure fn is_alphanumeric(c: char) -> bool {
|
pure fn is_alphanumeric(c: char) -> bool {
|
||||||
ret unicode::derived_property::Alphabetic(c) ||
|
ret unicode::derived_property::Alphabetic(c) ||
|
||||||
unicode::general_category::Nd(c) ||
|
unicode::general_category::Nd(c) ||
|
||||||
|
@ -85,26 +85,32 @@ pure fn is_alphanumeric(c: char) -> bool {
|
||||||
unicode::general_category::No(c);
|
unicode::general_category::No(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc( brief = "Indicates whether the character is an ASCII character" )]
|
#[doc = "Indicates whether the character is an ASCII character"]
|
||||||
pure fn is_ascii(c: char) -> bool {
|
pure fn is_ascii(c: char) -> bool {
|
||||||
c - ('\x7F' & c) == '\x00'
|
c - ('\x7F' & c) == '\x00'
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc( brief = "Indicates whether the character is numeric (Nd, Nl, or No)" )]
|
#[doc = "Indicates whether the character is numeric (Nd, Nl, or No)"]
|
||||||
pure fn is_digit(c: char) -> bool {
|
pure fn is_digit(c: char) -> bool {
|
||||||
ret unicode::general_category::Nd(c) ||
|
ret unicode::general_category::Nd(c) ||
|
||||||
unicode::general_category::Nl(c) ||
|
unicode::general_category::Nl(c) ||
|
||||||
unicode::general_category::No(c);
|
unicode::general_category::No(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Convert a char to the corresponding digit. \
|
Convert a char to the corresponding digit.
|
||||||
Safety note: This function fails if `c` is not a valid char",
|
|
||||||
return = "If `c` is between '0' and '9', the corresponding value \
|
# Safety note
|
||||||
between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is \
|
|
||||||
'b' or 'B', 11, etc. Returns none if the char does not \
|
This function fails if `c` is not a valid char
|
||||||
refer to a digit in the given radix."
|
|
||||||
)]
|
# Return value
|
||||||
|
|
||||||
|
If `c` is between '0' and '9', the corresponding value
|
||||||
|
between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is
|
||||||
|
'b' or 'B', 11, etc. Returns none if the char does not
|
||||||
|
refer to a digit in the given radix.
|
||||||
|
"]
|
||||||
pure fn to_digit(c: char, radix: uint) -> option<uint> {
|
pure fn to_digit(c: char, radix: uint) -> option<uint> {
|
||||||
let val = alt c {
|
let val = alt c {
|
||||||
'0' to '9' { c as uint - ('0' as uint) }
|
'0' to '9' { c as uint - ('0' as uint) }
|
||||||
|
@ -119,9 +125,7 @@ pure fn to_digit(c: char, radix: uint) -> option<uint> {
|
||||||
/*
|
/*
|
||||||
FIXME: works only on ASCII
|
FIXME: works only on ASCII
|
||||||
*/
|
*/
|
||||||
#[doc(
|
#[doc = "Convert a char to the corresponding lower case."]
|
||||||
brief = "Convert a char to the corresponding lower case."
|
|
||||||
)]
|
|
||||||
pure fn to_lower(c: char) -> char {
|
pure fn to_lower(c: char) -> char {
|
||||||
alt c {
|
alt c {
|
||||||
'A' to 'Z' { ((c as u8) + 32u8) as char }
|
'A' to 'Z' { ((c as u8) + 32u8) as char }
|
||||||
|
@ -132,9 +136,7 @@ pure fn to_lower(c: char) -> char {
|
||||||
/*
|
/*
|
||||||
FIXME: works only on ASCII
|
FIXME: works only on ASCII
|
||||||
*/
|
*/
|
||||||
#[doc(
|
#[doc = "Convert a char to the corresponding upper case."]
|
||||||
brief = "Convert a char to the corresponding upper case."
|
|
||||||
)]
|
|
||||||
pure fn to_upper(c: char) -> char {
|
pure fn to_upper(c: char) -> char {
|
||||||
alt c {
|
alt c {
|
||||||
'a' to 'z' { ((c as u8) - 32u8) as char }
|
'a' to 'z' { ((c as u8) - 32u8) as char }
|
||||||
|
@ -142,10 +144,13 @@ pure fn to_upper(c: char) -> char {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Compare two chars.",
|
Compare two chars
|
||||||
return = "-1 if a<b, 0 if a==b, +1 if a>b"
|
|
||||||
)]
|
# Return value
|
||||||
|
|
||||||
|
-1 if a < b, 0 if a == b, +1 if a > b
|
||||||
|
"]
|
||||||
pure fn cmp(a: char, b: char) -> int {
|
pure fn cmp(a: char, b: char) -> int {
|
||||||
ret if b > a { -1 }
|
ret if b > a { -1 }
|
||||||
else if b < a { 1 }
|
else if b < a { 1 }
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Communication between tasks",
|
Communication between tasks
|
||||||
desc = "
|
|
||||||
|
|
||||||
Communication between tasks is facilitated by ports (in the receiving
|
Communication between tasks is facilitated by ports (in the receiving
|
||||||
task), and channels (in the sending task). Any number of channels may
|
task), and channels (in the sending task). Any number of channels may
|
||||||
|
@ -11,15 +10,19 @@ vectors, strings, and records, tags, tuples and unique boxes (`~T`)
|
||||||
thereof. Most notably, shared boxes (`@T`) may not be transmitted
|
thereof. Most notably, shared boxes (`@T`) may not be transmitted
|
||||||
across channels.
|
across channels.
|
||||||
|
|
||||||
Example:
|
# Example
|
||||||
|
|
||||||
let p = comm::port();
|
~~~
|
||||||
task::spawn(comm::chan(p), fn (c: chan<str>) {
|
let po = comm::port();
|
||||||
comm::send(c, \"Hello, World\");
|
let ch = comm::chan(po);
|
||||||
});
|
|
||||||
io::println(comm::recv(p));
|
|
||||||
|
|
||||||
")];
|
task::spawn {||
|
||||||
|
comm::send(ch, \"Hello, World\");
|
||||||
|
});
|
||||||
|
|
||||||
|
io::println(comm::recv(p));
|
||||||
|
~~~
|
||||||
|
"];
|
||||||
|
|
||||||
import sys;
|
import sys;
|
||||||
import task;
|
import task;
|
||||||
|
@ -65,18 +68,16 @@ type port_id = int;
|
||||||
|
|
||||||
// It's critical that this only have one variant, so it has a record
|
// It's critical that this only have one variant, so it has a record
|
||||||
// layout, and will work in the rust_task structure in task.rs.
|
// layout, and will work in the rust_task structure in task.rs.
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "A communication endpoint that can send messages. \
|
A communication endpoint that can send messages
|
||||||
Channels send messages to ports.",
|
|
||||||
desc = "Each channel is bound to a port when the channel is \
|
Each channel is bound to a port when the channel is constructed, so
|
||||||
constructed, so the destination port for a channel \
|
the destination port for a channel must exist before the channel
|
||||||
must exist before the channel itself. \
|
itself. Channels are weak: a channel does not keep the port it is
|
||||||
Channels are weak: a channel does not keep the port it \
|
bound to alive. If a channel attempts to send data to a dead port that
|
||||||
is bound to alive. If a channel attempts to send data \
|
data will be silently dropped. Channels may be duplicated and
|
||||||
to a dead port that data will be silently dropped. \
|
themselves transmitted over other channels.
|
||||||
Channels may be duplicated and themselves transmitted \
|
"]
|
||||||
over other channels."
|
|
||||||
)]
|
|
||||||
enum chan<T: send> {
|
enum chan<T: send> {
|
||||||
chan_t(task_id, port_id)
|
chan_t(task_id, port_id)
|
||||||
}
|
}
|
||||||
|
@ -104,21 +105,19 @@ resource port_ptr<T: send>(po: *rust_port) {
|
||||||
rustrt::del_port(po);
|
rustrt::del_port(po);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "A communication endpoint that can receive messages. \
|
A communication endpoint that can receive messages
|
||||||
Ports receive messages from channels.",
|
|
||||||
desc = "Each port has a unique per-task identity and may not \
|
Each port has a unique per-task identity and may not be replicated or
|
||||||
be replicated or transmitted. If a port value is \
|
transmitted. If a port value is copied, both copies refer to the same
|
||||||
copied, both copies refer to the same port. \
|
port. Ports may be associated with multiple `chan`s.
|
||||||
Ports may be associated with multiple <chan>s."
|
"]
|
||||||
)]
|
|
||||||
enum port<T: send> { port_t(@port_ptr<T>) }
|
enum port<T: send> { port_t(@port_ptr<T>) }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Sends data over a channel. The sent data is moved \
|
Sends data over a channel. The sent data is moved into the channel,
|
||||||
into the channel, whereupon the caller loses \
|
whereupon the caller loses access to it.
|
||||||
access to it."
|
"]
|
||||||
)]
|
|
||||||
fn send<T: send>(ch: chan<T>, -data: T) {
|
fn send<T: send>(ch: chan<T>, -data: T) {
|
||||||
let chan_t(t, p) = ch;
|
let chan_t(t, p) = ch;
|
||||||
let res = rustrt::chan_id_send(sys::get_type_desc::<T>(), t, p, data);
|
let res = rustrt::chan_id_send(sys::get_type_desc::<T>(), t, p, data);
|
||||||
|
@ -129,23 +128,18 @@ fn send<T: send>(ch: chan<T>, -data: T) {
|
||||||
task::yield();
|
task::yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Constructs a port"]
|
||||||
brief = "Constructs a port."
|
|
||||||
)]
|
|
||||||
fn port<T: send>() -> port<T> {
|
fn port<T: send>() -> port<T> {
|
||||||
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
|
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Receive from a port. \
|
Receive from a port. If no data is available on the port then the
|
||||||
If no data is available on the port then the task will \
|
task will block until data becomes available.
|
||||||
block until data becomes available."
|
"]
|
||||||
)]
|
|
||||||
fn recv<T: send>(p: port<T>) -> T { recv_(***p) }
|
fn recv<T: send>(p: port<T>) -> T { recv_(***p) }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Receive on a raw port pointer"]
|
||||||
brief = "Receive on a raw port pointer"
|
|
||||||
)]
|
|
||||||
fn recv_<T: send>(p: *rust_port) -> T {
|
fn recv_<T: send>(p: *rust_port) -> T {
|
||||||
// FIXME: Due to issue 1185 we can't use a return pointer when
|
// FIXME: Due to issue 1185 we can't use a return pointer when
|
||||||
// calling C code, and since we can't create our own return
|
// calling C code, and since we can't create our own return
|
||||||
|
@ -218,10 +212,10 @@ fn peek<T: send>(p: port<T>) -> bool {
|
||||||
rustrt::rust_port_size(***p) != 0u as ctypes::size_t
|
rustrt::rust_port_size(***p) != 0u as ctypes::size_t
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Constructs a channel. The channel is bound to the \
|
Constructs a channel. The channel is bound to the port used to
|
||||||
port used to construct it."
|
construct it.
|
||||||
)]
|
"]
|
||||||
fn chan<T: send>(p: port<T>) -> chan<T> {
|
fn chan<T: send>(p: port<T>) -> chan<T> {
|
||||||
chan_t(rustrt::get_task_id(), rustrt::get_port_id(***p))
|
chan_t(rustrt::get_task_id(), rustrt::get_port_id(***p))
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,8 @@
|
||||||
// Don't link to core. We are core.
|
// Don't link to core. We are core.
|
||||||
#[no_core];
|
#[no_core];
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "The Rust core library",
|
The Rust core library
|
||||||
desc = "
|
|
||||||
|
|
||||||
The core library provides functionality that is closely tied to the Rust
|
The core library provides functionality that is closely tied to the Rust
|
||||||
built-in types and runtime services, or that is used in nearly every
|
built-in types and runtime services, or that is used in nearly every
|
||||||
|
@ -24,9 +23,8 @@ as though the user had written the following:
|
||||||
use core;
|
use core;
|
||||||
import core::*;
|
import core::*;
|
||||||
|
|
||||||
This behavior can be disabled with the `no_core` crate attribute."
|
This behavior can be disabled with the `no_core` crate attribute.
|
||||||
|
"];
|
||||||
)];
|
|
||||||
|
|
||||||
export int, i8, i16, i32, i64;
|
export int, i8, i16, i32, i64;
|
||||||
export uint, u8, u16, u32, u64;
|
export uint, u8, u16, u32, u64;
|
||||||
|
|
|
@ -40,13 +40,11 @@ mod std {
|
||||||
import std::test;
|
import std::test;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: unreachable
|
|
||||||
|
|
||||||
A standard function to use to indicate unreachable code. Because the
|
A standard function to use to indicate unreachable code. Because the
|
||||||
function is guaranteed to fail typestate will correctly identify
|
function is guaranteed to fail typestate will correctly identify
|
||||||
any code paths following the appearance of this function as unreachable.
|
any code paths following the appearance of this function as unreachable.
|
||||||
*/
|
"]
|
||||||
fn unreachable() -> ! {
|
fn unreachable() -> ! {
|
||||||
fail "Internal error: entered unreachable code";
|
fail "Internal error: entered unreachable code";
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,103 +19,73 @@ export enum;
|
||||||
#[doc = "A signed integer with the same size as a C `char`."]
|
#[doc = "A signed integer with the same size as a C `char`."]
|
||||||
type c_char = i8;
|
type c_char = i8;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `int`."]
|
||||||
brief = "A signed integer with the same size as a C `int`."
|
|
||||||
)]
|
|
||||||
type c_int = i32;
|
type c_int = i32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "An unsigned integer with the same size as a C `unsigned int`."]
|
||||||
brief = "An unsigned integer with the same size as a C `unsigned int`."
|
|
||||||
)]
|
|
||||||
type c_uint = u32;
|
type c_uint = u32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `long`."]
|
||||||
brief = "A signed integer with the same size as a C `long`."
|
|
||||||
)]
|
|
||||||
type long = int;
|
type long = int;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `long long`."]
|
||||||
brief = "A signed integer with the same size as a C `long long`."
|
|
||||||
)]
|
|
||||||
type longlong = i64;
|
type longlong = i64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `unsigned int`."]
|
||||||
brief = "A signed integer with the same size as a C `unsigned int`."
|
|
||||||
)]
|
|
||||||
type unsigned = u32;
|
type unsigned = u32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `unsigned long`."]
|
||||||
brief = "A signed integer with the same size as a C `unsigned long`."
|
|
||||||
)]
|
|
||||||
type ulong = uint;
|
type ulong = uint;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer with the same size as a C `unsigned long long`."]
|
||||||
brief = "A signed integer with the same size as a C `unsigned long long`."
|
|
||||||
)]
|
|
||||||
type ulonglong = u64;
|
type ulonglong = u64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "A signed integer with the same size as a pointer. \
|
A signed integer with the same size as a pointer. This is guaranteed
|
||||||
This is guaranteed to always be the same type as a \
|
to always be the same type as a Rust `int`.
|
||||||
Rust `int`."
|
"]
|
||||||
)]
|
|
||||||
type intptr_t = uint; // FIXME: int
|
type intptr_t = uint; // FIXME: int
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "An unsigned integer with the same size as a pointer. \
|
An unsigned integer with the same size as a pointer. This is
|
||||||
This is guaranteed to always be the same type as a Rust \
|
guaranteed to always be the same type as a Rust `uint`.
|
||||||
`uint`."
|
"]
|
||||||
)]
|
|
||||||
type uintptr_t = uint;
|
type uintptr_t = uint;
|
||||||
type uint32_t = u32;
|
type uint32_t = u32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "A type, a pointer to which can be used as C `void *`.",
|
A type, a pointer to which can be used as C `void *`.
|
||||||
desc = "The void type cannot be constructed or destructured, \
|
|
||||||
but using pointers to this type when interoperating \
|
The void type cannot be constructed or destructured, but using
|
||||||
with C void pointers can help in documentation."
|
pointers to this type when interoperating with C void pointers can
|
||||||
)]
|
help in documentation.
|
||||||
|
"]
|
||||||
enum void {}
|
enum void {}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A float value with the same size as a C `float`."]
|
||||||
brief = "A float value with the same size as a C `float`."
|
|
||||||
)]
|
|
||||||
type c_float = f32;
|
type c_float = f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A float value with the same size as a C `double`."]
|
||||||
brief = "A float value with the same size as a C `double`."
|
|
||||||
)]
|
|
||||||
type c_double = f64;
|
type c_double = f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "An unsigned integer corresponding to the C `size_t`."]
|
||||||
brief = "An unsigned integer corresponding to the C `size_t`."
|
|
||||||
)]
|
|
||||||
type size_t = uint;
|
type size_t = uint;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A signed integer corresponding to the C `ssize_t`."]
|
||||||
brief = "A signed integer corresponding to the C `ssize_t`."
|
|
||||||
)]
|
|
||||||
type ssize_t = int;
|
type ssize_t = int;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "An unsigned integer corresponding to the C `off_t`."]
|
||||||
brief = "An unsigned integer corresponding to the C `off_t`."
|
|
||||||
)]
|
|
||||||
type off_t = uint;
|
type off_t = uint;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A type that can be used for C file descriptors."]
|
||||||
brief = "A type that can be used for C file descriptors."
|
|
||||||
)]
|
|
||||||
type fd_t = i32; // not actually a C type, but should be.
|
type fd_t = i32; // not actually a C type, but should be.
|
||||||
|
|
||||||
#[doc(
|
#[doc = "A type for representing process ID's, corresponding to C `pid_t`."]
|
||||||
brief = "A type for representing process ID's, corresponding to C `pid_t`."
|
|
||||||
)]
|
|
||||||
type pid_t = i32;
|
type pid_t = i32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "An unsigned integer with the same size as a C enum. \
|
An unsigned integer with the same size as a C enum. enum is
|
||||||
enum is implementation-defined, but is 32-bits in \
|
implementation-defined, but is 32-bits in practice
|
||||||
practice"
|
"]
|
||||||
)]
|
|
||||||
type enum = u32;
|
type enum = u32;
|
||||||
|
|
||||||
|
|
|
@ -1,45 +1,27 @@
|
||||||
/*
|
#[doc = "A type that represents one of two alternatives"];
|
||||||
Module: either
|
|
||||||
|
|
||||||
A type that represents one of two alternatives
|
#[doc = "The either type"]
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Tag: t
|
|
||||||
|
|
||||||
The either type
|
|
||||||
*/
|
|
||||||
enum t<T, U> {
|
enum t<T, U> {
|
||||||
/* Variant: left */
|
|
||||||
left(T),
|
left(T),
|
||||||
/* Variant: right */
|
|
||||||
right(U)
|
right(U)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Section: Operations */
|
fn either<T, U, V>(f_left: fn(T) -> V,
|
||||||
|
f_right: fn(U) -> V, value: t<T, U>) -> V {
|
||||||
|
#[doc = "
|
||||||
|
Applies a function based on the given either value
|
||||||
|
|
||||||
/*
|
If `value` is left(T) then `f_left` is applied to its contents, if `value`
|
||||||
Function: either
|
is right(U) then `f_right` is applied to its contents, and the result is
|
||||||
|
returned.
|
||||||
|
"];
|
||||||
|
|
||||||
Applies a function based on the given either value
|
|
||||||
|
|
||||||
If `value` is left(T) then `f_left` is applied to its contents, if
|
|
||||||
`value` is right(U) then `f_right` is applied to its contents, and
|
|
||||||
the result is returned.
|
|
||||||
*/
|
|
||||||
fn either<T, U,
|
|
||||||
V>(f_left: fn(T) -> V, f_right: fn(U) -> V, value: t<T, U>) ->
|
|
||||||
V {
|
|
||||||
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
|
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: lefts
|
|
||||||
|
|
||||||
Extracts from a vector of either all the left values.
|
|
||||||
*/
|
|
||||||
fn lefts<T: copy, U>(eithers: [t<T, U>]) -> [T] {
|
fn lefts<T: copy, U>(eithers: [t<T, U>]) -> [T] {
|
||||||
|
#[doc = "Extracts from a vector of either all the left values"];
|
||||||
|
|
||||||
let mut result: [T] = [];
|
let mut result: [T] = [];
|
||||||
for elt: t<T, U> in eithers {
|
for elt: t<T, U> in eithers {
|
||||||
alt elt { left(l) { result += [l]; } _ {/* fallthrough */ } }
|
alt elt { left(l) { result += [l]; } _ {/* fallthrough */ } }
|
||||||
|
@ -47,12 +29,9 @@ fn lefts<T: copy, U>(eithers: [t<T, U>]) -> [T] {
|
||||||
ret result;
|
ret result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: rights
|
|
||||||
|
|
||||||
Extracts from a vector of either all the right values
|
|
||||||
*/
|
|
||||||
fn rights<T, U: copy>(eithers: [t<T, U>]) -> [U] {
|
fn rights<T, U: copy>(eithers: [t<T, U>]) -> [U] {
|
||||||
|
#[doc = "Extracts from a vector of either all the right values"];
|
||||||
|
|
||||||
let mut result: [U] = [];
|
let mut result: [U] = [];
|
||||||
for elt: t<T, U> in eithers {
|
for elt: t<T, U> in eithers {
|
||||||
alt elt { right(r) { result += [r]; } _ {/* fallthrough */ } }
|
alt elt { right(r) { result += [r]; } _ {/* fallthrough */ } }
|
||||||
|
@ -60,16 +39,15 @@ fn rights<T, U: copy>(eithers: [t<T, U>]) -> [U] {
|
||||||
ret result;
|
ret result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: partition
|
|
||||||
|
|
||||||
Extracts from a vector of either all the left values and right values
|
|
||||||
|
|
||||||
Returns a structure containing a vector of left values and a vector of
|
|
||||||
right values.
|
|
||||||
*/
|
|
||||||
fn partition<T: copy, U: copy>(eithers: [t<T, U>])
|
fn partition<T: copy, U: copy>(eithers: [t<T, U>])
|
||||||
-> {lefts: [T], rights: [U]} {
|
-> {lefts: [T], rights: [U]} {
|
||||||
|
#[doc = "
|
||||||
|
Extracts from a vector of either all the left values and right values
|
||||||
|
|
||||||
|
Returns a structure containing a vector of left values and a vector of
|
||||||
|
right values.
|
||||||
|
"];
|
||||||
|
|
||||||
let mut lefts: [T] = [];
|
let mut lefts: [T] = [];
|
||||||
let mut rights: [U] = [];
|
let mut rights: [U] = [];
|
||||||
for elt: t<T, U> in eithers {
|
for elt: t<T, U> in eithers {
|
||||||
|
@ -78,59 +56,41 @@ fn partition<T: copy, U: copy>(eithers: [t<T, U>])
|
||||||
ret {lefts: lefts, rights: rights};
|
ret {lefts: lefts, rights: rights};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: flip
|
|
||||||
|
|
||||||
Flips between left and right of a given either
|
|
||||||
*/
|
|
||||||
pure fn flip<T: copy, U: copy>(eith: t<T, U>) -> t<U, T> {
|
pure fn flip<T: copy, U: copy>(eith: t<T, U>) -> t<U, T> {
|
||||||
|
#[doc = "Flips between left and right of a given either"];
|
||||||
|
|
||||||
alt eith {
|
alt eith {
|
||||||
right(r) { left(r) }
|
right(r) { left(r) }
|
||||||
left(l) { right(l) }
|
left(l) { right(l) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: to_result
|
|
||||||
|
|
||||||
Converts either::t to a result::t, making the "right" choice
|
|
||||||
an ok result, and the "left" choice a fail
|
|
||||||
*/
|
|
||||||
pure fn to_result<T: copy, U: copy>(eith: t<T, U>) -> result::t<U, T> {
|
pure fn to_result<T: copy, U: copy>(eith: t<T, U>) -> result::t<U, T> {
|
||||||
|
#[doc = "
|
||||||
|
Converts either::t to a result::t
|
||||||
|
|
||||||
|
Converts an `either` type to a `result` type, making the \"right\" choice
|
||||||
|
an ok result, and the \"left\" choice a fail
|
||||||
|
"];
|
||||||
|
|
||||||
alt eith {
|
alt eith {
|
||||||
right(r) { result::ok(r) }
|
right(r) { result::ok(r) }
|
||||||
left(l) { result::err(l) }
|
left(l) { result::err(l) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: is_left
|
|
||||||
|
|
||||||
Checks whether the given value is a left
|
|
||||||
*/
|
|
||||||
pure fn is_left<T, U>(eith: t<T, U>) -> bool {
|
pure fn is_left<T, U>(eith: t<T, U>) -> bool {
|
||||||
|
#[doc = "Checks whether the given value is a left"];
|
||||||
|
|
||||||
alt eith { left(_) { true } _ { false } }
|
alt eith { left(_) { true } _ { false } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: is_right
|
|
||||||
|
|
||||||
Checks whether the given value is a right
|
|
||||||
*/
|
|
||||||
pure fn is_right<T, U>(eith: t<T, U>) -> bool {
|
pure fn is_right<T, U>(eith: t<T, U>) -> bool {
|
||||||
|
#[doc = "Checks whether the given value is a right"];
|
||||||
|
|
||||||
alt eith { right(_) { true } _ { false } }
|
alt eith { right(_) { true } _ { false } }
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Local Variables:
|
|
||||||
// mode: rust
|
|
||||||
// fill-column: 78;
|
|
||||||
// indent-tabs-mode: nil
|
|
||||||
// c-basic-offset: 4
|
|
||||||
// buffer-file-coding-system: utf-8-unix
|
|
||||||
// End:
|
|
||||||
//
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_either_left() {
|
fn test_either_left() {
|
||||||
let val = left(10);
|
let val = left(10);
|
||||||
|
@ -223,3 +183,13 @@ fn test_partition_empty() {
|
||||||
assert (vec::len(result.lefts) == 0u);
|
assert (vec::len(result.lefts) == 0u);
|
||||||
assert (vec::len(result.rights) == 0u);
|
assert (vec::len(result.rights) == 0u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Local Variables:
|
||||||
|
// mode: rust
|
||||||
|
// fill-column: 78;
|
||||||
|
// indent-tabs-mode: nil
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// buffer-file-coding-system: utf-8-unix
|
||||||
|
// End:
|
||||||
|
//
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#[doc(hidden)];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Syntax Extension: fmt
|
Syntax Extension: fmt
|
||||||
|
|
||||||
|
|
|
@ -58,56 +58,49 @@ pure fn gt(x: f32, y: f32) -> bool { ret x > y; }
|
||||||
// FIXME replace the predicates below with llvm intrinsics or calls
|
// FIXME replace the predicates below with llvm intrinsics or calls
|
||||||
// to the libmath macros in the rust runtime for performance
|
// to the libmath macros in the rust runtime for performance
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a positive number, including +0.0f320 \
|
Returns true if `x` is a positive number, including +0.0f320 and +Infinity
|
||||||
and +Infinity."
|
"]
|
||||||
)]
|
|
||||||
pure fn is_positive(x: f32) -> bool
|
pure fn is_positive(x: f32) -> bool
|
||||||
{ ret x > 0.0f32 || (1.0f32/x) == infinity; }
|
{ ret x > 0.0f32 || (1.0f32/x) == infinity; }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a negative number, including -0.0f320 \
|
Returns true if `x` is a negative number, including -0.0f320 and -Infinity
|
||||||
and -Infinity."
|
"]
|
||||||
)]
|
|
||||||
pure fn is_negative(x: f32) -> bool
|
pure fn is_negative(x: f32) -> bool
|
||||||
{ ret x < 0.0f32 || (1.0f32/x) == neg_infinity; }
|
{ ret x < 0.0f32 || (1.0f32/x) == neg_infinity; }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a negative number, including \
|
Returns true if `x` is a negative number, including -0.0f320 and -Infinity
|
||||||
-0.0f320 and -Infinity. (This is the same as \
|
|
||||||
`f32::negative`.)"
|
This is the same as `f32::is_negative`.
|
||||||
)]
|
"]
|
||||||
pure fn is_nonpositive(x: f32) -> bool {
|
pure fn is_nonpositive(x: f32) -> bool {
|
||||||
ret x < 0.0f32 || (1.0f32/x) == neg_infinity;
|
ret x < 0.0f32 || (1.0f32/x) == neg_infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a positive number, \
|
Returns true if `x` is a positive number, including +0.0f320 and +Infinity
|
||||||
including +0.0f320 and +Infinity. (This is \
|
|
||||||
the same as `f32::positive`.)"
|
This is the same as `f32::is_positive`.)
|
||||||
)]
|
"]
|
||||||
pure fn is_nonnegative(x: f32) -> bool {
|
pure fn is_nonnegative(x: f32) -> bool {
|
||||||
ret x > 0.0f32 || (1.0f32/x) == infinity;
|
ret x > 0.0f32 || (1.0f32/x) == infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a zero number \
|
Returns true if `x` is a zero number (positive or negative zero)
|
||||||
(positive or negative zero)"
|
"]
|
||||||
)]
|
|
||||||
pure fn is_zero(x: f32) -> bool {
|
pure fn is_zero(x: f32) -> bool {
|
||||||
ret x == 0.0f32 || x == -0.0f32;
|
ret x == 0.0f32 || x == -0.0f32;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Returns true if `x`is an infinite number"]
|
||||||
brief = "Returns true if `x`is an infinite number"
|
|
||||||
)]
|
|
||||||
pure fn is_infinite(x: f32) -> bool {
|
pure fn is_infinite(x: f32) -> bool {
|
||||||
ret x == infinity || x == neg_infinity;
|
ret x == infinity || x == neg_infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Returns true if `x`is a finite number"]
|
||||||
brief = "Returns true if `x`is a finite number"
|
|
||||||
)]
|
|
||||||
pure fn is_finite(x: f32) -> bool {
|
pure fn is_finite(x: f32) -> bool {
|
||||||
ret !(is_NaN(x) || is_infinite(x));
|
ret !(is_NaN(x) || is_infinite(x));
|
||||||
}
|
}
|
||||||
|
@ -118,69 +111,43 @@ pure fn is_finite(x: f32) -> bool {
|
||||||
mod consts {
|
mod consts {
|
||||||
|
|
||||||
// FIXME replace with mathematical constants from cmath
|
// FIXME replace with mathematical constants from cmath
|
||||||
#[doc(
|
#[doc = "Archimedes' constant"]
|
||||||
brief = "Archimedes' constant"
|
|
||||||
)]
|
|
||||||
const pi: f32 = 3.14159265358979323846264338327950288_f32;
|
const pi: f32 = 3.14159265358979323846264338327950288_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "pi/2.0"]
|
||||||
brief = "pi/2.0"
|
|
||||||
)]
|
|
||||||
const frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
|
const frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "pi/4.0"]
|
||||||
brief = "pi/4.0"
|
|
||||||
)]
|
|
||||||
const frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
|
const frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "1.0/pi"]
|
||||||
brief = "1.0/pi"
|
|
||||||
)]
|
|
||||||
const frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
|
const frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "2.0/pi"]
|
||||||
brief = "2.0/pi"
|
|
||||||
)]
|
|
||||||
const frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
|
const frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "2.0/sqrt(pi)"]
|
||||||
brief = "2.0/sqrt(pi)"
|
|
||||||
)]
|
|
||||||
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
|
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "sqrt(2.0)"]
|
||||||
brief = "sqrt(2.0)"
|
|
||||||
)]
|
|
||||||
const sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
|
const sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "1.0/sqrt(2.0)"]
|
||||||
brief = "1.0/sqrt(2.0)"
|
|
||||||
)]
|
|
||||||
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
|
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Euler's number"]
|
||||||
brief = "Euler's number"
|
|
||||||
)]
|
|
||||||
const e: f32 = 2.71828182845904523536028747135266250_f32;
|
const e: f32 = 2.71828182845904523536028747135266250_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "log2(e)"]
|
||||||
brief = "log2(e)"
|
|
||||||
)]
|
|
||||||
const log2_e: f32 = 1.44269504088896340735992468100189214_f32;
|
const log2_e: f32 = 1.44269504088896340735992468100189214_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "log10(e)"]
|
||||||
brief = "log10(e)"
|
|
||||||
)]
|
|
||||||
const log10_e: f32 = 0.434294481903251827651128918916605082_f32;
|
const log10_e: f32 = 0.434294481903251827651128918916605082_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "ln(2.0)"]
|
||||||
brief = "ln(2.0)"
|
|
||||||
)]
|
|
||||||
const ln_2: f32 = 0.693147180559945309417232121458176568_f32;
|
const ln_2: f32 = 0.693147180559945309417232121458176568_f32;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "ln(10.0)"]
|
||||||
brief = "ln(10.0)"
|
|
||||||
)]
|
|
||||||
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
|
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,56 +76,47 @@ pure fn ge(x: f64, y: f64) -> bool { ret x >= y; }
|
||||||
|
|
||||||
pure fn gt(x: f64, y: f64) -> bool { ret x > y; }
|
pure fn gt(x: f64, y: f64) -> bool { ret x > y; }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a positive number, including \
|
Returns true if `x` is a positive number, including +0.0f640 and +Infinity.
|
||||||
+0.0f640 and +Infinity."
|
"]
|
||||||
)]
|
|
||||||
pure fn is_positive(x: f64) -> bool
|
pure fn is_positive(x: f64) -> bool
|
||||||
{ ret x > 0.0f64 || (1.0f64/x) == infinity; }
|
{ ret x > 0.0f64 || (1.0f64/x) == infinity; }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a negative number, including \
|
Returns true if `x` is a negative number, including -0.0f640 and -Infinity
|
||||||
-0.0f640 and -Infinity."
|
"]
|
||||||
)]
|
|
||||||
pure fn is_negative(x: f64) -> bool
|
pure fn is_negative(x: f64) -> bool
|
||||||
{ ret x < 0.0f64 || (1.0f64/x) == neg_infinity; }
|
{ ret x < 0.0f64 || (1.0f64/x) == neg_infinity; }
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a negative number, including \
|
Returns true if `x` is a negative number, including -0.0f640 and -Infinity
|
||||||
-0.0f640 and -Infinity. (This is the same as \
|
|
||||||
`f64::negative`.)"
|
This is the same as `f64::is_negative`.
|
||||||
)]
|
"]
|
||||||
pure fn is_nonpositive(x: f64) -> bool {
|
pure fn is_nonpositive(x: f64) -> bool {
|
||||||
ret x < 0.0f64 || (1.0f64/x) == neg_infinity;
|
ret x < 0.0f64 || (1.0f64/x) == neg_infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Returns true if `x` is a positive number, including \
|
Returns true if `x` is a positive number, including +0.0f640 and +Infinity
|
||||||
+0.0f640 and +Infinity.(This is the same as \
|
|
||||||
`f64::positive`.)"
|
This is the same as `f64::positive`.
|
||||||
)]
|
"]
|
||||||
pure fn is_nonnegative(x: f64) -> bool {
|
pure fn is_nonnegative(x: f64) -> bool {
|
||||||
ret x > 0.0f64 || (1.0f64/x) == infinity;
|
ret x > 0.0f64 || (1.0f64/x) == infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Returns true if `x` is a zero number (positive or negative zero)"]
|
||||||
brief = "Returns true if `x` is a zero number (positive or \
|
|
||||||
negative zero)"
|
|
||||||
)]
|
|
||||||
pure fn is_zero(x: f64) -> bool {
|
pure fn is_zero(x: f64) -> bool {
|
||||||
ret x == 0.0f64 || x == -0.0f64;
|
ret x == 0.0f64 || x == -0.0f64;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Returns true if `x`is an infinite number"]
|
||||||
brief = "Returns true if `x`is an infinite number."
|
|
||||||
)]
|
|
||||||
pure fn is_infinite(x: f64) -> bool {
|
pure fn is_infinite(x: f64) -> bool {
|
||||||
ret x == infinity || x == neg_infinity;
|
ret x == infinity || x == neg_infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Returns true if `x`is a finite number"]
|
||||||
brief = "Returns true if `x`is a finite number."
|
|
||||||
)]
|
|
||||||
pure fn is_finite(x: f64) -> bool {
|
pure fn is_finite(x: f64) -> bool {
|
||||||
ret !(is_NaN(x) || is_infinite(x));
|
ret !(is_NaN(x) || is_infinite(x));
|
||||||
}
|
}
|
||||||
|
@ -137,69 +128,43 @@ mod consts {
|
||||||
|
|
||||||
// FIXME replace with mathematical constants from cmath
|
// FIXME replace with mathematical constants from cmath
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Archimedes' constant"]
|
||||||
brief = "Archimedes' constant"
|
|
||||||
)]
|
|
||||||
const pi: f64 = 3.14159265358979323846264338327950288_f64;
|
const pi: f64 = 3.14159265358979323846264338327950288_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "pi/2.0"]
|
||||||
brief = "pi/2.0"
|
|
||||||
)]
|
|
||||||
const frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
|
const frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "pi/4.0"]
|
||||||
brief = "pi/4.0"
|
|
||||||
)]
|
|
||||||
const frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
|
const frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "1.0/pi"]
|
||||||
brief = "1.0/pi"
|
|
||||||
)]
|
|
||||||
const frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
|
const frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "2.0/pi"]
|
||||||
brief = "2.0/pi"
|
|
||||||
)]
|
|
||||||
const frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
|
const frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "2.0/sqrt(pi)"]
|
||||||
brief = "2.0/sqrt(pi)"
|
|
||||||
)]
|
|
||||||
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
|
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "sqrt(2.0)"]
|
||||||
brief = "sqrt(2.0)"
|
|
||||||
)]
|
|
||||||
const sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
|
const sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "1.0/sqrt(2.0)"]
|
||||||
brief = "1.0/sqrt(2.0)"
|
|
||||||
)]
|
|
||||||
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
|
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "Euler's number"]
|
||||||
brief = "Euler's number"
|
|
||||||
)]
|
|
||||||
const e: f64 = 2.71828182845904523536028747135266250_f64;
|
const e: f64 = 2.71828182845904523536028747135266250_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "log2(e)"]
|
||||||
brief = "log2(e)"
|
|
||||||
)]
|
|
||||||
const log2_e: f64 = 1.44269504088896340735992468100189214_f64;
|
const log2_e: f64 = 1.44269504088896340735992468100189214_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "log10(e)"]
|
||||||
brief = "log10(e)"
|
|
||||||
)]
|
|
||||||
const log10_e: f64 = 0.434294481903251827651128918916605082_f64;
|
const log10_e: f64 = 0.434294481903251827651128918916605082_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "ln(2.0)"]
|
||||||
brief = "ln(2.0)"
|
|
||||||
)]
|
|
||||||
const ln_2: f64 = 0.693147180559945309417232121458176568_f64;
|
const ln_2: f64 = 0.693147180559945309417232121458176568_f64;
|
||||||
|
|
||||||
#[doc(
|
#[doc = "ln(10.0)"]
|
||||||
brief = "ln(10.0)"
|
|
||||||
)]
|
|
||||||
const ln_10: f64 = 2.30258509299404568401799145468436421_f64;
|
const ln_10: f64 = 2.30258509299404568401799145468436421_f64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
#[doc = "Operations and constants for `float`"];
|
#[doc = "Operations and constants for `float`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: float
|
|
||||||
*/
|
|
||||||
|
|
||||||
// FIXME find out why these have to be exported explicitly
|
// FIXME find out why these have to be exported explicitly
|
||||||
|
|
||||||
export to_str_common, to_str_exact, to_str, from_str;
|
export to_str_common, to_str_exact, to_str, from_str;
|
||||||
|
@ -36,17 +32,15 @@ type t = float;
|
||||||
* Section: String Conversions
|
* Section: String Conversions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: to_str_common
|
|
||||||
|
|
||||||
Converts a float to a string
|
Converts a float to a string
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
|
|
||||||
num - The float value
|
* num - The float value
|
||||||
digits - The number of significant digits
|
* digits - The number of significant digits
|
||||||
exact - Whether to enforce the exact number of significant digits
|
* exact - Whether to enforce the exact number of significant digits
|
||||||
*/
|
"]
|
||||||
fn to_str_common(num: float, digits: uint, exact: bool) -> str {
|
fn to_str_common(num: float, digits: uint, exact: bool) -> str {
|
||||||
if is_NaN(num) { ret "NaN"; }
|
if is_NaN(num) { ret "NaN"; }
|
||||||
let mut (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
|
let mut (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
|
||||||
|
@ -69,17 +63,15 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> str {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: to_str
|
Converts a float to a string with exactly the number of
|
||||||
|
provided significant digits
|
||||||
|
|
||||||
Converts a float to a string with exactly the number of provided significant
|
# Arguments
|
||||||
digits
|
|
||||||
|
|
||||||
Parameters:
|
* num - The float value
|
||||||
|
* digits - The number of significant digits
|
||||||
num - The float value
|
"]
|
||||||
digits - The number of significant digits
|
|
||||||
*/
|
|
||||||
fn to_str_exact(num: float, digits: uint) -> str {
|
fn to_str_exact(num: float, digits: uint) -> str {
|
||||||
to_str_common(num, digits, true)
|
to_str_common(num, digits, true)
|
||||||
}
|
}
|
||||||
|
@ -90,47 +82,45 @@ fn test_to_str_exact_do_decimal() {
|
||||||
assert s == "5.0000";
|
assert s == "5.0000";
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: to_str
|
|
||||||
|
|
||||||
Converts a float to a string with a maximum number of significant digits
|
#[doc = "
|
||||||
|
Converts a float to a string with a maximum number of
|
||||||
|
significant digits
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
|
|
||||||
num - The float value
|
* num - The float value
|
||||||
digits - The number of significant digits
|
* digits - The number of significant digits
|
||||||
*/
|
"]
|
||||||
fn to_str(num: float, digits: uint) -> str {
|
fn to_str(num: float, digits: uint) -> str {
|
||||||
to_str_common(num, digits, false)
|
to_str_common(num, digits, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: from_str
|
|
||||||
|
|
||||||
Convert a string to a float
|
Convert a string to a float
|
||||||
|
|
||||||
This function accepts strings such as
|
This function accepts strings such as
|
||||||
* "3.14"
|
|
||||||
* "+3.14", equivalent to "3.14"
|
* '3.14'
|
||||||
* "-3.14"
|
* '+3.14', equivalent to '3.14'
|
||||||
* "2.5E10", or equivalently, "2.5e10"
|
* '-3.14'
|
||||||
* "2.5E-10"
|
* '2.5E10', or equivalently, '2.5e10'
|
||||||
* "", or, equivalently, "." (understood as 0)
|
* '2.5E-10'
|
||||||
* "5."
|
* '', or, equivalently, '.' (understood as 0)
|
||||||
* ".5", or, equivalently, "0.5"
|
* '5.'
|
||||||
|
* '.5', or, equivalently, '0.5'
|
||||||
|
|
||||||
Leading and trailing whitespace are ignored.
|
Leading and trailing whitespace are ignored.
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
|
|
||||||
num - A string
|
* num - A string
|
||||||
|
|
||||||
Returns:
|
# Return value
|
||||||
|
|
||||||
none if the string did not represent a valid number.
|
`none` if the string did not represent a valid number. Otherwise, `some(n)`
|
||||||
Otherwise, some(n) where n is the floating-point
|
where `n` is the floating-point number represented by `[num]`.
|
||||||
number represented by [num].
|
"]
|
||||||
*/
|
|
||||||
fn from_str(num: str) -> option<float> {
|
fn from_str(num: str) -> option<float> {
|
||||||
let mut pos = 0u; //Current byte position in the string.
|
let mut pos = 0u; //Current byte position in the string.
|
||||||
//Used to walk the string in O(n).
|
//Used to walk the string in O(n).
|
||||||
|
@ -256,18 +246,18 @@ fn from_str(num: str) -> option<float> {
|
||||||
* Section: Arithmetics
|
* Section: Arithmetics
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: pow_with_uint
|
Compute the exponentiation of an integer by another integer as a float
|
||||||
|
|
||||||
Compute the exponentiation of an integer by another integer as a float.
|
# Arguments
|
||||||
|
|
||||||
Parameters:
|
* x - The base
|
||||||
x - The base.
|
* pow - The exponent
|
||||||
pow - The exponent.
|
|
||||||
|
|
||||||
Returns:
|
# Return value
|
||||||
<NaN> of both `x` and `pow` are `0u`, otherwise `x^pow`.
|
|
||||||
*/
|
`NaN` if both `x` and `pow` are `0u`, otherwise `x^pow`
|
||||||
|
"]
|
||||||
fn pow_with_uint(base: uint, pow: uint) -> float {
|
fn pow_with_uint(base: uint, pow: uint) -> float {
|
||||||
if base == 0u {
|
if base == 0u {
|
||||||
if pow == 0u {
|
if pow == 0u {
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
A type representing values that may be computed concurrently and
|
A type representing values that may be computed concurrently and
|
||||||
operations for working with them.
|
operations for working with them.
|
||||||
|
|
||||||
Example:
|
# Example
|
||||||
|
|
||||||
> let delayed_fib = future::spawn {|| fib(5000) };
|
|
||||||
> make_a_sandwitch();
|
|
||||||
> io::println(#fmt(\"fib(5000) = %?\", delayed_fib.get()))
|
|
||||||
|
|
||||||
|
~~~
|
||||||
|
let delayed_fib = future::spawn {|| fib(5000) };
|
||||||
|
make_a_sandwitch();
|
||||||
|
io::println(#fmt(\"fib(5000) = %?\", delayed_fib.get()))
|
||||||
|
~~~
|
||||||
"];
|
"];
|
||||||
|
|
||||||
export future;
|
export future;
|
||||||
|
@ -45,10 +45,10 @@ impl future<A:send> for future<A> {
|
||||||
|
|
||||||
fn from_value<A>(+val: A) -> future<A> {
|
fn from_value<A>(+val: A) -> future<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
Create a future from a value
|
||||||
|
|
||||||
Create a future from a value. The value is immediately available
|
The value is immediately available and calling `get` later will
|
||||||
and calling `get` later will not block.
|
not block.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
future({
|
future({
|
||||||
|
@ -58,11 +58,10 @@ fn from_value<A>(+val: A) -> future<A> {
|
||||||
|
|
||||||
fn from_port<A:send>(-port: comm::port<A>) -> future<A> {
|
fn from_port<A:send>(-port: comm::port<A>) -> future<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
Create a future from a port
|
||||||
|
|
||||||
Create a future from a port. The first time that the value is
|
The first time that the value is requested the task will block
|
||||||
requested the task will block waiting for the result to be
|
waiting for the result to be received on the port.
|
||||||
received on the port.
|
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
from_fn {||
|
from_fn {||
|
||||||
|
@ -72,12 +71,11 @@ fn from_port<A:send>(-port: comm::port<A>) -> future<A> {
|
||||||
|
|
||||||
fn from_fn<A>(f: fn@() -> A) -> future<A> {
|
fn from_fn<A>(f: fn@() -> A) -> future<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
Create a future from a function.
|
||||||
|
|
||||||
Create a future from a function. The first time that the value is
|
The first time that the value is requested it will be retreived by
|
||||||
requested it will be retreived by calling the function.
|
calling the function. Note that this function is a local
|
||||||
|
function. It is not spawned into another task.
|
||||||
Note that this function is a local function. It is not spawned into
|
|
||||||
another task.
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
future({
|
future({
|
||||||
|
@ -87,10 +85,10 @@ fn from_fn<A>(f: fn@() -> A) -> future<A> {
|
||||||
|
|
||||||
fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
|
fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
Create a future from a unique closure.
|
||||||
|
|
||||||
Create a future from a unique closure. The closure will be run
|
The closure will be run in a new task and its result used as the
|
||||||
in a new task and its result used as the value of the future.
|
value of the future.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
let mut po = comm::port();
|
let mut po = comm::port();
|
||||||
|
|
|
@ -1,25 +1,11 @@
|
||||||
#[doc = "Operations and constants for `int`"];
|
#[doc = "Operations and constants for `int`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: int
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Const: min_value
|
|
||||||
|
|
||||||
The minumum value of an integer
|
|
||||||
*/
|
|
||||||
#[cfg(target_arch="x86")]
|
#[cfg(target_arch="x86")]
|
||||||
const min_value: int = -1 << 31;
|
const min_value: int = -1 << 31;
|
||||||
|
|
||||||
#[cfg(target_arch="x86_64")]
|
#[cfg(target_arch="x86_64")]
|
||||||
const min_value: int = -1 << 63;
|
const min_value: int = -1 << 63;
|
||||||
|
|
||||||
/*
|
|
||||||
Const: max_value
|
|
||||||
|
|
||||||
The maximum value of an integer
|
|
||||||
*/
|
|
||||||
// FIXME: Find another way to access the machine word size in a const expr
|
// FIXME: Find another way to access the machine word size in a const expr
|
||||||
#[cfg(target_arch="x86")]
|
#[cfg(target_arch="x86")]
|
||||||
const max_value: int = (-1 << 31)-1;
|
const max_value: int = (-1 << 31)-1;
|
||||||
|
@ -30,80 +16,43 @@ const max_value: int = (-1 << 63)-1;
|
||||||
pure fn min(x: int, y: int) -> int { if x < y { x } else { y } }
|
pure fn min(x: int, y: int) -> int { if x < y { x } else { y } }
|
||||||
pure fn max(x: int, y: int) -> int { if x > y { x } else { y } }
|
pure fn max(x: int, y: int) -> int { if x > y { x } else { y } }
|
||||||
|
|
||||||
/* Function: add */
|
|
||||||
pure fn add(x: int, y: int) -> int { ret x + y; }
|
pure fn add(x: int, y: int) -> int { ret x + y; }
|
||||||
|
|
||||||
/* Function: sub */
|
|
||||||
pure fn sub(x: int, y: int) -> int { ret x - y; }
|
pure fn sub(x: int, y: int) -> int { ret x - y; }
|
||||||
|
|
||||||
/* Function: mul */
|
|
||||||
pure fn mul(x: int, y: int) -> int { ret x * y; }
|
pure fn mul(x: int, y: int) -> int { ret x * y; }
|
||||||
|
|
||||||
/* Function: div */
|
|
||||||
pure fn div(x: int, y: int) -> int { ret x / y; }
|
pure fn div(x: int, y: int) -> int { ret x / y; }
|
||||||
|
|
||||||
/* Function: rem */
|
|
||||||
pure fn rem(x: int, y: int) -> int { ret x % y; }
|
pure fn rem(x: int, y: int) -> int { ret x % y; }
|
||||||
|
|
||||||
/* Predicate: lt */
|
|
||||||
pure fn lt(x: int, y: int) -> bool { ret x < y; }
|
pure fn lt(x: int, y: int) -> bool { ret x < y; }
|
||||||
|
|
||||||
/* Predicate: le */
|
|
||||||
pure fn le(x: int, y: int) -> bool { ret x <= y; }
|
pure fn le(x: int, y: int) -> bool { ret x <= y; }
|
||||||
|
|
||||||
/* Predicate: eq */
|
|
||||||
pure fn eq(x: int, y: int) -> bool { ret x == y; }
|
pure fn eq(x: int, y: int) -> bool { ret x == y; }
|
||||||
|
|
||||||
/* Predicate: ne */
|
|
||||||
pure fn ne(x: int, y: int) -> bool { ret x != y; }
|
pure fn ne(x: int, y: int) -> bool { ret x != y; }
|
||||||
|
|
||||||
/* Predicate: ge */
|
|
||||||
pure fn ge(x: int, y: int) -> bool { ret x >= y; }
|
pure fn ge(x: int, y: int) -> bool { ret x >= y; }
|
||||||
|
|
||||||
/* Predicate: gt */
|
|
||||||
pure fn gt(x: int, y: int) -> bool { ret x > y; }
|
pure fn gt(x: int, y: int) -> bool { ret x > y; }
|
||||||
|
|
||||||
/* Predicate: positive */
|
|
||||||
pure fn positive(x: int) -> bool { ret x > 0; }
|
pure fn positive(x: int) -> bool { ret x > 0; }
|
||||||
|
|
||||||
/* Predicate: negative */
|
|
||||||
pure fn negative(x: int) -> bool { ret x < 0; }
|
pure fn negative(x: int) -> bool { ret x < 0; }
|
||||||
|
|
||||||
/* Predicate: nonpositive */
|
|
||||||
pure fn nonpositive(x: int) -> bool { ret x <= 0; }
|
pure fn nonpositive(x: int) -> bool { ret x <= 0; }
|
||||||
|
|
||||||
/* Predicate: nonnegative */
|
|
||||||
pure fn nonnegative(x: int) -> bool { ret x >= 0; }
|
pure fn nonnegative(x: int) -> bool { ret x >= 0; }
|
||||||
|
|
||||||
|
|
||||||
// FIXME: Make sure this works with negative integers.
|
// FIXME: Make sure this works with negative integers.
|
||||||
/*
|
#[doc = "Produce a uint suitable for use in a hash table"]
|
||||||
Function: hash
|
|
||||||
|
|
||||||
Produce a uint suitable for use in a hash table
|
|
||||||
*/
|
|
||||||
fn hash(x: int) -> uint { ret x as uint; }
|
fn hash(x: int) -> uint { ret x as uint; }
|
||||||
|
|
||||||
/*
|
#[doc = "Iterate over the range `[lo..hi)`"]
|
||||||
Function: range
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`)
|
|
||||||
*/
|
|
||||||
fn range(lo: int, hi: int, it: fn(int)) {
|
fn range(lo: int, hi: int, it: fn(int)) {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi { it(i); i += 1; }
|
while i < hi { it(i); i += 1; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: parse_buf
|
|
||||||
|
|
||||||
Parse a buffer of bytes
|
Parse a buffer of bytes
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
|
|
||||||
buf - A byte buffer
|
* buf - A byte buffer
|
||||||
radix - The base of the number
|
* radix - The base of the number
|
||||||
*/
|
"]
|
||||||
fn parse_buf(buf: [u8], radix: uint) -> option<int> {
|
fn parse_buf(buf: [u8], radix: uint) -> option<int> {
|
||||||
if vec::len(buf) == 0u { ret none; }
|
if vec::len(buf) == 0u { ret none; }
|
||||||
let mut i = vec::len(buf) - 1u;
|
let mut i = vec::len(buf) - 1u;
|
||||||
|
@ -127,18 +76,10 @@ fn parse_buf(buf: [u8], radix: uint) -> option<int> {
|
||||||
fail;
|
fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Parse a string to an int"]
|
||||||
Function: from_str
|
|
||||||
|
|
||||||
Parse a string to an int
|
|
||||||
*/
|
|
||||||
fn from_str(s: str) -> option<int> { parse_buf(str::bytes(s), 10u) }
|
fn from_str(s: str) -> option<int> { parse_buf(str::bytes(s), 10u) }
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string in a given base"]
|
||||||
Function: to_str
|
|
||||||
|
|
||||||
Convert to a string in a given base
|
|
||||||
*/
|
|
||||||
fn to_str(n: int, radix: uint) -> str {
|
fn to_str(n: int, radix: uint) -> str {
|
||||||
assert (0u < radix && radix <= 16u);
|
assert (0u < radix && radix <= 16u);
|
||||||
ret if n < 0 {
|
ret if n < 0 {
|
||||||
|
@ -146,18 +87,10 @@ fn to_str(n: int, radix: uint) -> str {
|
||||||
} else { uint::to_str(n as uint, radix) };
|
} else { uint::to_str(n as uint, radix) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string"]
|
||||||
Function: str
|
|
||||||
|
|
||||||
Convert to a string
|
|
||||||
*/
|
|
||||||
fn str(i: int) -> str { ret to_str(i, 10u); }
|
fn str(i: int) -> str { ret to_str(i, 10u); }
|
||||||
|
|
||||||
/*
|
#[doc = "Returns `base` raised to the power of `exponent`"]
|
||||||
Function: pow
|
|
||||||
|
|
||||||
Returns `base` raised to the power of `exponent`
|
|
||||||
*/
|
|
||||||
fn pow(base: int, exponent: uint) -> int {
|
fn pow(base: int, exponent: uint) -> int {
|
||||||
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
|
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
|
||||||
if base == 0 { ret 0; }
|
if base == 0 { ret 0; }
|
||||||
|
|
|
@ -13,14 +13,13 @@ fn console_on() {
|
||||||
rustrt::rust_log_console_on();
|
rustrt::rust_log_console_on();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief =
|
Turns off logging to stdout globally
|
||||||
"Turns off logging to stdout globally",
|
|
||||||
desc =
|
Turns off the console unless the user has overridden the
|
||||||
"Turns off the console unless the user has overridden the \
|
runtime environment's logging spec, e.g. by setting
|
||||||
runtime environment's logging spec, e.g. by setting \
|
the RUST_LOG environment variable
|
||||||
the RUST_LOG environment variable"
|
"]
|
||||||
)]
|
|
||||||
fn console_off() {
|
fn console_off() {
|
||||||
rustrt::rust_log_console_off();
|
rustrt::rust_log_console_off();
|
||||||
}
|
}
|
|
@ -1,19 +1,13 @@
|
||||||
// Generic functions that have been defined for all numeric types
|
#[doc = "
|
||||||
//
|
Generic functions that have been defined for all numeric types
|
||||||
// (may very well go away again soon)
|
|
||||||
|
|
||||||
/*
|
(may very well go away again soon)
|
||||||
Function: min
|
"];
|
||||||
|
|
||||||
Returns the minimum of two values
|
#[doc = "Returns the minimum of two values"]
|
||||||
*/
|
|
||||||
pure fn min<T: copy>(x: T, y: T) -> T { if x < y { x } else { y} }
|
pure fn min<T: copy>(x: T, y: T) -> T { if x < y { x } else { y} }
|
||||||
|
|
||||||
/*
|
#[doc = "Returns the maximum of two values"]
|
||||||
Function: max
|
|
||||||
|
|
||||||
Returns the maximum of two values
|
|
||||||
*/
|
|
||||||
pure fn max<T: copy>(x: T, y: T) -> T { if x < y { y } else { x } }
|
pure fn max<T: copy>(x: T, y: T) -> T { if x < y { y } else { x } }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,107 +1,83 @@
|
||||||
/*
|
#[doc = "
|
||||||
Module: option
|
Operations on the ubiquitous `option` type.
|
||||||
|
|
||||||
Represents the presence or absence of a value.
|
Type `option` represents an optional value.
|
||||||
|
|
||||||
Every option<T> value can either be some(T) or none. Where in other languages
|
Every `option<T>` value can either be `some(T)` or `none`. Where in other
|
||||||
you might use a nullable type, in Rust you would use an option type.
|
languages you might use a nullable type, in Rust you would use an option type.
|
||||||
*/
|
"];
|
||||||
|
|
||||||
/*
|
#[doc = "The option type"]
|
||||||
Tag: t
|
|
||||||
|
|
||||||
The option type
|
|
||||||
*/
|
|
||||||
enum t<T> {
|
enum t<T> {
|
||||||
/* Variant: none */
|
|
||||||
none,
|
none,
|
||||||
/* Variant: some */
|
|
||||||
some(T),
|
some(T),
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Section: Operations */
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: get
|
|
||||||
|
|
||||||
Gets the value out of an option
|
|
||||||
|
|
||||||
Failure:
|
|
||||||
|
|
||||||
Fails if the value equals `none`.
|
|
||||||
*/
|
|
||||||
pure fn get<T: copy>(opt: t<T>) -> T {
|
pure fn get<T: copy>(opt: t<T>) -> T {
|
||||||
|
#[doc = "
|
||||||
|
Gets the value out of an option
|
||||||
|
|
||||||
|
# Failure
|
||||||
|
|
||||||
|
Fails if the value equals `none`
|
||||||
|
"];
|
||||||
|
|
||||||
alt opt { some(x) { ret x; } none { fail "option none"; } }
|
alt opt { some(x) { ret x; } none { fail "option none"; } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
*/
|
|
||||||
fn map<T, U: copy>(opt: t<T>, f: fn(T) -> U) -> t<U> {
|
fn map<T, U: copy>(opt: t<T>, f: fn(T) -> U) -> t<U> {
|
||||||
|
#[doc = "Maps a `some` value from one type to another"];
|
||||||
|
|
||||||
alt opt { some(x) { some(f(x)) } none { none } }
|
alt opt { some(x) { some(f(x)) } none { none } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: chain
|
|
||||||
|
|
||||||
Update an optional value by optionally running its content through a function
|
|
||||||
that returns an option.
|
|
||||||
*/
|
|
||||||
fn chain<T, U>(opt: t<T>, f: fn(T) -> t<U>) -> t<U> {
|
fn chain<T, U>(opt: t<T>, f: fn(T) -> t<U>) -> t<U> {
|
||||||
|
#[doc = "
|
||||||
|
Update an optional value by optionally running its content through a
|
||||||
|
function that returns an option.
|
||||||
|
"];
|
||||||
|
|
||||||
alt opt { some(x) { f(x) } none { none } }
|
alt opt { some(x) { f(x) } none { none } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: is_none
|
|
||||||
|
|
||||||
Returns true if the option equals none
|
|
||||||
*/
|
|
||||||
pure fn is_none<T>(opt: t<T>) -> bool {
|
pure fn is_none<T>(opt: t<T>) -> bool {
|
||||||
|
#[doc = "Returns true if the option equals `none`"];
|
||||||
|
|
||||||
alt opt { none { true } some(_) { false } }
|
alt opt { none { true } some(_) { false } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
pure fn is_some<T>(opt: t<T>) -> bool {
|
||||||
Function: is_some
|
#[doc = "Returns true if the option contains some value"];
|
||||||
|
|
||||||
Returns true if the option contains some value
|
!is_none(opt)
|
||||||
*/
|
}
|
||||||
pure fn is_some<T>(opt: t<T>) -> bool { !is_none(opt) }
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: from_maybe
|
|
||||||
|
|
||||||
Returns the contained value or a default
|
|
||||||
*/
|
|
||||||
pure fn from_maybe<T: copy>(def: T, opt: t<T>) -> T {
|
pure fn from_maybe<T: copy>(def: T, opt: t<T>) -> T {
|
||||||
|
#[doc = "Returns the contained value or a default"];
|
||||||
|
|
||||||
alt opt { some(x) { x } none { def } }
|
alt opt { some(x) { x } none { def } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: maybe
|
|
||||||
|
|
||||||
Applies a function to the contained value or returns a default
|
|
||||||
*/
|
|
||||||
fn maybe<T, U: copy>(def: U, opt: t<T>, f: fn(T) -> U) -> U {
|
fn maybe<T, U: copy>(def: U, opt: t<T>, f: fn(T) -> U) -> U {
|
||||||
|
#[doc = "Applies a function to the contained value or returns a default"];
|
||||||
|
|
||||||
alt opt { none { def } some(t) { f(t) } }
|
alt opt { none { def } some(t) { f(t) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Can be defined in terms of the above when/if we have const bind.
|
|
||||||
/*
|
|
||||||
Function: may
|
|
||||||
|
|
||||||
Performs an operation on the contained value or does nothing
|
|
||||||
*/
|
|
||||||
fn may<T>(opt: t<T>, f: fn(T)) {
|
fn may<T>(opt: t<T>, f: fn(T)) {
|
||||||
alt opt { none {/* nothing */ } some(t) { f(t); } }
|
#[doc = "Performs an operation on the contained value or does nothing"];
|
||||||
|
|
||||||
|
alt opt { none { } some(t) { f(t); } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Function: unwrap
|
|
||||||
|
|
||||||
Moves a value out of an option type and returns it. Useful primarily
|
|
||||||
for getting strings, vectors and unique pointers out of option types
|
|
||||||
without copying them.
|
|
||||||
*/
|
|
||||||
fn unwrap<T>(-opt: t<T>) -> T unsafe {
|
fn unwrap<T>(-opt: t<T>) -> T unsafe {
|
||||||
|
#[doc = "
|
||||||
|
Moves a value out of an option type and returns it.
|
||||||
|
|
||||||
|
Useful primarily for getting strings, vectors and unique pointers out of
|
||||||
|
option types without copying them.
|
||||||
|
"];
|
||||||
|
|
||||||
let addr = alt opt {
|
let addr = alt opt {
|
||||||
some(x) { ptr::addr_of(x) }
|
some(x) { ptr::addr_of(x) }
|
||||||
none { fail "option none" }
|
none { fail "option none" }
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
// Higher-level interfaces to libc::* functions and operating system services.
|
#[doc = "
|
||||||
//
|
Higher-level interfaces to libc::* functions and operating system services.
|
||||||
// In general these take and return rust types, use rust idioms (enums,
|
|
||||||
// closures, vectors) rather than C idioms, and do more extensive safety
|
In general these take and return rust types, use rust idioms (enums,
|
||||||
// checks.
|
closures, vectors) rather than C idioms, and do more extensive safety
|
||||||
//
|
checks.
|
||||||
// This module is not meant to only contain 1:1 mappings to libc entries; any
|
|
||||||
// os-interface code that is reasonably useful and broadly applicable can go
|
This module is not meant to only contain 1:1 mappings to libc entries; any
|
||||||
// here. Including utility routines that merely build on other os code.
|
os-interface code that is reasonably useful and broadly applicable can go
|
||||||
//
|
here. Including utility routines that merely build on other os code.
|
||||||
// We assume the general case is that users do not care, and do not want to
|
|
||||||
// be made to care, which operating system they are on. While they may want
|
We assume the general case is that users do not care, and do not want to
|
||||||
// to special case various special cases -- and so we will not _hide_ the
|
be made to care, which operating system they are on. While they may want
|
||||||
// facts of which OS the user is on -- they should be given the opportunity
|
to special case various special cases -- and so we will not _hide_ the
|
||||||
// to write OS-ignorant code by default.
|
facts of which OS the user is on -- they should be given the opportunity
|
||||||
|
to write OS-ignorant code by default.
|
||||||
|
"];
|
||||||
|
|
||||||
import libc::{c_char, c_void, c_int, c_uint, size_t, ssize_t,
|
import libc::{c_char, c_void, c_int, c_uint, size_t, ssize_t,
|
||||||
mode_t, pid_t, FILE};
|
mode_t, pid_t, FILE};
|
||||||
|
@ -327,21 +329,19 @@ fn self_exe_path() -> option<path> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: homedir
|
|
||||||
|
|
||||||
Returns the path to the user's home directory, if known.
|
Returns the path to the user's home directory, if known.
|
||||||
|
|
||||||
On Unix, returns the value of the "HOME" environment variable if it is set and
|
On Unix, returns the value of the 'HOME' environment variable if it is set and
|
||||||
not equal to the empty string.
|
not equal to the empty string.
|
||||||
|
|
||||||
On Windows, returns the value of the "HOME" environment variable if it is set
|
On Windows, returns the value of the 'HOME' environment variable if it is set
|
||||||
and not equal to the empty string. Otherwise, returns the value of the
|
and not equal to the empty string. Otherwise, returns the value of the
|
||||||
"USERPROFILE" environment variable if it is set and not equal to the empty
|
'USERPROFILE' environment variable if it is set and not equal to the empty
|
||||||
string.
|
string.
|
||||||
|
|
||||||
Otherwise, homedir returns option::none.
|
Otherwise, homedir returns option::none.
|
||||||
*/
|
"]
|
||||||
fn homedir() -> option<path> {
|
fn homedir() -> option<path> {
|
||||||
ret alt getenv("HOME") {
|
ret alt getenv("HOME") {
|
||||||
some(p) {
|
some(p) {
|
||||||
|
@ -377,22 +377,14 @@ fn homedir() -> option<path> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
#[doc = "Indicates whether a path represents a directory"]
|
||||||
Function: path_is_dir
|
|
||||||
|
|
||||||
Indicates whether a path represents a directory.
|
|
||||||
*/
|
|
||||||
fn path_is_dir(p: path) -> bool {
|
fn path_is_dir(p: path) -> bool {
|
||||||
str::as_buf(p) {|buf|
|
str::as_buf(p) {|buf|
|
||||||
rustrt::rust_path_is_dir(buf) != 0 as c_int
|
rustrt::rust_path_is_dir(buf) != 0 as c_int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Indicates whether a path exists"]
|
||||||
Function: path_exists
|
|
||||||
|
|
||||||
Indicates whether a path exists.
|
|
||||||
*/
|
|
||||||
fn path_exists(p: path) -> bool {
|
fn path_exists(p: path) -> bool {
|
||||||
str::as_buf(p) {|buf|
|
str::as_buf(p) {|buf|
|
||||||
rustrt::rust_path_exists(buf) != 0 as c_int
|
rustrt::rust_path_exists(buf) != 0 as c_int
|
||||||
|
@ -401,15 +393,13 @@ fn path_exists(p: path) -> bool {
|
||||||
|
|
||||||
// FIXME: under Windows, we should prepend the current drive letter to paths
|
// FIXME: under Windows, we should prepend the current drive letter to paths
|
||||||
// that start with a slash.
|
// that start with a slash.
|
||||||
/*
|
#[doc = "
|
||||||
Function: make_absolute
|
|
||||||
|
|
||||||
Convert a relative path to an absolute path
|
Convert a relative path to an absolute path
|
||||||
|
|
||||||
If the given path is relative, return it prepended with the current working
|
If the given path is relative, return it prepended with the current working
|
||||||
directory. If the given path is already an absolute path, return it
|
directory. If the given path is already an absolute path, return it
|
||||||
as is.
|
as is.
|
||||||
*/
|
"]
|
||||||
// NB: this is here rather than in path because it is a form of environment
|
// NB: this is here rather than in path because it is a form of environment
|
||||||
// querying; what it does depends on the process working directory, not just
|
// querying; what it does depends on the process working directory, not just
|
||||||
// the input paths.
|
// the input paths.
|
||||||
|
@ -422,11 +412,7 @@ fn make_absolute(p: path) -> path {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
#[doc = "Creates a directory at the specified path"]
|
||||||
Function: make_dir
|
|
||||||
|
|
||||||
Creates a directory at the specified path.
|
|
||||||
*/
|
|
||||||
fn make_dir(p: path, mode: c_int) -> bool {
|
fn make_dir(p: path, mode: c_int) -> bool {
|
||||||
ret mkdir(p, mode);
|
ret mkdir(p, mode);
|
||||||
|
|
||||||
|
@ -453,11 +439,7 @@ fn make_dir(p: path, mode: c_int) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Lists the contents of a directory"]
|
||||||
Function: list_dir
|
|
||||||
|
|
||||||
Lists the contents of a directory.
|
|
||||||
*/
|
|
||||||
fn list_dir(p: path) -> [str] {
|
fn list_dir(p: path) -> [str] {
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
|
@ -485,11 +467,7 @@ fn list_dir(p: path) -> [str] {
|
||||||
ret full_paths;
|
ret full_paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Removes a directory at the specified path"]
|
||||||
Function: remove_dir
|
|
||||||
|
|
||||||
Removes a directory at the specified path.
|
|
||||||
*/
|
|
||||||
fn remove_dir(p: path) -> bool {
|
fn remove_dir(p: path) -> bool {
|
||||||
ret rmdir(p);
|
ret rmdir(p);
|
||||||
|
|
||||||
|
@ -538,11 +516,7 @@ fn change_dir(p: path) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Deletes an existing file"]
|
||||||
Function: remove_file
|
|
||||||
|
|
||||||
Deletes an existing file.
|
|
||||||
*/
|
|
||||||
fn remove_file(p: path) -> bool {
|
fn remove_file(p: path) -> bool {
|
||||||
ret unlink(p);
|
ret unlink(p);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
/*
|
#[doc = "Path data type and helper functions"]
|
||||||
Module: path
|
|
||||||
|
|
||||||
Path data type and helper functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
#[cfg(target_os = "freebsd")]
|
#[cfg(target_os = "freebsd")]
|
||||||
|
@ -18,14 +14,12 @@ mod consts {
|
||||||
const alt_path_sep: char = '\\';
|
const alt_path_sep: char = '\\';
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: path_is_absolute
|
|
||||||
|
|
||||||
Indicates whether a path is absolute.
|
Indicates whether a path is absolute.
|
||||||
|
|
||||||
A path is considered absolute if it begins at the filesystem root ("/") or,
|
A path is considered absolute if it begins at the filesystem root (\"/\") or,
|
||||||
on Windows, begins with a drive letter.
|
on Windows, begins with a drive letter.
|
||||||
*/
|
"]
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
#[cfg(target_os = "freebsd")]
|
#[cfg(target_os = "freebsd")]
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
|
@ -43,19 +37,11 @@ fn path_is_absolute(p: str) -> bool {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
#[doc = "Get the default path separator for the host platform"]
|
||||||
Function: path_sep
|
|
||||||
|
|
||||||
Get the default path separator for the host platform
|
|
||||||
*/
|
|
||||||
fn path_sep() -> str { ret str::from_char(consts::path_sep); }
|
fn path_sep() -> str { ret str::from_char(consts::path_sep); }
|
||||||
|
|
||||||
// FIXME: This type should probably be constrained
|
// FIXME: This type should probably be constrained
|
||||||
/*
|
#[doc = "A path or fragment of a filesystem path"]
|
||||||
Type: path
|
|
||||||
|
|
||||||
A path or fragment of a filesystem path
|
|
||||||
*/
|
|
||||||
type path = str;
|
type path = str;
|
||||||
|
|
||||||
fn split_dirname_basename (pp: path) -> {dirname: str, basename: str} {
|
fn split_dirname_basename (pp: path) -> {dirname: str, basename: str} {
|
||||||
|
@ -70,47 +56,40 @@ fn split_dirname_basename (pp: path) -> {dirname: str, basename: str} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: dirname
|
|
||||||
|
|
||||||
Get the directory portion of a path
|
Get the directory portion of a path
|
||||||
|
|
||||||
Returns all of the path up to, but excluding, the final path separator.
|
Returns all of the path up to, but excluding, the final path separator.
|
||||||
The dirname of "/usr/share" will be "/usr", but the dirname of
|
The dirname of \"/usr/share\" will be \"/usr\", but the dirname of
|
||||||
"/usr/share/" is "/usr/share".
|
\"/usr/share/\" is \"/usr/share\".
|
||||||
|
|
||||||
If the path is not prefixed with a directory, then "." is returned.
|
If the path is not prefixed with a directory, then \".\" is returned.
|
||||||
*/
|
"]
|
||||||
fn dirname(pp: path) -> path {
|
fn dirname(pp: path) -> path {
|
||||||
ret split_dirname_basename(pp).dirname;
|
ret split_dirname_basename(pp).dirname;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: basename
|
|
||||||
|
|
||||||
Get the file name portion of a path
|
Get the file name portion of a path
|
||||||
|
|
||||||
Returns the portion of the path after the final path separator.
|
Returns the portion of the path after the final path separator.
|
||||||
The basename of "/usr/share" will be "share". If there are no
|
The basename of \"/usr/share\" will be \"share\". If there are no
|
||||||
path separators in the path then the returned path is identical to
|
path separators in the path then the returned path is identical to
|
||||||
the provided path. If an empty path is provided or the path ends
|
the provided path. If an empty path is provided or the path ends
|
||||||
with a path separator then an empty path is returned.
|
with a path separator then an empty path is returned.
|
||||||
*/
|
"]
|
||||||
fn basename(pp: path) -> path {
|
fn basename(pp: path) -> path {
|
||||||
ret split_dirname_basename(pp).basename;
|
ret split_dirname_basename(pp).basename;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
|
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
|
||||||
/*
|
#[doc = "
|
||||||
Function: connect
|
|
||||||
|
|
||||||
Connects to path segments
|
Connects to path segments
|
||||||
|
|
||||||
Given paths `pre` and `post, removes any trailing path separator on `pre` and
|
Given paths `pre` and `post, removes any trailing path separator on `pre` and
|
||||||
any leading path separator on `post`, and returns the concatenation of the two
|
any leading path separator on `post`, and returns the concatenation of the two
|
||||||
with a single path separator between them.
|
with a single path separator between them.
|
||||||
*/
|
"]
|
||||||
|
|
||||||
fn connect(pre: path, post: path) -> path unsafe {
|
fn connect(pre: path, post: path) -> path unsafe {
|
||||||
let mut pre_ = pre;
|
let mut pre_ = pre;
|
||||||
let mut post_ = post;
|
let mut post_ = post;
|
||||||
|
@ -122,13 +101,11 @@ fn connect(pre: path, post: path) -> path unsafe {
|
||||||
ret pre_ + path_sep() + post_;
|
ret pre_ + path_sep() + post_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: connect_many
|
|
||||||
|
|
||||||
Connects a vector of path segments into a single path.
|
Connects a vector of path segments into a single path.
|
||||||
|
|
||||||
Inserts path separators as needed.
|
Inserts path separators as needed.
|
||||||
*/
|
"]
|
||||||
fn connect_many(paths: [path]) -> path {
|
fn connect_many(paths: [path]) -> path {
|
||||||
ret if vec::len(paths) == 1u {
|
ret if vec::len(paths) == 1u {
|
||||||
paths[0]
|
paths[0]
|
||||||
|
@ -138,31 +115,29 @@ fn connect_many(paths: [path]) -> path {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: split
|
|
||||||
|
|
||||||
Split a path into it's individual components
|
Split a path into it's individual components
|
||||||
|
|
||||||
Splits a given path by path separators and returns a vector containing
|
Splits a given path by path separators and returns a vector containing
|
||||||
each piece of the path. On Windows, if the path is absolute then
|
each piece of the path. On Windows, if the path is absolute then
|
||||||
the first element of the returned vector will be the drive letter
|
the first element of the returned vector will be the drive letter
|
||||||
followed by a colon.
|
followed by a colon.
|
||||||
*/
|
"]
|
||||||
fn split(p: path) -> [path] {
|
fn split(p: path) -> [path] {
|
||||||
str::split_nonempty(p, {|c|
|
str::split_nonempty(p, {|c|
|
||||||
c == consts::path_sep || c == consts::alt_path_sep
|
c == consts::path_sep || c == consts::alt_path_sep
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: splitext
|
Split a path into the part before the extension and the extension
|
||||||
|
|
||||||
Split a path into a pair of strings with the first element being the filename
|
Split a path into a pair of strings with the first element being the filename
|
||||||
without the extension and the second being either empty or the file extension
|
without the extension and the second being either empty or the file extension
|
||||||
including the period. Leading periods in the basename are ignored. If the
|
including the period. Leading periods in the basename are ignored. If the
|
||||||
path includes directory components then they are included in the filename part
|
path includes directory components then they are included in the filename part
|
||||||
of the result pair.
|
of the result pair.
|
||||||
*/
|
"]
|
||||||
fn splitext(p: path) -> (str, str) {
|
fn splitext(p: path) -> (str, str) {
|
||||||
if str::is_empty(p) { ("", "") }
|
if str::is_empty(p) { ("", "") }
|
||||||
else {
|
else {
|
||||||
|
@ -204,13 +179,11 @@ fn splitext(p: path) -> (str, str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: normalize
|
Removes extra '.' and '..' entries from paths
|
||||||
|
|
||||||
Removes extra "." and ".." entries from paths.
|
|
||||||
|
|
||||||
Does not follow symbolic links.
|
Does not follow symbolic links.
|
||||||
*/
|
"]
|
||||||
fn normalize(p: path) -> path {
|
fn normalize(p: path) -> path {
|
||||||
let s = split(p);
|
let s = split(p);
|
||||||
let s = strip_dots(s);
|
let s = strip_dots(s);
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
/*
|
#[doc = "Unsafe pointer utility functions"]
|
||||||
Module: ptr
|
|
||||||
|
|
||||||
Unsafe pointer utility functions
|
|
||||||
*/
|
|
||||||
#[abi = "rust-intrinsic"]
|
#[abi = "rust-intrinsic"]
|
||||||
native mod rusti {
|
native mod rusti {
|
||||||
fn addr_of<T>(val: T) -> *T;
|
fn addr_of<T>(val: T) -> *T;
|
||||||
|
@ -11,70 +8,50 @@ native mod rusti {
|
||||||
fn memmove<T>(dst: *T, src: *T, count: ctypes::uintptr_t);
|
fn memmove<T>(dst: *T, src: *T, count: ctypes::uintptr_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Get an unsafe pointer to a value"]
|
||||||
Function: addr_of
|
|
||||||
|
|
||||||
Get an unsafe pointer to a value
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn addr_of<T>(val: T) -> *T { ret rusti::addr_of(val); }
|
fn addr_of<T>(val: T) -> *T { ret rusti::addr_of(val); }
|
||||||
|
|
||||||
/*
|
#[doc = "Get an unsafe mutable pointer to a value"]
|
||||||
Function: mut_addr_of
|
|
||||||
|
|
||||||
Get an unsafe mutable pointer to a value
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
|
fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
|
||||||
ret unsafe::reinterpret_cast(rusti::addr_of(val));
|
ret unsafe::reinterpret_cast(rusti::addr_of(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Calculate the offset from a pointer"]
|
||||||
Function: offset
|
|
||||||
|
|
||||||
Calculate the offset from a pointer
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn offset<T>(ptr: *T, count: uint) -> *T {
|
fn offset<T>(ptr: *T, count: uint) -> *T {
|
||||||
ret rusti::ptr_offset(ptr, count);
|
ret rusti::ptr_offset(ptr, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Calculate the offset from a mutable pointer"]
|
||||||
Function: mut_offset
|
|
||||||
|
|
||||||
Calculate the offset from a mutable pointer
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mut_offset<T>(ptr: *mutable T, count: uint) -> *mutable T {
|
fn mut_offset<T>(ptr: *mutable T, count: uint) -> *mutable T {
|
||||||
ret rusti::ptr_offset(ptr as *T, count) as *mutable T;
|
ret rusti::ptr_offset(ptr as *T, count) as *mutable T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
#[doc = "Create an unsafe null pointer"]
|
||||||
Function: null
|
|
||||||
|
|
||||||
Create an unsafe null pointer
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
|
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: memcpy
|
Copies data from one location to another
|
||||||
|
|
||||||
Copies data from one src to dst that is not overlapping each other.
|
Copies `count` elements (not bytes) from `src` to `dst`. The source
|
||||||
Count is the number of elements to copy and not the number of bytes.
|
and destination may not overlap.
|
||||||
*/
|
"]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
|
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
|
||||||
rusti::memcpy(dst, src, count);
|
rusti::memcpy(dst, src, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: memmove
|
Copies data from one location to another
|
||||||
|
|
||||||
Copies data from one src to dst, overlap between the two pointers may occur.
|
Copies `count` elements (not bytes) from `src` to `dst`. The source
|
||||||
Count is the number of elements to copy and not the number of bytes.
|
and destination may overlap.
|
||||||
*/
|
"]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
|
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
|
||||||
rusti::memmove(dst, src, count);
|
rusti::memmove(dst, src, count);
|
||||||
|
|
|
@ -1,42 +1,20 @@
|
||||||
/*
|
#[doc = "A type representing either success or failure"];
|
||||||
Module: result
|
|
||||||
|
|
||||||
A type representing either success or failure
|
#[doc = "The result type"]
|
||||||
*/
|
|
||||||
|
|
||||||
/* Section: Types */
|
|
||||||
|
|
||||||
/*
|
|
||||||
Tag: t
|
|
||||||
|
|
||||||
The result type
|
|
||||||
*/
|
|
||||||
enum t<T, U> {
|
enum t<T, U> {
|
||||||
/*
|
#[doc = "Contains the successful result value"]
|
||||||
Variant: ok
|
|
||||||
|
|
||||||
Contains the result value
|
|
||||||
*/
|
|
||||||
ok(T),
|
ok(T),
|
||||||
/*
|
#[doc = "Contains the error value"]
|
||||||
Variant: err
|
|
||||||
|
|
||||||
Contains the error value
|
|
||||||
*/
|
|
||||||
err(U)
|
err(U)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Section: Operations */
|
#[doc = "
|
||||||
|
|
||||||
/*
|
|
||||||
Function: get
|
|
||||||
|
|
||||||
Get the value out of a successful result
|
Get the value out of a successful result
|
||||||
|
|
||||||
Failure:
|
# Failure
|
||||||
|
|
||||||
If the result is an error
|
If the result is an error
|
||||||
*/
|
"]
|
||||||
fn get<T: copy, U>(res: t<T, U>) -> T {
|
fn get<T: copy, U>(res: t<T, U>) -> T {
|
||||||
alt res {
|
alt res {
|
||||||
ok(t) { t }
|
ok(t) { t }
|
||||||
|
@ -48,15 +26,13 @@ fn get<T: copy, U>(res: t<T, U>) -> T {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: get_err
|
|
||||||
|
|
||||||
Get the value out of an error result
|
Get the value out of an error result
|
||||||
|
|
||||||
Failure:
|
# Failure
|
||||||
|
|
||||||
If the result is not an error
|
If the result is not an error
|
||||||
*/
|
"]
|
||||||
fn get_err<T, U: copy>(res: t<T, U>) -> U {
|
fn get_err<T, U: copy>(res: t<T, U>) -> U {
|
||||||
alt res {
|
alt res {
|
||||||
err(u) { u }
|
err(u) { u }
|
||||||
|
@ -66,11 +42,7 @@ fn get_err<T, U: copy>(res: t<T, U>) -> U {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns true if the result is `ok`"]
|
||||||
Function: success
|
|
||||||
|
|
||||||
Returns true if the result is <ok>
|
|
||||||
*/
|
|
||||||
pure fn success<T, U>(res: t<T, U>) -> bool {
|
pure fn success<T, U>(res: t<T, U>) -> bool {
|
||||||
alt res {
|
alt res {
|
||||||
ok(_) { true }
|
ok(_) { true }
|
||||||
|
@ -78,15 +50,17 @@ pure fn success<T, U>(res: t<T, U>) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns true if the result is `error`"]
|
||||||
Function: failure
|
|
||||||
|
|
||||||
Returns true if the result is <error>
|
|
||||||
*/
|
|
||||||
pure fn failure<T, U>(res: t<T, U>) -> bool {
|
pure fn failure<T, U>(res: t<T, U>) -> bool {
|
||||||
!success(res)
|
!success(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc = "
|
||||||
|
Convert to the `either` type
|
||||||
|
|
||||||
|
`ok` result variants are converted to `either::right` variants, `err`
|
||||||
|
result variants are converted to `either::left`.
|
||||||
|
"]
|
||||||
pure fn to_either<T: copy, U: copy>(res: t<U, T>) -> either::t<T, U> {
|
pure fn to_either<T: copy, U: copy>(res: t<U, T>) -> either::t<T, U> {
|
||||||
alt res {
|
alt res {
|
||||||
ok(res) { either::right(res) }
|
ok(res) { either::right(res) }
|
||||||
|
@ -94,22 +68,19 @@ pure fn to_either<T: copy, U: copy>(res: t<U, T>) -> either::t<T, U> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: chain
|
|
||||||
|
|
||||||
Call a function based on a previous result
|
Call a function based on a previous result
|
||||||
|
|
||||||
If `res` is <ok> then the value is extracted and passed to `op` whereupon
|
If `res` is `ok` then the value is extracted and passed to `op` whereupon
|
||||||
`op`s result is returned. if `res` is <err> then it is immediately returned.
|
`op`s result is returned. if `res` is `err` then it is immediately returned.
|
||||||
This function can be used to compose the results of two functions.
|
This function can be used to compose the results of two functions.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
> let res = chain(read_file(file), { |buf|
|
let res = chain(read_file(file)) { |buf|
|
||||||
> ok(parse_buf(buf))
|
ok(parse_buf(buf))
|
||||||
> })
|
}
|
||||||
|
"]
|
||||||
*/
|
|
||||||
fn chain<T, U: copy, V: copy>(res: t<T, V>, op: fn(T) -> t<U, V>)
|
fn chain<T, U: copy, V: copy>(res: t<T, V>, op: fn(T) -> t<U, V>)
|
||||||
-> t<U, V> {
|
-> t<U, V> {
|
||||||
alt res {
|
alt res {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,5 @@
|
||||||
/*
|
#[doc = "Misc low level stuff"]
|
||||||
Module: sys
|
|
||||||
|
|
||||||
Misc low level stuff
|
|
||||||
*/
|
|
||||||
enum type_desc = {
|
enum type_desc = {
|
||||||
first_param: **ctypes::c_int,
|
first_param: **ctypes::c_int,
|
||||||
size: ctypes::size_t,
|
size: ctypes::size_t,
|
||||||
|
@ -31,48 +28,32 @@ native mod rusti {
|
||||||
fn frame_address(n: ctypes::c_uint) -> ctypes::uintptr_t;
|
fn frame_address(n: ctypes::c_uint) -> ctypes::uintptr_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: get_type_desc
|
Returns a pointer to a type descriptor.
|
||||||
|
|
||||||
Returns a pointer to a type descriptor. Useful for calling certain
|
Useful for calling certain function in the Rust runtime or otherwise
|
||||||
function in the Rust runtime or otherwise performing dark magick.
|
performing dark magick.
|
||||||
*/
|
"]
|
||||||
fn get_type_desc<T>() -> *type_desc {
|
fn get_type_desc<T>() -> *type_desc {
|
||||||
ret rusti::get_type_desc::<T>();
|
ret rusti::get_type_desc::<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Get a string representing the platform-dependent last error"]
|
||||||
Function: last_os_error
|
|
||||||
|
|
||||||
Get a string representing the platform-dependent last error
|
|
||||||
*/
|
|
||||||
fn last_os_error() -> str {
|
fn last_os_error() -> str {
|
||||||
rustrt::last_os_error()
|
rustrt::last_os_error()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns the size of a type"]
|
||||||
Function: size_of
|
|
||||||
|
|
||||||
Returns the size of a type
|
|
||||||
*/
|
|
||||||
fn size_of<T>() -> uint unsafe {
|
fn size_of<T>() -> uint unsafe {
|
||||||
ret (*get_type_desc::<T>()).size;
|
ret (*get_type_desc::<T>()).size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns the alignment of a type"]
|
||||||
Function: align_of
|
|
||||||
|
|
||||||
Returns the alignment of a type
|
|
||||||
*/
|
|
||||||
fn align_of<T>() -> uint unsafe {
|
fn align_of<T>() -> uint unsafe {
|
||||||
ret (*get_type_desc::<T>()).align;
|
ret (*get_type_desc::<T>()).align;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns the refcount of a shared box"]
|
||||||
Function: refcount
|
|
||||||
|
|
||||||
Returns the refcount of a shared box
|
|
||||||
*/
|
|
||||||
fn refcount<T>(t: @T) -> uint {
|
fn refcount<T>(t: @T) -> uint {
|
||||||
ret rustrt::refcount::<T>(t);
|
ret rustrt::refcount::<T>(t);
|
||||||
}
|
}
|
||||||
|
@ -81,14 +62,14 @@ fn log_str<T>(t: T) -> str {
|
||||||
rustrt::shape_log_str(get_type_desc::<T>(), t)
|
rustrt::shape_log_str(get_type_desc::<T>(), t)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc = "
|
||||||
brief = "Sets the process exit code",
|
Sets the process exit code
|
||||||
desc = "Sets the exit code returned by the process if all supervised \
|
|
||||||
tasks terminate successfully (without failing). If the current \
|
Sets the exit code returned by the process if all supervised tasks terminate
|
||||||
root task fails and is supervised by the scheduler then any \
|
successfully (without failing). If the current root task fails and is
|
||||||
user-specified exit status is ignored and the process exits \
|
supervised by the scheduler then any user-specified exit status is ignored and
|
||||||
with the default failure status."
|
the process exits with the default failure status
|
||||||
)]
|
"]
|
||||||
fn set_exit_status(code: int) {
|
fn set_exit_status(code: int) {
|
||||||
rustrt::rust_set_exit_status(code as ctypes::intptr_t);
|
rustrt::rust_set_exit_status(code as ctypes::intptr_t);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Task management.
|
Task management.
|
||||||
|
|
||||||
An executing Rust program consists of a tree of tasks, each with their own
|
An executing Rust program consists of a tree of tasks, each with their own
|
||||||
|
@ -14,12 +13,13 @@ process.
|
||||||
|
|
||||||
Tasks may execute in parallel and are scheduled automatically by the runtime.
|
Tasks may execute in parallel and are scheduled automatically by the runtime.
|
||||||
|
|
||||||
Example:
|
# Example
|
||||||
|
|
||||||
spawn {||
|
|
||||||
log(error, \"Hello, World!\");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
~~~
|
||||||
|
spawn {||
|
||||||
|
log(error, \"Hello, World!\");
|
||||||
|
}
|
||||||
|
~~~
|
||||||
"];
|
"];
|
||||||
|
|
||||||
export task;
|
export task;
|
||||||
|
@ -58,7 +58,6 @@ export get_task;
|
||||||
enum task = task_id;
|
enum task = task_id;
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Indicates the manner in which a task exited.
|
Indicates the manner in which a task exited.
|
||||||
|
|
||||||
A task that completes without failing and whose supervised children complete
|
A task that completes without failing and whose supervised children complete
|
||||||
|
@ -66,18 +65,13 @@ without failing is considered to exit successfully.
|
||||||
|
|
||||||
FIXME: This description does not indicate the current behavior for linked
|
FIXME: This description does not indicate the current behavior for linked
|
||||||
failure.
|
failure.
|
||||||
|
|
||||||
"]
|
"]
|
||||||
enum task_result {
|
enum task_result {
|
||||||
success,
|
success,
|
||||||
failure,
|
failure,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "A message type for notifying of task lifecycle events"]
|
||||||
|
|
||||||
A message type for notifying of task lifecycle events
|
|
||||||
|
|
||||||
"]
|
|
||||||
enum notification {
|
enum notification {
|
||||||
#[doc = "Sent when a task exits with the task handle and result"]
|
#[doc = "Sent when a task exits with the task handle and result"]
|
||||||
exit(task, task_result)
|
exit(task, task_result)
|
||||||
|
@ -96,10 +90,9 @@ enum sched_mode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Scheduler configuration options
|
Scheduler configuration options
|
||||||
|
|
||||||
Fields:
|
# Fields
|
||||||
|
|
||||||
* sched_mode - The operating mode of the scheduler
|
* sched_mode - The operating mode of the scheduler
|
||||||
|
|
||||||
|
@ -110,7 +103,6 @@ Fields:
|
||||||
appropriate for running code written in languages like C. By default these
|
appropriate for running code written in languages like C. By default these
|
||||||
native stacks have unspecified size, but with this option their size can
|
native stacks have unspecified size, but with this option their size can
|
||||||
be precisely specified.
|
be precisely specified.
|
||||||
|
|
||||||
"]
|
"]
|
||||||
type sched_opts = {
|
type sched_opts = {
|
||||||
mode: sched_mode,
|
mode: sched_mode,
|
||||||
|
@ -118,10 +110,9 @@ type sched_opts = {
|
||||||
};
|
};
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Task configuration options
|
Task configuration options
|
||||||
|
|
||||||
Fields:
|
# Fields
|
||||||
|
|
||||||
* supervise - Do not propagate failure to the parent task
|
* supervise - Do not propagate failure to the parent task
|
||||||
|
|
||||||
|
@ -152,11 +143,9 @@ type task_opts = {
|
||||||
};
|
};
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
The task builder type.
|
The task builder type.
|
||||||
|
|
||||||
Provides detailed control over the properties and behavior of new tasks.
|
Provides detailed control over the properties and behavior of new tasks.
|
||||||
|
|
||||||
"]
|
"]
|
||||||
// NB: Builders are designed to be single-use because they do stateful
|
// NB: Builders are designed to be single-use because they do stateful
|
||||||
// things that get weird when reusing - e.g. if you create a result future
|
// things that get weird when reusing - e.g. if you create a result future
|
||||||
|
@ -176,12 +165,10 @@ enum task_builder = {
|
||||||
|
|
||||||
fn default_task_opts() -> task_opts {
|
fn default_task_opts() -> task_opts {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
The default task options
|
The default task options
|
||||||
|
|
||||||
By default all tasks are supervised by their parent, are spawned
|
By default all tasks are supervised by their parent, are spawned
|
||||||
into the same scheduler, and do not post lifecycle notifications.
|
into the same scheduler, and do not post lifecycle notifications.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -211,7 +198,6 @@ fn get_opts(builder: task_builder) -> task_opts {
|
||||||
|
|
||||||
fn set_opts(builder: task_builder, opts: task_opts) {
|
fn set_opts(builder: task_builder, opts: task_opts) {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Set the task_opts associated with a task_builder
|
Set the task_opts associated with a task_builder
|
||||||
|
|
||||||
To update a single option use a pattern like the following:
|
To update a single option use a pattern like the following:
|
||||||
|
@ -220,7 +206,6 @@ fn set_opts(builder: task_builder, opts: task_opts) {
|
||||||
supervise: false
|
supervise: false
|
||||||
with get_opts(builder)
|
with get_opts(builder)
|
||||||
});
|
});
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
builder.opts = opts;
|
builder.opts = opts;
|
||||||
|
@ -228,7 +213,6 @@ fn set_opts(builder: task_builder, opts: task_opts) {
|
||||||
|
|
||||||
fn add_wrapper(builder: task_builder, gen_body: fn@(+fn~()) -> fn~()) {
|
fn add_wrapper(builder: task_builder, gen_body: fn@(+fn~()) -> fn~()) {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Add a wrapper to the body of the spawned task.
|
Add a wrapper to the body of the spawned task.
|
||||||
|
|
||||||
Before the task is spawned it is passed through a 'body generator'
|
Before the task is spawned it is passed through a 'body generator'
|
||||||
|
@ -239,7 +223,6 @@ fn add_wrapper(builder: task_builder, gen_body: fn@(+fn~()) -> fn~()) {
|
||||||
This function augments the current body generator with a new body
|
This function augments the current body generator with a new body
|
||||||
generator by applying the task body which results from the
|
generator by applying the task body which results from the
|
||||||
existing body generator to the new body generator.
|
existing body generator to the new body generator.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
let prev_gen_body = builder.gen_body;
|
let prev_gen_body = builder.gen_body;
|
||||||
|
@ -249,20 +232,18 @@ fn add_wrapper(builder: task_builder, gen_body: fn@(+fn~()) -> fn~()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(-builder: task_builder, +f: fn~()) {
|
fn run(-builder: task_builder, +f: fn~()) {
|
||||||
#[doc(desc = "
|
#[doc = "
|
||||||
|
|
||||||
Creates and exucutes a new child task
|
Creates and exucutes a new child task
|
||||||
|
|
||||||
Sets up a new task with its own call stack and schedules it to run
|
Sets up a new task with its own call stack and schedules it to run
|
||||||
the provided unique closure. The task has the properties and behavior
|
the provided unique closure. The task has the properties and behavior
|
||||||
specified by `builder`.
|
specified by `builder`.
|
||||||
|
|
||||||
", failure = "
|
# Failure
|
||||||
|
|
||||||
When spawning into a new scheduler, the number of threads requested
|
When spawning into a new scheduler, the number of threads requested
|
||||||
must be greater than zero.
|
must be greater than zero.
|
||||||
|
"];
|
||||||
")];
|
|
||||||
|
|
||||||
let body = builder.gen_body(f);
|
let body = builder.gen_body(f);
|
||||||
spawn_raw(builder.opts, body);
|
spawn_raw(builder.opts, body);
|
||||||
|
@ -273,7 +254,6 @@ fn run(-builder: task_builder, +f: fn~()) {
|
||||||
|
|
||||||
fn future_result(builder: task_builder) -> future::future<task_result> {
|
fn future_result(builder: task_builder) -> future::future<task_result> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Get a future representing the exit status of the task.
|
Get a future representing the exit status of the task.
|
||||||
|
|
||||||
Taking the value of the future will block until the child task terminates.
|
Taking the value of the future will block until the child task terminates.
|
||||||
|
@ -283,7 +263,6 @@ fn future_result(builder: task_builder) -> future::future<task_result> {
|
||||||
builder. If additional tasks are spawned with the same builder
|
builder. If additional tasks are spawned with the same builder
|
||||||
then a new result future must be obtained prior to spawning each
|
then a new result future must be obtained prior to spawning each
|
||||||
task.
|
task.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
// FIXME (1087, 1857): Once linked failure and notification are
|
// FIXME (1087, 1857): Once linked failure and notification are
|
||||||
|
@ -332,7 +311,6 @@ fn unsupervise(builder: task_builder) {
|
||||||
fn run_listener<A:send>(-builder: task_builder,
|
fn run_listener<A:send>(-builder: task_builder,
|
||||||
+f: fn~(comm::port<A>)) -> comm::chan<A> {
|
+f: fn~(comm::port<A>)) -> comm::chan<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Runs a new task while providing a channel from the parent to the child
|
Runs a new task while providing a channel from the parent to the child
|
||||||
|
|
||||||
Sets up a communication channel from the current task to the new
|
Sets up a communication channel from the current task to the new
|
||||||
|
@ -362,7 +340,6 @@ fn run_listener<A:send>(-builder: task_builder,
|
||||||
|
|
||||||
fn spawn(+f: fn~()) {
|
fn spawn(+f: fn~()) {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Creates and exucutes a new child task
|
Creates and exucutes a new child task
|
||||||
|
|
||||||
Sets up a new task with its own call stack and schedules it to run
|
Sets up a new task with its own call stack and schedules it to run
|
||||||
|
@ -376,7 +353,6 @@ fn spawn(+f: fn~()) {
|
||||||
|
|
||||||
fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
|
fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
|
||||||
#[doc = "
|
#[doc = "
|
||||||
|
|
||||||
Runs a new task while providing a channel from the parent to the child
|
Runs a new task while providing a channel from the parent to the child
|
||||||
|
|
||||||
Sets up a communication channel from the current task to the new
|
Sets up a communication channel from the current task to the new
|
||||||
|
@ -399,27 +375,24 @@ fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
|
||||||
// Likewise, the parent has both a 'po' and 'ch'
|
// Likewise, the parent has both a 'po' and 'ch'
|
||||||
|
|
||||||
This function is equivalent to `run_listener(mk_task_builder(), f)`.
|
This function is equivalent to `run_listener(mk_task_builder(), f)`.
|
||||||
|
|
||||||
"];
|
"];
|
||||||
|
|
||||||
run_listener(mk_task_builder(), f)
|
run_listener(mk_task_builder(), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_sched(mode: sched_mode, +f: fn~()) {
|
fn spawn_sched(mode: sched_mode, +f: fn~()) {
|
||||||
#[doc(desc = "
|
#[doc = "
|
||||||
|
|
||||||
Creates a new scheduler and executes a task on it
|
Creates a new scheduler and executes a task on it
|
||||||
|
|
||||||
Tasks subsequently spawned by that task will also execute on
|
Tasks subsequently spawned by that task will also execute on
|
||||||
the new scheduler. When there are no more tasks to execute the
|
the new scheduler. When there are no more tasks to execute the
|
||||||
scheduler terminates.
|
scheduler terminates.
|
||||||
|
|
||||||
", failure = "
|
# Failure
|
||||||
|
|
||||||
In manual threads mode the number of threads requested must be
|
In manual threads mode the number of threads requested must be
|
||||||
greater than zero.
|
greater than zero.
|
||||||
|
"];
|
||||||
")];
|
|
||||||
|
|
||||||
let mut builder = mk_task_builder();
|
let mut builder = mk_task_builder();
|
||||||
set_opts(builder, {
|
set_opts(builder, {
|
||||||
|
@ -433,18 +406,16 @@ fn spawn_sched(mode: sched_mode, +f: fn~()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try<T:send>(+f: fn~() -> T) -> result::t<T,()> {
|
fn try<T:send>(+f: fn~() -> T) -> result::t<T,()> {
|
||||||
#[doc(desc = "
|
#[doc = "
|
||||||
|
|
||||||
Execute a function in another task and return either the return value
|
Execute a function in another task and return either the return value
|
||||||
of the function or result::err.
|
of the function or result::err.
|
||||||
|
|
||||||
", return = "
|
# Return value
|
||||||
|
|
||||||
If the function executed successfully then try returns result::ok
|
If the function executed successfully then try returns result::ok
|
||||||
containing the value returned by the function. If the function fails
|
containing the value returned by the function. If the function fails
|
||||||
then try returns result::err containing nil.
|
then try returns result::err containing nil.
|
||||||
|
"];
|
||||||
")];
|
|
||||||
|
|
||||||
let po = comm::port();
|
let po = comm::port();
|
||||||
let ch = comm::chan(po);
|
let ch = comm::chan(po);
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
/*
|
|
||||||
Module: tuple
|
|
||||||
*/
|
|
||||||
|
|
||||||
fn first<T:copy, U:copy>(pair: (T, U)) -> T {
|
fn first<T:copy, U:copy>(pair: (T, U)) -> T {
|
||||||
let (t, _) = pair;
|
let (t, _) = pair;
|
||||||
ret t;
|
ret t;
|
||||||
|
|
|
@ -1,64 +1,25 @@
|
||||||
#[doc = "Operations and constants for `u32`"];
|
#[doc = "Operations and constants for `u32`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: u32
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Const: min_value
|
|
||||||
|
|
||||||
Return the minimal value for a u32
|
|
||||||
*/
|
|
||||||
const min_value: u32 = 0u32;
|
const min_value: u32 = 0u32;
|
||||||
|
|
||||||
/*
|
|
||||||
Const: max_value
|
|
||||||
|
|
||||||
Return the maximal value for a u32
|
|
||||||
*/
|
|
||||||
const max_value: u32 = 0u32 - 1u32;
|
const max_value: u32 = 0u32 - 1u32;
|
||||||
|
|
||||||
pure fn min(x: u32, y: u32) -> u32 { if x < y { x } else { y } }
|
pure fn min(x: u32, y: u32) -> u32 { if x < y { x } else { y } }
|
||||||
pure fn max(x: u32, y: u32) -> u32 { if x > y { x } else { y } }
|
pure fn max(x: u32, y: u32) -> u32 { if x > y { x } else { y } }
|
||||||
|
|
||||||
/* Function: add */
|
|
||||||
pure fn add(x: u32, y: u32) -> u32 { ret x + y; }
|
pure fn add(x: u32, y: u32) -> u32 { ret x + y; }
|
||||||
|
|
||||||
/* Function: sub */
|
|
||||||
pure fn sub(x: u32, y: u32) -> u32 { ret x - y; }
|
pure fn sub(x: u32, y: u32) -> u32 { ret x - y; }
|
||||||
|
|
||||||
/* Function: mul */
|
|
||||||
pure fn mul(x: u32, y: u32) -> u32 { ret x * y; }
|
pure fn mul(x: u32, y: u32) -> u32 { ret x * y; }
|
||||||
|
|
||||||
/* Function: div */
|
|
||||||
pure fn div(x: u32, y: u32) -> u32 { ret x / y; }
|
pure fn div(x: u32, y: u32) -> u32 { ret x / y; }
|
||||||
|
|
||||||
/* Function: rem */
|
|
||||||
pure fn rem(x: u32, y: u32) -> u32 { ret x % y; }
|
pure fn rem(x: u32, y: u32) -> u32 { ret x % y; }
|
||||||
|
|
||||||
/* Predicate: lt */
|
|
||||||
pure fn lt(x: u32, y: u32) -> bool { ret x < y; }
|
pure fn lt(x: u32, y: u32) -> bool { ret x < y; }
|
||||||
|
|
||||||
/* Predicate: le */
|
|
||||||
pure fn le(x: u32, y: u32) -> bool { ret x <= y; }
|
pure fn le(x: u32, y: u32) -> bool { ret x <= y; }
|
||||||
|
|
||||||
/* Predicate: eq */
|
|
||||||
pure fn eq(x: u32, y: u32) -> bool { ret x == y; }
|
pure fn eq(x: u32, y: u32) -> bool { ret x == y; }
|
||||||
|
|
||||||
/* Predicate: ne */
|
|
||||||
pure fn ne(x: u32, y: u32) -> bool { ret x != y; }
|
pure fn ne(x: u32, y: u32) -> bool { ret x != y; }
|
||||||
|
|
||||||
/* Predicate: ge */
|
|
||||||
pure fn ge(x: u32, y: u32) -> bool { ret x >= y; }
|
pure fn ge(x: u32, y: u32) -> bool { ret x >= y; }
|
||||||
|
|
||||||
/* Predicate: gt */
|
|
||||||
pure fn gt(x: u32, y: u32) -> bool { ret x > y; }
|
pure fn gt(x: u32, y: u32) -> bool { ret x > y; }
|
||||||
|
|
||||||
/*
|
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||||
Function: range
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`)
|
|
||||||
*/
|
|
||||||
fn range(lo: u32, hi: u32, it: fn(u32)) {
|
fn range(lo: u32, hi: u32, it: fn(u32)) {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi { it(i); i += 1u32; }
|
while i < hi { it(i); i += 1u32; }
|
||||||
|
|
|
@ -1,74 +1,31 @@
|
||||||
#[doc = "Operations and constants for `u64`"];
|
#[doc = "Operations and constants for `u64`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: u64
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Const: min_value
|
|
||||||
|
|
||||||
Return the minimal value for a u64
|
|
||||||
*/
|
|
||||||
const min_value: u64 = 0u64;
|
const min_value: u64 = 0u64;
|
||||||
|
|
||||||
/*
|
|
||||||
Const: max_value
|
|
||||||
|
|
||||||
Return the maximal value for a u64
|
|
||||||
*/
|
|
||||||
const max_value: u64 = 0u64 - 1u64;
|
const max_value: u64 = 0u64 - 1u64;
|
||||||
|
|
||||||
pure fn min(x: u64, y: u64) -> u64 { if x < y { x } else { y } }
|
pure fn min(x: u64, y: u64) -> u64 { if x < y { x } else { y } }
|
||||||
pure fn max(x: u64, y: u64) -> u64 { if x > y { x } else { y } }
|
pure fn max(x: u64, y: u64) -> u64 { if x > y { x } else { y } }
|
||||||
|
|
||||||
/* Function: add */
|
|
||||||
pure fn add(x: u64, y: u64) -> u64 { ret x + y; }
|
pure fn add(x: u64, y: u64) -> u64 { ret x + y; }
|
||||||
|
|
||||||
/* Function: sub */
|
|
||||||
pure fn sub(x: u64, y: u64) -> u64 { ret x - y; }
|
pure fn sub(x: u64, y: u64) -> u64 { ret x - y; }
|
||||||
|
|
||||||
/* Function: mul */
|
|
||||||
pure fn mul(x: u64, y: u64) -> u64 { ret x * y; }
|
pure fn mul(x: u64, y: u64) -> u64 { ret x * y; }
|
||||||
|
|
||||||
/* Function: div */
|
|
||||||
pure fn div(x: u64, y: u64) -> u64 { ret x / y; }
|
pure fn div(x: u64, y: u64) -> u64 { ret x / y; }
|
||||||
|
|
||||||
/* Function: rem */
|
|
||||||
pure fn rem(x: u64, y: u64) -> u64 { ret x % y; }
|
pure fn rem(x: u64, y: u64) -> u64 { ret x % y; }
|
||||||
|
|
||||||
/* Predicate: lt */
|
|
||||||
pure fn lt(x: u64, y: u64) -> bool { ret x < y; }
|
pure fn lt(x: u64, y: u64) -> bool { ret x < y; }
|
||||||
|
|
||||||
/* Predicate: le */
|
|
||||||
pure fn le(x: u64, y: u64) -> bool { ret x <= y; }
|
pure fn le(x: u64, y: u64) -> bool { ret x <= y; }
|
||||||
|
|
||||||
/* Predicate: eq */
|
|
||||||
pure fn eq(x: u64, y: u64) -> bool { ret x == y; }
|
pure fn eq(x: u64, y: u64) -> bool { ret x == y; }
|
||||||
|
|
||||||
/* Predicate: ne */
|
|
||||||
pure fn ne(x: u64, y: u64) -> bool { ret x != y; }
|
pure fn ne(x: u64, y: u64) -> bool { ret x != y; }
|
||||||
|
|
||||||
/* Predicate: ge */
|
|
||||||
pure fn ge(x: u64, y: u64) -> bool { ret x >= y; }
|
pure fn ge(x: u64, y: u64) -> bool { ret x >= y; }
|
||||||
|
|
||||||
/* Predicate: gt */
|
|
||||||
pure fn gt(x: u64, y: u64) -> bool { ret x > y; }
|
pure fn gt(x: u64, y: u64) -> bool { ret x > y; }
|
||||||
|
|
||||||
/*
|
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||||
Function: range
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`)
|
|
||||||
*/
|
|
||||||
fn range(lo: u64, hi: u64, it: fn(u64)) {
|
fn range(lo: u64, hi: u64, it: fn(u64)) {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi { it(i); i += 1u64; }
|
while i < hi { it(i); i += 1u64; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string in a given base"]
|
||||||
Function: to_str
|
|
||||||
|
|
||||||
Convert to a string in a given base
|
|
||||||
*/
|
|
||||||
fn to_str(n: u64, radix: uint) -> str {
|
fn to_str(n: u64, radix: uint) -> str {
|
||||||
assert (0u < radix && radix <= 16u);
|
assert (0u < radix && radix <= 16u);
|
||||||
|
|
||||||
|
@ -105,18 +62,10 @@ fn to_str(n: u64, radix: uint) -> str {
|
||||||
ret s;
|
ret s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string"]
|
||||||
Function: str
|
|
||||||
|
|
||||||
Convert to a string
|
|
||||||
*/
|
|
||||||
fn str(n: u64) -> str { ret to_str(n, 10u); }
|
fn str(n: u64) -> str { ret to_str(n, 10u); }
|
||||||
|
|
||||||
/*
|
#[doc = "Parse a string as an unsigned integer."]
|
||||||
Function: from_str
|
|
||||||
|
|
||||||
Parse a string as an unsigned integer.
|
|
||||||
*/
|
|
||||||
fn from_str(buf: str, radix: u64) -> option<u64> {
|
fn from_str(buf: str, radix: u64) -> option<u64> {
|
||||||
if str::len(buf) == 0u { ret none; }
|
if str::len(buf) == 0u { ret none; }
|
||||||
let mut i = str::len(buf) - 1u;
|
let mut i = str::len(buf) - 1u;
|
||||||
|
|
|
@ -1,64 +1,27 @@
|
||||||
#[doc = "Operations and constants for `u8`"];
|
#[doc = "Operations and constants for `u8`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: u8
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Const: min_value
|
|
||||||
|
|
||||||
The minumum value of a u8.
|
|
||||||
*/
|
|
||||||
const min_value: u8 = 0u8;
|
const min_value: u8 = 0u8;
|
||||||
|
|
||||||
/*
|
|
||||||
Const: max_value
|
|
||||||
|
|
||||||
The maximum value of a u8.
|
|
||||||
*/
|
|
||||||
const max_value: u8 = 0u8 - 1u8;
|
const max_value: u8 = 0u8 - 1u8;
|
||||||
|
|
||||||
/* Function: add */
|
pure fn min(x: u8, y: u8) -> u8 { if x < y { x } else { y } }
|
||||||
|
pure fn max(x: u8, y: u8) -> u8 { if x > y { x } else { y } }
|
||||||
|
|
||||||
pure fn add(x: u8, y: u8) -> u8 { ret x + y; }
|
pure fn add(x: u8, y: u8) -> u8 { ret x + y; }
|
||||||
|
|
||||||
/* Function: sub */
|
|
||||||
pure fn sub(x: u8, y: u8) -> u8 { ret x - y; }
|
pure fn sub(x: u8, y: u8) -> u8 { ret x - y; }
|
||||||
|
|
||||||
/* Function: mul */
|
|
||||||
pure fn mul(x: u8, y: u8) -> u8 { ret x * y; }
|
pure fn mul(x: u8, y: u8) -> u8 { ret x * y; }
|
||||||
|
|
||||||
/* Function: div */
|
|
||||||
pure fn div(x: u8, y: u8) -> u8 { ret x / y; }
|
pure fn div(x: u8, y: u8) -> u8 { ret x / y; }
|
||||||
|
|
||||||
/* Function: rem */
|
|
||||||
pure fn rem(x: u8, y: u8) -> u8 { ret x % y; }
|
pure fn rem(x: u8, y: u8) -> u8 { ret x % y; }
|
||||||
|
|
||||||
/* Predicate: lt */
|
|
||||||
pure fn lt(x: u8, y: u8) -> bool { ret x < y; }
|
pure fn lt(x: u8, y: u8) -> bool { ret x < y; }
|
||||||
|
|
||||||
/* Predicate: le */
|
|
||||||
pure fn le(x: u8, y: u8) -> bool { ret x <= y; }
|
pure fn le(x: u8, y: u8) -> bool { ret x <= y; }
|
||||||
|
|
||||||
/* Predicate: eq */
|
|
||||||
pure fn eq(x: u8, y: u8) -> bool { ret x == y; }
|
pure fn eq(x: u8, y: u8) -> bool { ret x == y; }
|
||||||
|
|
||||||
/* Predicate: ne */
|
|
||||||
pure fn ne(x: u8, y: u8) -> bool { ret x != y; }
|
pure fn ne(x: u8, y: u8) -> bool { ret x != y; }
|
||||||
|
|
||||||
/* Predicate: ge */
|
|
||||||
pure fn ge(x: u8, y: u8) -> bool { ret x >= y; }
|
pure fn ge(x: u8, y: u8) -> bool { ret x >= y; }
|
||||||
|
|
||||||
/* Predicate: gt */
|
|
||||||
pure fn gt(x: u8, y: u8) -> bool { ret x > y; }
|
pure fn gt(x: u8, y: u8) -> bool { ret x > y; }
|
||||||
|
|
||||||
/* Predicate: is_ascii */
|
|
||||||
pure fn is_ascii(x: u8) -> bool { ret 0u8 == x & 128u8; }
|
pure fn is_ascii(x: u8) -> bool { ret 0u8 == x & 128u8; }
|
||||||
|
|
||||||
/*
|
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||||
Function: range
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`)
|
|
||||||
*/
|
|
||||||
fn range(lo: u8, hi: u8, it: fn(u8)) {
|
fn range(lo: u8, hi: u8, it: fn(u8)) {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi { it(i); i += 1u8; }
|
while i < hi { it(i); i += 1u8; }
|
||||||
|
|
|
@ -1,158 +1,103 @@
|
||||||
#[doc = "Operations and constants for `uint`"];
|
#[doc = "Operations and constants for `uint`"];
|
||||||
|
|
||||||
/*
|
|
||||||
Module: uint
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Const: min_value
|
|
||||||
|
|
||||||
Return the minimal value for an uint.
|
|
||||||
|
|
||||||
This is always 0
|
|
||||||
*/
|
|
||||||
const min_value: uint = 0u;
|
const min_value: uint = 0u;
|
||||||
|
|
||||||
/*
|
|
||||||
Const: max_value
|
|
||||||
|
|
||||||
Return the maximal value for an uint.
|
|
||||||
|
|
||||||
This is 2^wordsize - 1
|
|
||||||
*/
|
|
||||||
const max_value: uint = 0u - 1u;
|
const max_value: uint = 0u - 1u;
|
||||||
|
|
||||||
/*
|
pure fn min(x: uint, y: uint) -> uint { if x < y { x } else { y } }
|
||||||
Function: min
|
pure fn max(x: uint, y: uint) -> uint { if x > y { x } else { y } }
|
||||||
*/
|
|
||||||
pure fn min(x: uint, y: uint) -> uint {
|
|
||||||
if x < y { x } else { y }
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: max
|
|
||||||
*/
|
|
||||||
pure fn max(x: uint, y: uint) -> uint {
|
|
||||||
if x > y { x } else { y }
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Function: add */
|
|
||||||
pure fn add(x: uint, y: uint) -> uint { ret x + y; }
|
pure fn add(x: uint, y: uint) -> uint { ret x + y; }
|
||||||
|
|
||||||
/* Function: sub */
|
|
||||||
pure fn sub(x: uint, y: uint) -> uint { ret x - y; }
|
pure fn sub(x: uint, y: uint) -> uint { ret x - y; }
|
||||||
|
|
||||||
/* Function: mul */
|
|
||||||
pure fn mul(x: uint, y: uint) -> uint { ret x * y; }
|
pure fn mul(x: uint, y: uint) -> uint { ret x * y; }
|
||||||
|
|
||||||
/* Function: div */
|
|
||||||
pure fn div(x: uint, y: uint) -> uint { ret x / y; }
|
pure fn div(x: uint, y: uint) -> uint { ret x / y; }
|
||||||
|
|
||||||
/* Function: div_ceil
|
pure fn rem(x: uint, y: uint) -> uint { ret x % y; }
|
||||||
|
pure fn lt(x: uint, y: uint) -> bool { ret x < y; }
|
||||||
|
pure fn le(x: uint, y: uint) -> bool { ret x <= y; }
|
||||||
|
pure fn eq(x: uint, y: uint) -> bool { ret x == y; }
|
||||||
|
pure fn ne(x: uint, y: uint) -> bool { ret x != y; }
|
||||||
|
pure fn ge(x: uint, y: uint) -> bool { ret x >= y; }
|
||||||
|
pure fn gt(x: uint, y: uint) -> bool { ret x > y; }
|
||||||
|
|
||||||
Divide two numbers, return the result, rounded up.
|
|
||||||
|
|
||||||
Parameters:
|
#[doc = "
|
||||||
x - an integer
|
Divide two numbers, return the result, rounded up.
|
||||||
y - an integer distinct from 0u
|
|
||||||
|
|
||||||
Return:
|
# Arguments
|
||||||
The smallest integer `q` such that `x/y <= q`.
|
|
||||||
*/
|
* x - an integer
|
||||||
|
* y - an integer distinct from 0u
|
||||||
|
|
||||||
|
# Return value
|
||||||
|
|
||||||
|
The smallest integer `q` such that `x/y <= q`.
|
||||||
|
"]
|
||||||
pure fn div_ceil(x: uint, y: uint) -> uint {
|
pure fn div_ceil(x: uint, y: uint) -> uint {
|
||||||
let div = div(x, y);
|
let div = div(x, y);
|
||||||
if x % y == 0u { ret div;}
|
if x % y == 0u { ret div;}
|
||||||
else { ret div + 1u; }
|
else { ret div + 1u; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function: div_ceil
|
#[doc = "
|
||||||
|
Divide two numbers, return the result, rounded to the closest integer.
|
||||||
|
|
||||||
Divide two numbers, return the result, rounded to the closest integer.
|
# Arguments
|
||||||
|
|
||||||
Parameters:
|
* x - an integer
|
||||||
x - an integer
|
* y - an integer distinct from 0u
|
||||||
y - an integer distinct from 0u
|
|
||||||
|
|
||||||
Return:
|
# Return value
|
||||||
The integer `q` closest to `x/y`.
|
|
||||||
*/
|
The integer `q` closest to `x/y`.
|
||||||
|
"]
|
||||||
pure fn div_round(x: uint, y: uint) -> uint {
|
pure fn div_round(x: uint, y: uint) -> uint {
|
||||||
let div = div(x, y);
|
let div = div(x, y);
|
||||||
if x % y * 2u < y { ret div;}
|
if x % y * 2u < y { ret div;}
|
||||||
else { ret div + 1u; }
|
else { ret div + 1u; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function: div_ceil
|
#[doc = "
|
||||||
|
Divide two numbers, return the result, rounded down.
|
||||||
|
|
||||||
Divide two numbers, return the result, rounded down.
|
Note: This is the same function as `div`.
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
x - an integer
|
|
||||||
y - an integer distinct from 0u
|
|
||||||
|
|
||||||
Note: This is the same function as `div`.
|
* x - an integer
|
||||||
|
* y - an integer distinct from 0u
|
||||||
|
|
||||||
Return:
|
# Return value
|
||||||
The smallest integer `q` such that `x/y <= q`. This
|
|
||||||
is either `x/y` or `x/y + 1`.
|
The smallest integer `q` such that `x/y <= q`. This
|
||||||
*/
|
is either `x/y` or `x/y + 1`.
|
||||||
|
"]
|
||||||
pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; }
|
pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; }
|
||||||
|
|
||||||
/* Function: rem */
|
#[doc = "Produce a uint suitable for use in a hash table"]
|
||||||
pure fn rem(x: uint, y: uint) -> uint { ret x % y; }
|
|
||||||
|
|
||||||
/* Predicate: lt */
|
|
||||||
pure fn lt(x: uint, y: uint) -> bool { ret x < y; }
|
|
||||||
|
|
||||||
/* Predicate: le */
|
|
||||||
pure fn le(x: uint, y: uint) -> bool { ret x <= y; }
|
|
||||||
|
|
||||||
/* Predicate: eq */
|
|
||||||
pure fn eq(x: uint, y: uint) -> bool { ret x == y; }
|
|
||||||
|
|
||||||
/* Predicate: ne */
|
|
||||||
pure fn ne(x: uint, y: uint) -> bool { ret x != y; }
|
|
||||||
|
|
||||||
/* Predicate: ge */
|
|
||||||
pure fn ge(x: uint, y: uint) -> bool { ret x >= y; }
|
|
||||||
|
|
||||||
/* Predicate: gt */
|
|
||||||
pure fn gt(x: uint, y: uint) -> bool { ret x > y; }
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function: hash
|
|
||||||
|
|
||||||
Produce a uint suitable for use in a hash table
|
|
||||||
*/
|
|
||||||
fn hash(x: uint) -> uint { ret x; }
|
fn hash(x: uint) -> uint { ret x; }
|
||||||
|
|
||||||
/*
|
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||||
Function: range
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`)
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn range(lo: uint, hi: uint, it: fn(uint)) {
|
fn range(lo: uint, hi: uint, it: fn(uint)) {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi { it(i); i += 1u; }
|
while i < hi { it(i); i += 1u; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: iterate
|
|
||||||
|
|
||||||
Iterate over the range [`lo`..`hi`), or stop when requested
|
Iterate over the range [`lo`..`hi`), or stop when requested
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
lo - The integer at which to start the loop (included)
|
|
||||||
hi - The integer at which to stop the loop (excluded)
|
|
||||||
it - A block to execute with each consecutive integer of the range.
|
|
||||||
Return `true` to continue, `false` to stop.
|
|
||||||
|
|
||||||
Returns:
|
* lo - The integer at which to start the loop (included)
|
||||||
|
* hi - The integer at which to stop the loop (excluded)
|
||||||
|
* it - A block to execute with each consecutive integer of the range.
|
||||||
|
Return `true` to continue, `false` to stop.
|
||||||
|
|
||||||
|
# Return value
|
||||||
|
|
||||||
`true` If execution proceeded correctly, `false` if it was interrupted,
|
`true` If execution proceeded correctly, `false` if it was interrupted,
|
||||||
that is if `it` returned `false` at any point.
|
that is if `it` returned `false` at any point.
|
||||||
*/
|
"]
|
||||||
fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
|
fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
|
||||||
let mut i = lo;
|
let mut i = lo;
|
||||||
while i < hi {
|
while i < hi {
|
||||||
|
@ -162,11 +107,7 @@ fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
|
||||||
ret true;
|
ret true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Returns the smallest power of 2 greater than or equal to `n`"]
|
||||||
Function: next_power_of_two
|
|
||||||
|
|
||||||
Returns the smallest power of 2 greater than or equal to `n`
|
|
||||||
*/
|
|
||||||
fn next_power_of_two(n: uint) -> uint {
|
fn next_power_of_two(n: uint) -> uint {
|
||||||
let halfbits: uint = sys::size_of::<uint>() * 4u;
|
let halfbits: uint = sys::size_of::<uint>() * 4u;
|
||||||
let mut tmp: uint = n - 1u;
|
let mut tmp: uint = n - 1u;
|
||||||
|
@ -175,20 +116,18 @@ fn next_power_of_two(n: uint) -> uint {
|
||||||
ret tmp + 1u;
|
ret tmp + 1u;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: parse_buf
|
|
||||||
|
|
||||||
Parse a buffer of bytes
|
Parse a buffer of bytes
|
||||||
|
|
||||||
Parameters:
|
# Arguments
|
||||||
|
|
||||||
buf - A byte buffer
|
* buf - A byte buffer
|
||||||
radix - The base of the number
|
* radix - The base of the number
|
||||||
|
|
||||||
Failure:
|
# Failure
|
||||||
|
|
||||||
buf must not be empty
|
`buf` must not be empty
|
||||||
*/
|
"]
|
||||||
fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
|
fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
|
||||||
if vec::len(buf) == 0u { ret none; }
|
if vec::len(buf) == 0u { ret none; }
|
||||||
let mut i = vec::len(buf) - 1u;
|
let mut i = vec::len(buf) - 1u;
|
||||||
|
@ -206,18 +145,10 @@ fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
|
||||||
fail;
|
fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Parse a string to an int"]
|
||||||
Function: from_str
|
|
||||||
|
|
||||||
Parse a string to an int
|
|
||||||
*/
|
|
||||||
fn from_str(s: str) -> option<uint> { parse_buf(str::bytes(s), 10u) }
|
fn from_str(s: str) -> option<uint> { parse_buf(str::bytes(s), 10u) }
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string in a given base"]
|
||||||
Function: to_str
|
|
||||||
|
|
||||||
Convert to a string in a given base
|
|
||||||
*/
|
|
||||||
fn to_str(num: uint, radix: uint) -> str {
|
fn to_str(num: uint, radix: uint) -> str {
|
||||||
let mut n = num;
|
let mut n = num;
|
||||||
assert (0u < radix && radix <= 16u);
|
assert (0u < radix && radix <= 16u);
|
||||||
|
@ -254,18 +185,10 @@ fn to_str(num: uint, radix: uint) -> str {
|
||||||
ret s1;
|
ret s1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "Convert to a string"]
|
||||||
Function: str
|
|
||||||
|
|
||||||
Convert to a string
|
|
||||||
*/
|
|
||||||
fn str(i: uint) -> str { ret to_str(i, 10u); }
|
fn str(i: uint) -> str { ret to_str(i, 10u); }
|
||||||
|
|
||||||
/*
|
#[doc = "Computes the bitwise complement"]
|
||||||
Function: compl
|
|
||||||
|
|
||||||
Computes the bitwise complement.
|
|
||||||
*/
|
|
||||||
fn compl(i: uint) -> uint {
|
fn compl(i: uint) -> uint {
|
||||||
max_value ^ i
|
max_value ^ i
|
||||||
}
|
}
|
||||||
|
|
|
@ -2565,6 +2565,7 @@ mod general_category {
|
||||||
|
|
||||||
}
|
}
|
||||||
mod derived_property {
|
mod derived_property {
|
||||||
|
#[doc = "Check if a character has the alphabetic unicode property"]
|
||||||
pure fn Alphabetic(c: char) -> bool {
|
pure fn Alphabetic(c: char) -> bool {
|
||||||
ret alt c {
|
ret alt c {
|
||||||
'\x41' to '\x5a'
|
'\x41' to '\x5a'
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
/*
|
#[doc = "Unsafe operations"];
|
||||||
Module: unsafe
|
|
||||||
|
|
||||||
Unsafe operations
|
|
||||||
*/
|
|
||||||
|
|
||||||
export reinterpret_cast, leak;
|
export reinterpret_cast, leak;
|
||||||
|
|
||||||
|
@ -12,11 +8,9 @@ native mod rusti {
|
||||||
fn leak<T>(-thing: T);
|
fn leak<T>(-thing: T);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc = "
|
||||||
Function: reinterpret_cast
|
|
||||||
|
|
||||||
Casts the value at `src` to U. The two types must have the same length.
|
Casts the value at `src` to U. The two types must have the same length.
|
||||||
*/
|
"]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
|
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
|
||||||
let t1 = sys::get_type_desc::<T>();
|
let t1 = sys::get_type_desc::<T>();
|
||||||
|
@ -27,16 +21,14 @@ unsafe fn reinterpret_cast<T, U>(src: T) -> U {
|
||||||
ret rusti::cast(src);
|
ret rusti::cast(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[doc ="
|
||||||
Function: leak
|
Move a thing into the void
|
||||||
|
|
||||||
Move `thing` into the void.
|
|
||||||
|
|
||||||
The leak function will take ownership of the provided value but neglect
|
The leak function will take ownership of the provided value but neglect
|
||||||
to run any required cleanup or memory-management operations on it. This
|
to run any required cleanup or memory-management operations on it. This
|
||||||
can be used for various acts of magick, particularly when using
|
can be used for various acts of magick, particularly when using
|
||||||
reinterpret_cast on managed pointer types.
|
reinterpret_cast on managed pointer types.
|
||||||
*/
|
"]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn leak<T>(-thing: T) { rusti::leak(thing); }
|
unsafe fn leak<T>(-thing: T) { rusti::leak(thing); }
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue