提交 fdf0c1b3 编写于 作者: P Patrick Walton

core: Newtype a bunch of types in libcore

上级 c0961bb8
......@@ -46,28 +46,32 @@
* pointers achieved about 103 million pushes/second. Using an option
* type could only produce 47 million pushes/second.
*/
type dvec<A> = {
type dvec_<A> = {
mut data: ~[mut A]
};
enum dvec<A> {
dvec_(dvec_<A>)
}
/// Creates a new, empty dvec
fn dvec<A>() -> dvec<A> {
{mut data: ~[mut]}
dvec_({mut data: ~[mut]})
}
/// Creates a new dvec with a single element
fn from_elem<A>(+e: A) -> dvec<A> {
{mut data: ~[mut e]}
dvec_({mut data: ~[mut e]})
}
/// Creates a new dvec with the contents of a vector
fn from_vec<A>(+v: ~[mut A]) -> dvec<A> {
{mut data: v}
dvec_({mut data: v})
}
/// Consumes the vector and returns its contents
fn unwrap<A>(-d: dvec<A>) -> ~[mut A] {
let {data: v} <- d;
let dvec_({data: v}) <- d;
ret v;
}
......
......@@ -21,7 +21,7 @@ fn position(f: fn(A) -> bool) -> option<uint> {
}
}
impl extensions<A:copy> for IMPL_T<A> {
impl extensions<A:copy> of iter::copyable_iter<A> for IMPL_T<A> {
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
......
......@@ -7,6 +7,15 @@
fn times(it: fn() -> bool);
}
trait copyable_iter<A:copy> {
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
fn to_vec() -> ~[A];
fn min() -> A;
fn max() -> A;
fn find(p: fn(A) -> bool) -> option<A>;
}
fn eachi<A,IA:base_iter<A>>(self: IA, blk: fn(uint, A) -> bool) {
let mut i = 0u;
for self.each |a| {
......
......@@ -11,16 +11,24 @@ enum state {
terminated
}
type packet_header = {
type packet_header_ = {
mut state: state,
mut blocked_task: option<*rust_task>,
};
type packet<T: send> = {
enum packet_header {
packet_header_(packet_header_)
}
type packet_<T:send> = {
header: packet_header,
mut payload: option<T>
};
enum packet<T:send> {
packet_(packet_<T>)
}
fn packet<T: send>() -> *packet<T> unsafe {
let p: *packet<T> = unsafe::transmute(~{
header: {
......@@ -428,8 +436,17 @@ fn spawn_service_recv<T: send>(
}
}
type chan<T:send> = { mut endp: option<streamp::client::open<T>> };
type port<T:send> = { mut endp: option<streamp::server::open<T>> };
type chan_<T:send> = { mut endp: option<streamp::client::open<T>> };
enum chan<T:send> {
chan_(chan_<T>)
}
type port_<T:send> = { mut endp: option<streamp::server::open<T>> };
enum port<T:send> {
port_(port_<T>)
}
fn stream<T:send>() -> (chan<T>, port<T>) {
let (c, s) = streamp::init();
......@@ -439,7 +456,7 @@ fn stream<T:send>() -> (chan<T>, port<T>) {
unsafe { let y <- *ptr::addr_of(x); y }]
];
({ mut endp: some(c) }, { mut endp: some(s) })
(chan_({ mut endp: some(c) }), port_({ mut endp: some(s) }))
}
impl chan<T: send> for chan<T> {
......
......@@ -123,8 +123,13 @@ unsafe fn memset<T>(dst: *mut T, c: int, count: uint) {
libc_::memset(dst as *c_void, c as libc::c_int, n as size_t);
}
trait ptr {
pure fn is_null() -> bool;
pure fn is_not_null() -> bool;
}
/// Extension methods for pointers
impl extensions<T> for *T {
impl extensions<T> of ptr for *T {
/// Returns true if the pointer is equal to the null pointer.
pure fn is_null() -> bool { is_null(self) }
......
......@@ -1882,8 +1882,15 @@ fn test_from_buf_len() {
}
trait unique_str {
fn trim() -> self;
fn trim_left() -> self;
fn trim_right() -> self;
pure fn +(rhs: str/&) -> self;
}
/// Extension methods for strings
impl extensions for str {
impl extensions of unique_str for str {
/// Returns a string with leading and trailing whitespace removed
#[inline]
fn trim() -> str { trim(self) }
......@@ -1901,8 +1908,35 @@ fn trim_right() -> str { trim_right(self) }
}
}
trait str_slice {
fn all(it: fn(char) -> bool) -> bool;
fn any(it: fn(char) -> bool) -> bool;
fn contains(needle: str/&a) -> bool;
fn contains_char(needle: char) -> bool;
fn each(it: fn(u8) -> bool);
fn eachi(it: fn(uint, u8) -> bool);
fn each_char(it: fn(char) -> bool);
fn each_chari(it: fn(uint, char) -> bool);
fn ends_with(needle: str/&) -> bool;
fn is_empty() -> bool;
fn is_not_empty() -> bool;
fn is_whitespace() -> bool;
fn is_alphanumeric() -> bool;
pure fn len() -> uint;
fn slice(begin: uint, end: uint) -> str;
fn split(sepfn: fn(char) -> bool) -> ~[str];
fn split_char(sep: char) -> ~[str];
fn split_str(sep: str/&a) -> ~[str];
fn starts_with(needle: str/&a) -> bool;
fn substr(begin: uint, n: uint) -> str;
fn to_lower() -> str;
fn to_upper() -> str;
fn escape_default() -> str;
fn escape_unicode() -> str;
}
/// Extension methods for strings
impl extensions/& for str/& {
impl extensions/& of str_slice for str/& {
/**
* Return true if a predicate matches all characters or if the string
* contains no characters
......
......@@ -1181,22 +1181,32 @@ fn as_mut_buf<E,T>(v: &[mut E], f: fn(*mut E) -> T) -> T {
}
}
impl extensions<T: copy> for ~[T] {
trait vec_concat<T> {
pure fn +(rhs: &[const T]) -> self;
}
impl extensions<T: copy> of vec_concat<T> for ~[T] {
#[inline(always)]
pure fn +(rhs: &[const T]) -> ~[T] {
append(self, rhs)
}
}
impl extensions<T: copy> for ~[mut T] {
impl extensions<T: copy> of vec_concat<T> for ~[mut T] {
#[inline(always)]
pure fn +(rhs: &[const T]) -> ~[mut T] {
append_mut(self, rhs)
}
}
trait const_vector {
pure fn is_empty() -> bool;
pure fn is_not_empty() -> bool;
pure fn len() -> uint;
}
/// Extension methods for vectors
impl extensions/&<T> for &[const T] {
impl extensions/&<T> of const_vector for &[const T] {
/// Returns true if a vector contains no elements
#[inline]
pure fn is_empty() -> bool { is_empty(self) }
......@@ -1208,8 +1218,16 @@ impl extensions/&<T> for &[const T] {
pure fn len() -> uint { len(self) }
}
trait copyable_vector<T> {
pure fn head() -> T;
pure fn init() -> ~[T];
pure fn last() -> T;
pure fn slice(start: uint, end: uint) -> ~[T];
pure fn tail() -> ~[T];
}
/// Extension methods for vectors
impl extensions/&<T: copy> for &[const T] {
impl extensions/&<T: copy> of copyable_vector<T> for &[const T] {
/// Returns the first element of a vector
#[inline]
pure fn head() -> T { head(self) }
......@@ -1227,8 +1245,26 @@ impl extensions/&<T: copy> for &[const T] {
pure fn tail() -> ~[T] { tail(self) }
}
trait immutable_vector/&<T> {
pure fn foldr<U: copy>(z: U, p: fn(T, U) -> U) -> U;
pure fn iter(f: fn(T));
pure fn iteri(f: fn(uint, T));
pure fn position(f: fn(T) -> bool) -> option<uint>;
pure fn position_elem(x: T) -> option<uint>;
pure fn riter(f: fn(T));
pure fn riteri(f: fn(uint, T));
pure fn rposition(f: fn(T) -> bool) -> option<uint>;
pure fn rposition_elem(x: T) -> option<uint>;
pure fn map<U>(f: fn(T) -> U) -> ~[U];
pure fn mapi<U>(f: fn(uint, T) -> U) -> ~[U];
fn map_r<U>(f: fn(x: &self.T) -> U) -> ~[U];
pure fn alli(f: fn(uint, T) -> bool) -> bool;
pure fn flat_map<U>(f: fn(T) -> ~[U]) -> ~[U];
pure fn filter_map<U: copy>(f: fn(T) -> option<U>) -> ~[U];
}
/// Extension methods for vectors
impl extensions/&<T> for &[T] {
impl extensions/&<T> of immutable_vector<T> for &[T] {
/// Reduce a vector from right to left
#[inline]
pure fn foldr<U: copy>(z: U, p: fn(T, U) -> U) -> U { foldr(self, z, p) }
......@@ -1336,8 +1372,14 @@ fn map_r<U>(f: fn(x: &self.T) -> U) -> ~[U] {
}
}
trait immutable_copyable_vector<T> {
pure fn filter(f: fn(T) -> bool) -> ~[T];
pure fn find(f: fn(T) -> bool) -> option<T>;
pure fn rfind(f: fn(T) -> bool) -> option<T>;
}
/// Extension methods for vectors
impl extensions/&<T: copy> for &[T] {
impl extensions/&<T: copy> of immutable_copyable_vector<T> for &[T] {
/**
* Construct a new vector from the elements of a vector for which some
* predicate holds.
......@@ -1510,7 +1552,16 @@ fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
fn contains(x: A) -> bool { iter::contains(self, x) }
fn count(x: A) -> uint { iter::count(self, x) }
}
impl extensions/&<A:copy> for &[const A] {
trait iter_trait_extensions<A> {
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
fn to_vec() -> ~[A];
fn min() -> A;
fn max() -> A;
}
impl extensions/&<A:copy> of iter_trait_extensions<A> for &[const A] {
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册