From fdf0c1b353393ff34b1dee6edfbfece13f7c3093 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 11 Jul 2012 12:45:54 -0700 Subject: [PATCH] core: Newtype a bunch of types in libcore --- src/libcore/dvec.rs | 14 ++++++--- src/libcore/iter-trait.rs | 2 +- src/libcore/iter.rs | 9 ++++++ src/libcore/pipes.rs | 27 +++++++++++++--- src/libcore/ptr.rs | 7 ++++- src/libcore/str.rs | 38 +++++++++++++++++++++-- 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 @@ * pointers achieved about 103 million pushes/second. Using an option * type could only produce 47 million pushes/second. */ -type dvec = { +type dvec_ = { mut data: ~[mut A] }; +enum dvec { + dvec_(dvec_) +} + /// Creates a new, empty dvec fn dvec() -> dvec { - {mut data: ~[mut]} + dvec_({mut data: ~[mut]}) } /// Creates a new dvec with a single element fn from_elem(+e: A) -> dvec { - {mut data: ~[mut e]} + dvec_({mut data: ~[mut e]}) } /// Creates a new dvec with the contents of a vector fn from_vec(+v: ~[mut A]) -> dvec { - {mut data: v} + dvec_({mut data: v}) } /// Consumes the vector and returns its contents fn unwrap(-d: dvec) -> ~[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 @@ fn position(f: fn(A) -> bool) -> option { } } -impl extensions for IMPL_T { +impl extensions of iter::copyable_iter for IMPL_T { 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 @@ fn times(it: fn() -> bool); } +trait copyable_iter { + fn filter_to_vec(pred: fn(A) -> bool) -> ~[A]; + fn map_to_vec(op: fn(A) -> B) -> ~[B]; + fn to_vec() -> ~[A]; + fn min() -> A; + fn max() -> A; + fn find(p: fn(A) -> bool) -> option; +} + fn eachi>(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 = { +enum packet_header { + packet_header_(packet_header_) +} + +type packet_ = { header: packet_header, mut payload: option }; +enum packet { + packet_(packet_) +} + fn packet() -> *packet unsafe { let p: *packet = unsafe::transmute(~{ header: { @@ -428,8 +436,17 @@ fn spawn_service_recv( } } -type chan = { mut endp: option> }; -type port = { mut endp: option> }; +type chan_ = { mut endp: option> }; + +enum chan { + chan_(chan_) +} + +type port_ = { mut endp: option> }; + +enum port { + port_(port_) +} fn stream() -> (chan, port) { let (c, s) = streamp::init(); @@ -439,7 +456,7 @@ fn stream() -> (chan, port) { 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 for chan { 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(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 for *T { +impl extensions 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 @@ 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 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 @@ fn as_mut_buf(v: &[mut E], f: fn(*mut E) -> T) -> T { } } -impl extensions for ~[T] { +trait vec_concat { + pure fn +(rhs: &[const T]) -> self; +} + +impl extensions of vec_concat for ~[T] { #[inline(always)] pure fn +(rhs: &[const T]) -> ~[T] { append(self, rhs) } } -impl extensions for ~[mut T] { +impl extensions of vec_concat 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/& for &[const T] { +impl extensions/& 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/& for &[const T] { pure fn len() -> uint { len(self) } } +trait copyable_vector { + 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/& for &[const T] { +impl extensions/& of copyable_vector for &[const T] { /// Returns the first element of a vector #[inline] pure fn head() -> T { head(self) } @@ -1227,8 +1245,26 @@ impl extensions/& for &[const T] { pure fn tail() -> ~[T] { tail(self) } } +trait immutable_vector/& { + pure fn foldr(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; + pure fn position_elem(x: T) -> option; + pure fn riter(f: fn(T)); + pure fn riteri(f: fn(uint, T)); + pure fn rposition(f: fn(T) -> bool) -> option; + pure fn rposition_elem(x: T) -> option; + pure fn map(f: fn(T) -> U) -> ~[U]; + pure fn mapi(f: fn(uint, T) -> U) -> ~[U]; + fn map_r(f: fn(x: &self.T) -> U) -> ~[U]; + pure fn alli(f: fn(uint, T) -> bool) -> bool; + pure fn flat_map(f: fn(T) -> ~[U]) -> ~[U]; + pure fn filter_map(f: fn(T) -> option) -> ~[U]; +} + /// Extension methods for vectors -impl extensions/& for &[T] { +impl extensions/& of immutable_vector for &[T] { /// Reduce a vector from right to left #[inline] pure fn foldr(z: U, p: fn(T, U) -> U) -> U { foldr(self, z, p) } @@ -1336,8 +1372,14 @@ fn map_r(f: fn(x: &self.T) -> U) -> ~[U] { } } +trait immutable_copyable_vector { + pure fn filter(f: fn(T) -> bool) -> ~[T]; + pure fn find(f: fn(T) -> bool) -> option; + pure fn rfind(f: fn(T) -> bool) -> option; +} + /// Extension methods for vectors -impl extensions/& for &[T] { +impl extensions/& of immutable_copyable_vector for &[T] { /** * Construct a new vector from the elements of a vector for which some * predicate holds. @@ -1510,7 +1552,16 @@ fn foldl(+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/& for &[const A] { + +trait iter_trait_extensions { + fn filter_to_vec(pred: fn(A) -> bool) -> ~[A]; + fn map_to_vec(op: fn(A) -> B) -> ~[B]; + fn to_vec() -> ~[A]; + fn min() -> A; + fn max() -> A; +} + +impl extensions/& of iter_trait_extensions for &[const A] { fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] { iter::filter_to_vec(self, pred) } -- GitLab