Remove standalone comparison functions in vec, make the trait impls better.

This commit is contained in:
Huon Wilson
2013-07-03 12:13:00 +10:00
parent 9207802589
commit f19fb2459f
15 changed files with 184 additions and 206 deletions

View File

@@ -601,9 +601,8 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
ProcArgs { ProcArgs {
// If we've got another tool to run under (valgrind), // If we've got another tool to run under (valgrind),
// then split apart its command // then split apart its command
let toolargs = split_maybe_args(&config.runtool); let mut args = split_maybe_args(&config.runtool);
args.push(make_exe_name(config, testfile).to_str());
let mut args = toolargs + [make_exe_name(config, testfile).to_str()];
let prog = args.shift(); let prog = args.shift();
return ProcArgs {prog: prog, args: args}; return ProcArgs {prog: prog, args: args};
} }

View File

@@ -240,7 +240,6 @@ impl Digest for Sha1 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::vec;
use digest::{Digest, DigestUtil}; use digest::{Digest, DigestUtil};
use sha1::Sha1; use sha1::Sha1;
@@ -337,7 +336,7 @@ mod tests {
for tests.iter().advance |t| { for tests.iter().advance |t| {
(*sh).input_str(t.input); (*sh).input_str(t.input);
sh.result(out); sh.result(out);
assert!(vec::eq(t.output, out)); assert!(t.output.as_slice() == out);
let out_str = (*sh).result_str(); let out_str = (*sh).result_str();
assert_eq!(out_str.len(), 40); assert_eq!(out_str.len(), 40);
@@ -357,7 +356,7 @@ mod tests {
left = left - take; left = left - take;
} }
sh.result(out); sh.result(out);
assert!(vec::eq(t.output, out)); assert!(t.output.as_slice() == out);
let out_str = (*sh).result_str(); let out_str = (*sh).result_str();
assert_eq!(out_str.len(), 40); assert_eq!(out_str.len(), 40);

View File

@@ -55,7 +55,6 @@ use std::io;
use std::comm::GenericChan; use std::comm::GenericChan;
use std::comm::GenericPort; use std::comm::GenericPort;
use std::sys::size_of; use std::sys::size_of;
use std::vec;
/** /**
A FlatPort, consisting of a `BytePort` that receives byte vectors, A FlatPort, consisting of a `BytePort` that receives byte vectors,
@@ -274,7 +273,7 @@ impl<T,U:Unflattener<T>,P:BytePort> GenericPort<T> for FlatPort<T, U, P> {
} }
}; };
if vec::eq(command, CONTINUE) { if CONTINUE.as_slice() == command {
let msg_len = match self.byte_port.try_recv(size_of::<u64>()) { let msg_len = match self.byte_port.try_recv(size_of::<u64>()) {
Some(bytes) => { Some(bytes) => {
io::u64_from_be_bytes(bytes, 0, size_of::<u64>()) io::u64_from_be_bytes(bytes, 0, size_of::<u64>())
@@ -931,7 +930,7 @@ mod test {
fn test_try_recv_none3<P:BytePort>(loader: PortLoader<P>) { fn test_try_recv_none3<P:BytePort>(loader: PortLoader<P>) {
static CONTINUE: [u8, ..4] = [0xAA, 0xBB, 0xCC, 0xDD]; static CONTINUE: [u8, ..4] = [0xAA, 0xBB, 0xCC, 0xDD];
// The control word is followed by garbage // The control word is followed by garbage
let bytes = CONTINUE.to_owned() + [0]; let bytes = CONTINUE.to_owned() + &[0u8];
let port = loader(bytes); let port = loader(bytes);
let res: Option<int> = port.try_recv(); let res: Option<int> = port.try_recv();
assert!(res.is_none()); assert!(res.is_none());
@@ -955,7 +954,7 @@ mod test {
1, sys::size_of::<u64>()) |len_bytes| { 1, sys::size_of::<u64>()) |len_bytes| {
len_bytes.to_owned() len_bytes.to_owned()
}; };
let bytes = CONTINUE.to_owned() + len_bytes + [0, 0, 0, 0]; let bytes = CONTINUE.to_owned() + len_bytes + &[0u8, 0, 0, 0];
let port = loader(bytes); let port = loader(bytes);

View File

@@ -207,7 +207,7 @@ impl Add<BigUint, BigUint> for BigUint {
let new_len = uint::max(self.data.len(), other.data.len()); let new_len = uint::max(self.data.len(), other.data.len());
let mut carry = 0; let mut carry = 0;
let sum = do vec::from_fn(new_len) |i| { let mut sum = do vec::from_fn(new_len) |i| {
let ai = if i < self.data.len() { self.data[i] } else { 0 }; let ai = if i < self.data.len() { self.data[i] } else { 0 };
let bi = if i < other.data.len() { other.data[i] } else { 0 }; let bi = if i < other.data.len() { other.data[i] } else { 0 };
let (hi, lo) = BigDigit::from_uint( let (hi, lo) = BigDigit::from_uint(
@@ -216,8 +216,8 @@ impl Add<BigUint, BigUint> for BigUint {
carry = hi; carry = hi;
lo lo
}; };
if carry == 0 { return BigUint::new(sum) }; if carry != 0 { sum.push(carry); }
return BigUint::new(sum + [carry]); return BigUint::new(sum);
} }
} }
@@ -284,15 +284,15 @@ impl Mul<BigUint, BigUint> for BigUint {
if n == 1 { return copy *a; } if n == 1 { return copy *a; }
let mut carry = 0; let mut carry = 0;
let prod = do a.data.iter().transform |ai| { let mut prod = do a.data.iter().transform |ai| {
let (hi, lo) = BigDigit::from_uint( let (hi, lo) = BigDigit::from_uint(
(*ai as uint) * (n as uint) + (carry as uint) (*ai as uint) * (n as uint) + (carry as uint)
); );
carry = hi; carry = hi;
lo lo
}.collect::<~[BigDigit]>(); }.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(prod) }; if carry != 0 { prod.push(carry); }
return BigUint::new(prod + [carry]); return BigUint::new(prod);
} }
@@ -621,15 +621,15 @@ impl BigUint {
if n_bits == 0 || self.is_zero() { return copy *self; } if n_bits == 0 || self.is_zero() { return copy *self; }
let mut carry = 0; let mut carry = 0;
let shifted = do self.data.iter().transform |elem| { let mut shifted = do self.data.iter().transform |elem| {
let (hi, lo) = BigDigit::from_uint( let (hi, lo) = BigDigit::from_uint(
(*elem as uint) << n_bits | (carry as uint) (*elem as uint) << n_bits | (carry as uint)
); );
carry = hi; carry = hi;
lo lo
}.collect::<~[BigDigit]>(); }.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(shifted); } if carry != 0 { shifted.push(carry); }
return BigUint::new(shifted + [carry]); return BigUint::new(shifted);
} }

View File

@@ -131,13 +131,13 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
} }
ty::ty_struct(def_id, ref substs) => { ty::ty_struct(def_id, ref substs) => {
let fields = ty::lookup_struct_fields(cx.tcx, def_id); let fields = ty::lookup_struct_fields(cx.tcx, def_id);
let ftys = do fields.map |field| { let mut ftys = do fields.map |field| {
ty::lookup_field_type(cx.tcx, def_id, field.id, substs) ty::lookup_field_type(cx.tcx, def_id, field.id, substs)
}; };
let packed = ty::lookup_packed(cx.tcx, def_id); let packed = ty::lookup_packed(cx.tcx, def_id);
let dtor = ty::ty_dtor(cx.tcx, def_id).has_drop_flag(); let dtor = ty::ty_dtor(cx.tcx, def_id).has_drop_flag();
let ftys = if dtor { ftys.push(ty::mk_bool()); }
if dtor { ftys + [ty::mk_bool()] } else { ftys };
return Univariant(mk_struct(cx, ftys, packed), dtor) return Univariant(mk_struct(cx, ftys, packed), dtor)
} }
ty::ty_enum(def_id, ref substs) => { ty::ty_enum(def_id, ref substs) => {
@@ -263,7 +263,7 @@ fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
let padding = largest_size - most_aligned.size; let padding = largest_size - most_aligned.size;
struct_llfields(cx, most_aligned, sizing) struct_llfields(cx, most_aligned, sizing)
+ [Type::array(&Type::i8(), padding)] + &[Type::array(&Type::i8(), padding)]
} }
} }
} }
@@ -512,7 +512,7 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: int,
let discr_ty = C_int(ccx, discr); let discr_ty = C_int(ccx, discr);
let contents = build_const_struct(ccx, case, let contents = build_const_struct(ccx, case,
~[discr_ty] + vals); ~[discr_ty] + vals);
C_struct(contents + [padding(max_sz - case.size)]) C_struct(contents + &[padding(max_sz - case.size)])
} }
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => { NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
if discr == nndiscr { if discr == nndiscr {

View File

@@ -113,7 +113,7 @@ fn shim_types(ccx: @mut CrateContext, id: ast::node_id) -> ShimTypes {
_ => ccx.sess.bug("c_arg_and_ret_lltys called on non-function type") _ => ccx.sess.bug("c_arg_and_ret_lltys called on non-function type")
}; };
let llsig = foreign_signature(ccx, &fn_sig); let llsig = foreign_signature(ccx, &fn_sig);
let bundle_ty = Type::struct_(llsig.llarg_tys + [llsig.llret_ty.ptr_to()], false); let bundle_ty = Type::struct_(llsig.llarg_tys + &[llsig.llret_ty.ptr_to()], false);
let ret_def = !ty::type_is_bot(fn_sig.output) && let ret_def = !ty::type_is_bot(fn_sig.output) &&
!ty::type_is_nil(fn_sig.output); !ty::type_is_nil(fn_sig.output);
let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, llsig.llret_ty, ret_def); let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, llsig.llret_ty, ret_def);

View File

@@ -278,7 +278,7 @@ impl Reflector {
let opaqueptrty = ty::mk_ptr(ccx.tcx, ty::mt { ty: opaquety, mutbl: ast::m_imm }); let opaqueptrty = ty::mk_ptr(ccx.tcx, ty::mt { ty: opaquety, mutbl: ast::m_imm });
let make_get_disr = || { let make_get_disr = || {
let sub_path = bcx.fcx.path + [path_name(special_idents::anon)]; let sub_path = bcx.fcx.path + &[path_name(special_idents::anon)];
let sym = mangle_internal_name_by_path_and_seq(ccx, let sym = mangle_internal_name_by_path_and_seq(ccx,
sub_path, sub_path,
"get_disr"); "get_disr");

View File

@@ -245,7 +245,7 @@ impl Type {
} }
pub fn box(ctx: &CrateContext, ty: &Type) -> Type { pub fn box(ctx: &CrateContext, ty: &Type) -> Type {
Type::struct_(Type::box_header_fields(ctx) + [*ty], false) Type::struct_(Type::box_header_fields(ctx) + &[*ty], false)
} }
pub fn opaque_box(ctx: &CrateContext) -> Type { pub fn opaque_box(ctx: &CrateContext) -> Type {

View File

@@ -141,7 +141,7 @@ fn first_sentence_(s: &str) -> ~str {
pub fn paragraphs(s: &str) -> ~[~str] { pub fn paragraphs(s: &str) -> ~[~str] {
let mut whitespace_lines = 0; let mut whitespace_lines = 0;
let mut accum = ~""; let mut accum = ~"";
let paras = do s.any_line_iter().fold(~[]) |paras, line| { let mut paras = do s.any_line_iter().fold(~[]) |paras, line| {
let mut res = paras; let mut res = paras;
if line.is_whitespace() { if line.is_whitespace() {
@@ -166,11 +166,8 @@ pub fn paragraphs(s: &str) -> ~[~str] {
res res
}; };
if !accum.is_empty() { if !accum.is_empty() { paras.push(accum); }
paras + [accum] paras
} else {
paras
}
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -172,7 +172,7 @@ pub fn header_kind(doc: doc::ItemTag) -> ~str {
} }
pub fn header_name(doc: doc::ItemTag) -> ~str { pub fn header_name(doc: doc::ItemTag) -> ~str {
let fullpath = (doc.path() + [doc.name()]).connect("::"); let fullpath = (doc.path() + &[doc.name()]).connect("::");
match &doc { match &doc {
&doc::ModTag(_) if doc.id() != syntax::ast::crate_node_id => { &doc::ModTag(_) if doc.id() != syntax::ast::crate_node_id => {
fullpath fullpath

View File

@@ -163,7 +163,7 @@ pub fn make_filename(
} }
} }
doc::ItemPage(doc) => { doc::ItemPage(doc) => {
(doc.path() + [doc.name()]).connect("_") (doc.path() + &[doc.name()]).connect("_")
} }
} }
}; };

View File

@@ -176,13 +176,14 @@ pub fn to_managed<T:Copy>(v: &[T]) -> @[T] {
#[cfg(not(test))] #[cfg(not(test))]
pub mod traits { pub mod traits {
use at_vec::append; use at_vec::append;
use vec::Vector;
use kinds::Copy; use kinds::Copy;
use ops::Add; use ops::Add;
impl<'self,T:Copy> Add<&'self [T],@[T]> for @[T] { impl<'self,T:Copy, V: Vector<T>> Add<V,@[T]> for @[T] {
#[inline] #[inline]
fn add(&self, rhs: & &'self [T]) -> @[T] { fn add(&self, rhs: &V) -> @[T] {
append(*self, (*rhs)) append(*self, rhs.as_slice())
} }
} }
} }
@@ -312,7 +313,7 @@ mod test {
#[test] #[test]
fn append_test() { fn append_test() {
assert_eq!(@[1,2,3] + [4,5,6], @[1,2,3,4,5,6]); assert_eq!(@[1,2,3] + &[4,5,6], @[1,2,3,4,5,6]);
} }
#[test] #[test]

View File

@@ -72,7 +72,7 @@ pub use tuple::{CloneableTuple10, CloneableTuple11, CloneableTuple12};
pub use tuple::{ImmutableTuple2, ImmutableTuple3, ImmutableTuple4, ImmutableTuple5}; pub use tuple::{ImmutableTuple2, ImmutableTuple3, ImmutableTuple4, ImmutableTuple5};
pub use tuple::{ImmutableTuple6, ImmutableTuple7, ImmutableTuple8, ImmutableTuple9}; pub use tuple::{ImmutableTuple6, ImmutableTuple7, ImmutableTuple8, ImmutableTuple9};
pub use tuple::{ImmutableTuple10, ImmutableTuple11, ImmutableTuple12}; pub use tuple::{ImmutableTuple10, ImmutableTuple11, ImmutableTuple12};
pub use vec::{VectorVector, CopyableVector, ImmutableVector}; pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector}; pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector}; pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
pub use io::{Reader, ReaderUtil, Writer, WriterUtil}; pub use io::{Reader, ReaderUtil, Writer, WriterUtil};

View File

@@ -16,14 +16,13 @@ use cast::transmute;
use cast; use cast;
use container::{Container, Mutable}; use container::{Container, Mutable};
use cmp; use cmp;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use cmp::{Eq, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use clone::Clone; use clone::Clone;
use iterator::{FromIterator, Iterator, IteratorUtil}; use iterator::{FromIterator, Iterator, IteratorUtil};
use kinds::Copy; use kinds::Copy;
use libc; use libc;
use libc::c_void; use libc::c_void;
use num::Zero; use num::Zero;
use ops::Add;
use option::{None, Option, Some}; use option::{None, Option, Some};
use ptr::to_unsafe_ptr; use ptr::to_unsafe_ptr;
use ptr; use ptr;
@@ -40,8 +39,6 @@ use unstable::intrinsics::{get_tydesc, contains_managed};
use vec; use vec;
use util; use util;
#[cfg(not(test))] use cmp::Equiv;
#[doc(hidden)] #[doc(hidden)]
pub mod rustrt { pub mod rustrt {
use libc; use libc;
@@ -549,179 +546,165 @@ pub fn as_mut_buf<T,U>(s: &mut [T], f: &fn(*mut T, uint) -> U) -> U {
// Equality // Equality
/// Tests whether two slices are equal to one another. This is only true if both #[cfg(not(test))]
/// slices are of the same length, and each of the corresponding elements return pub mod traits {
/// true when queried via the `eq` function. use super::Vector;
fn eq<T: Eq>(a: &[T], b: &[T]) -> bool { use kinds::Copy;
let (a_len, b_len) = (a.len(), b.len()); use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
if a_len != b_len { return false; } use ops::Add;
let mut i = 0; impl<'self,T:Eq> Eq for &'self [T] {
while i < a_len { fn eq(&self, other: & &'self [T]) -> bool {
if a[i] != b[i] { return false; } self.len() == other.len() &&
i += 1; self.iter().zip(other.iter()).all(|(s,o)| *s == *o)
}
#[inline]
fn ne(&self, other: & &'self [T]) -> bool { !self.eq(other) }
} }
true
}
/// Similar to the `vec::eq` function, but this is defined for types which impl<T:Eq> Eq for ~[T] {
/// implement `TotalEq` as opposed to types which implement `Eq`. Equality #[inline]
/// comparisons are done via the `equals` function instead of `eq`. fn eq(&self, other: &~[T]) -> bool { self.as_slice() == *other }
fn equals<T: TotalEq>(a: &[T], b: &[T]) -> bool { #[inline]
let (a_len, b_len) = (a.len(), b.len()); fn ne(&self, other: &~[T]) -> bool { !self.eq(other) }
if a_len != b_len { return false; }
let mut i = 0;
while i < a_len {
if !a[i].equals(&b[i]) { return false; }
i += 1;
} }
true
}
#[cfg(not(test))] impl<T:Eq> Eq for @[T] {
impl<'self,T:Eq> Eq for &'self [T] { #[inline]
#[inline] fn eq(&self, other: &@[T]) -> bool { self.as_slice() == *other }
fn eq(&self, other: & &'self [T]) -> bool { eq(*self, *other) } #[inline]
#[inline] fn ne(&self, other: &@[T]) -> bool { !self.eq(other) }
fn ne(&self, other: & &'self [T]) -> bool { !self.eq(other) } }
}
#[cfg(not(test))] impl<'self,T:TotalEq> TotalEq for &'self [T] {
impl<T:Eq> Eq for ~[T] { fn equals(&self, other: & &'self [T]) -> bool {
#[inline] self.len() == other.len() &&
fn eq(&self, other: &~[T]) -> bool { eq(*self, *other) } self.iter().zip(other.iter()).all(|(s,o)| s.equals(o))
#[inline]
fn ne(&self, other: &~[T]) -> bool { !self.eq(other) }
}
#[cfg(not(test))]
impl<T:Eq> Eq for @[T] {
#[inline]
fn eq(&self, other: &@[T]) -> bool { eq(*self, *other) }
#[inline]
fn ne(&self, other: &@[T]) -> bool { !self.eq(other) }
}
#[cfg(not(test))]
impl<'self,T:TotalEq> TotalEq for &'self [T] {
#[inline]
fn equals(&self, other: & &'self [T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<T:TotalEq> TotalEq for ~[T] {
#[inline]
fn equals(&self, other: &~[T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<T:TotalEq> TotalEq for @[T] {
#[inline]
fn equals(&self, other: &@[T]) -> bool { equals(*self, *other) }
}
#[cfg(not(test))]
impl<'self,T:Eq> Equiv<~[T]> for &'self [T] {
#[inline]
fn equiv(&self, other: &~[T]) -> bool { eq(*self, *other) }
}
// Lexicographical comparison
fn cmp<T: TotalOrd>(a: &[T], b: &[T]) -> Ordering {
let low = uint::min(a.len(), b.len());
for uint::range(0, low) |idx| {
match a[idx].cmp(&b[idx]) {
Greater => return Greater,
Less => return Less,
Equal => ()
} }
} }
a.len().cmp(&b.len()) impl<T:TotalEq> TotalEq for ~[T] {
} #[inline]
fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
#[cfg(not(test))]
impl<'self,T:TotalOrd> TotalOrd for &'self [T] {
#[inline]
fn cmp(&self, other: & &'self [T]) -> Ordering { cmp(*self, *other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for ~[T] {
#[inline]
fn cmp(&self, other: &~[T]) -> Ordering { cmp(*self, *other) }
}
#[cfg(not(test))]
impl<T: TotalOrd> TotalOrd for @[T] {
#[inline]
fn cmp(&self, other: &@[T]) -> Ordering { cmp(*self, *other) }
}
fn lt<T:Ord>(a: &[T], b: &[T]) -> bool {
let (a_len, b_len) = (a.len(), b.len());
let end = uint::min(a_len, b_len);
let mut i = 0;
while i < end {
let (c_a, c_b) = (&a[i], &b[i]);
if *c_a < *c_b { return true; }
if *c_a > *c_b { return false; }
i += 1;
} }
a_len < b_len impl<T:TotalEq> TotalEq for @[T] {
} #[inline]
fn equals(&self, other: &@[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
fn le<T:Ord>(a: &[T], b: &[T]) -> bool { !lt(b, a) }
fn ge<T:Ord>(a: &[T], b: &[T]) -> bool { !lt(a, b) }
fn gt<T:Ord>(a: &[T], b: &[T]) -> bool { lt(b, a) }
#[cfg(not(test))]
impl<'self,T:Ord> Ord for &'self [T] {
#[inline]
fn lt(&self, other: & &'self [T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: & &'self [T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: & &'self [T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: & &'self [T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for ~[T] {
#[inline]
fn lt(&self, other: &~[T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: &~[T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: &~[T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: &~[T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<T:Ord> Ord for @[T] {
#[inline]
fn lt(&self, other: &@[T]) -> bool { lt((*self), (*other)) }
#[inline]
fn le(&self, other: &@[T]) -> bool { le((*self), (*other)) }
#[inline]
fn ge(&self, other: &@[T]) -> bool { ge((*self), (*other)) }
#[inline]
fn gt(&self, other: &@[T]) -> bool { gt((*self), (*other)) }
}
#[cfg(not(test))]
impl<'self,T:Copy> Add<&'self [T], ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: & &'self [T]) -> ~[T] {
append(copy *self, (*rhs))
} }
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for &'self [T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for ~[T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:Eq, V: Vector<T>> Equiv<V> for @[T] {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
}
impl<'self,T:TotalOrd> TotalOrd for &'self [T] {
fn cmp(&self, other: & &'self [T]) -> Ordering {
for self.iter().zip(other.iter()).advance |(s,o)| {
match s.cmp(o) {
Equal => {},
non_eq => { return non_eq; }
}
}
self.len().cmp(&other.len())
}
}
impl<T: TotalOrd> TotalOrd for ~[T] {
#[inline]
fn cmp(&self, other: &~[T]) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl<T: TotalOrd> TotalOrd for @[T] {
#[inline]
fn cmp(&self, other: &@[T]) -> Ordering { self.as_slice().cmp(&other.as_slice()) }
}
impl<'self,T:Ord> Ord for &'self [T] {
fn lt(&self, other: & &'self [T]) -> bool {
for self.iter().zip(other.iter()).advance |(s,o)| {
if *s < *o { return true; }
if *s > *o { return false; }
}
self.len() < other.len()
}
#[inline]
fn le(&self, other: & &'self [T]) -> bool { !(*other < *self) }
#[inline]
fn ge(&self, other: & &'self [T]) -> bool { !(*self < *other) }
#[inline]
fn gt(&self, other: & &'self [T]) -> bool { *other < *self }
}
impl<T:Ord> Ord for ~[T] {
#[inline]
fn lt(&self, other: &~[T]) -> bool { self.as_slice() < other.as_slice() }
#[inline]
fn le(&self, other: &~[T]) -> bool { self.as_slice() <= other.as_slice() }
#[inline]
fn ge(&self, other: &~[T]) -> bool { self.as_slice() >= other.as_slice() }
#[inline]
fn gt(&self, other: &~[T]) -> bool { self.as_slice() > other.as_slice() }
}
impl<T:Ord> Ord for @[T] {
#[inline]
fn lt(&self, other: &@[T]) -> bool { self.as_slice() < other.as_slice() }
#[inline]
fn le(&self, other: &@[T]) -> bool { self.as_slice() <= other.as_slice() }
#[inline]
fn ge(&self, other: &@[T]) -> bool { self.as_slice() >= other.as_slice() }
#[inline]
fn gt(&self, other: &@[T]) -> bool { self.as_slice() > other.as_slice() }
}
impl<'self,T:Copy, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
res.push_all(rhs.as_slice());
res
}
}
impl<T:Copy, V: Vector<T>> Add<V, ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
res.push_all(rhs.as_slice());
res
}
}
}
#[cfg(test)]
pub mod traits {}
/// Any vector that can be represented as a slice.
pub trait Vector<T> {
/// Work with `self` as a slice.
fn as_slice<'a>(&'a self) -> &'a [T];
}
impl<'self,T> Vector<T> for &'self [T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { *self }
}
impl<T> Vector<T> for ~[T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { let v: &'a [T] = *self; v }
}
impl<T> Vector<T> for @[T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { let v: &'a [T] = *self; v }
} }
impl<'self, T> Container for &'self [T] { impl<'self, T> Container for &'self [T] {

View File

@@ -96,7 +96,7 @@ fn recurse_or_fail(depth: int, st: Option<State>) {
fn_box: || @Cons((), fn_box()), fn_box: || @Cons((), fn_box()),
tuple: (@Cons((), st.tuple.first()), tuple: (@Cons((), st.tuple.first()),
~Cons((), @*st.tuple.second())), ~Cons((), @*st.tuple.second())),
vec: st.vec + [@Cons((), *st.vec.last())], vec: st.vec + &[@Cons((), *st.vec.last())],
res: r(@Cons((), st.res._l)) res: r(@Cons((), st.res._l))
} }
} }