diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-07-11 12:45:54 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-07-11 12:47:32 -0700 |
| commit | fdf0c1b353393ff34b1dee6edfbfece13f7c3093 (patch) | |
| tree | 8b2801f2f3ce11e49e56a0e7d131fc725d2ed3eb /src/libcore | |
| parent | c0961bb88fe274795725c871871d7053429ae22e (diff) | |
core: Newtype a bunch of types in libcore
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/dvec.rs | 14 | ||||
| -rw-r--r-- | src/libcore/iter-trait.rs | 2 | ||||
| -rw-r--r-- | src/libcore/iter.rs | 9 | ||||
| -rw-r--r-- | src/libcore/pipes.rs | 27 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 7 | ||||
| -rw-r--r-- | src/libcore/str.rs | 38 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 65 |
7 files changed, 141 insertions, 21 deletions
diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index ec396f474a7..f9bb58380b2 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -46,28 +46,32 @@ export unwrap; * 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; } diff --git a/src/libcore/iter-trait.rs b/src/libcore/iter-trait.rs index 02c32a1bf0d..80b81753af8 100644 --- a/src/libcore/iter-trait.rs +++ b/src/libcore/iter-trait.rs @@ -21,7 +21,7 @@ impl extensions<A> of iter::base_iter<A> for IMPL_T<A> { } } -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) } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 55d47cfe75d..31e47d19c8f 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -7,6 +7,15 @@ iface times { 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| { diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index 658d59a4375..2b17a8a7874 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -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 @@ proto! streamp { } } -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> { diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 704c0fcaf4b..97795103f52 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -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) } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 2d9d9797909..cd172476563 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1882,8 +1882,15 @@ mod unsafe { } +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 @@ impl extensions for str { } } +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 diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index eb3d52edc24..a4bc4d66999 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1181,22 +1181,32 @@ pure fn unpack_mut_slice<T,U>(s: &[mut 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 @@ impl extensions/&<T> for &[T] { } } +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 @@ impl extensions/&<A> of iter::base_iter<A> for &[const A] { 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) } |
